line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cache::Memory::Simple::Scalar; |
2
|
1
|
|
|
1
|
|
41477
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
3
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
4
|
1
|
|
|
1
|
|
4
|
use utf8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1136
|
use Time::HiRes; |
|
1
|
|
|
|
|
3264
|
|
|
1
|
|
|
|
|
6
|
|
7
|
1
|
|
|
1
|
|
403
|
use 5.008001; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
55
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use constant { |
10
|
1
|
|
|
|
|
659
|
TIME => 0, |
11
|
|
|
|
|
|
|
VALUE => 1, |
12
|
1
|
|
|
1
|
|
7
|
}; |
|
1
|
|
|
|
|
2
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
3
|
|
|
3
|
1
|
3166
|
my ($class) = @_; |
16
|
3
|
|
|
|
|
15
|
bless [undef, undef], $class; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub get { |
20
|
10
|
|
|
10
|
1
|
32
|
my ($self) = @_; |
21
|
10
|
100
|
|
|
|
25
|
if (defined $self->[TIME]) { # data with expiration |
22
|
5
|
100
|
|
|
|
12
|
if ($self->[TIME] > Time::HiRes::time()) { |
23
|
3
|
|
|
|
|
29
|
return $self->[VALUE]; |
24
|
|
|
|
|
|
|
} else { |
25
|
2
|
|
|
|
|
15
|
undef $self->[VALUE]; # remove expired data |
26
|
2
|
|
|
|
|
7
|
return undef; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
} else { |
29
|
5
|
|
|
|
|
21
|
return $self->[VALUE]; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub get_or_set { |
34
|
0
|
|
|
0
|
1
|
0
|
my ($self, $code, $expiration) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
0
|
|
|
|
0
|
if (my $val = $self->get()) { |
37
|
0
|
|
|
|
|
0
|
return $val; |
38
|
|
|
|
|
|
|
} else { |
39
|
0
|
|
|
|
|
0
|
my $val = $code->(); |
40
|
0
|
|
|
|
|
0
|
$self->set($val, $expiration); |
41
|
0
|
|
|
|
|
0
|
return $val; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub set { |
46
|
3
|
|
|
3
|
1
|
6
|
my ($self, $val, $expiration) = @_; |
47
|
3
|
100
|
|
|
|
13
|
$self->[TIME] = defined($expiration) |
48
|
|
|
|
|
|
|
? $expiration + Time::HiRes::time() |
49
|
|
|
|
|
|
|
: undef; |
50
|
3
|
|
|
|
|
19
|
$self->[VALUE] = $val; |
51
|
3
|
|
|
|
|
5
|
return $val; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub delete :method { |
55
|
1
|
|
|
1
|
1
|
2
|
my ($self) = @_; |
56
|
1
|
|
|
|
|
3
|
undef $self->[VALUE]; |
57
|
1
|
|
|
|
|
3
|
return undef; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |