line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cache::Scalar::WithExpiry; |
2
|
3
|
|
|
3
|
|
56422
|
use 5.008001; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
137
|
|
3
|
3
|
|
|
3
|
|
18
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
99
|
|
4
|
3
|
|
|
3
|
|
38
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
154
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
18
|
use Carp (); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
118
|
|
9
|
3
|
|
|
3
|
|
3468
|
use Time::HiRes; |
|
3
|
|
|
|
|
6323
|
|
|
3
|
|
|
|
|
17
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
3139
|
use parent 'Exporter'; |
|
3
|
|
|
|
|
9509
|
|
|
3
|
|
|
|
|
21
|
|
12
|
|
|
|
|
|
|
our @EXPORT = qw/cache_with_expiry/; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use constant { |
15
|
3
|
|
|
|
|
1888
|
TIME => 0, |
16
|
|
|
|
|
|
|
VALUE => 1, |
17
|
3
|
|
|
3
|
|
260
|
}; |
|
3
|
|
|
|
|
6
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
12
|
|
|
12
|
1
|
13494
|
my ($class) = @_; |
21
|
12
|
|
|
|
|
77
|
bless [undef, undef], $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub get { |
25
|
32
|
|
|
32
|
1
|
60
|
my ($self) = @_; |
26
|
|
|
|
|
|
|
|
27
|
32
|
100
|
100
|
|
|
129
|
if (defined $self->[TIME] && $self->[TIME] <= Time::HiRes::time()) { |
28
|
9
|
|
|
|
|
157
|
undef $self->[VALUE]; |
29
|
9
|
|
|
|
|
14
|
undef $self->[TIME]; |
30
|
|
|
|
|
|
|
} |
31
|
32
|
50
|
|
|
|
235
|
wantarray ? ($self->[VALUE], $self->[TIME]) : $self->[VALUE]; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub get_or_set { |
35
|
20
|
|
|
20
|
1
|
121
|
my ($self, $code) = @_; |
36
|
|
|
|
|
|
|
|
37
|
20
|
100
|
|
|
|
38
|
if (defined $self->get) { |
38
|
6
|
100
|
|
|
|
21
|
return wantarray ? ($self->[VALUE], $self->[TIME]) : $self->[VALUE]; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
14
|
|
|
|
|
160
|
my ($val, $expiry) = $code->(); |
42
|
14
|
|
|
|
|
128
|
return $self->set($val, $expiry); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub set { |
47
|
18
|
|
|
18
|
1
|
1971
|
my ($self, $val, $expiry) = @_; |
48
|
|
|
|
|
|
|
|
49
|
18
|
100
|
66
|
|
|
86
|
if (!$expiry || $expiry <= 0) { |
50
|
2
|
|
|
|
|
445
|
Carp::carp 'Expiry time is required. Value is not to be cached.'; |
51
|
2
|
|
|
|
|
119
|
undef $expiry; |
52
|
2
|
|
|
|
|
5
|
undef $self->[VALUE]; |
53
|
2
|
|
|
|
|
4
|
undef $self->[TIME]; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
16
|
|
|
|
|
19
|
$self->[TIME] = $expiry; |
57
|
16
|
|
|
|
|
28
|
$self->[VALUE] = $val; |
58
|
|
|
|
|
|
|
} |
59
|
18
|
100
|
|
|
|
60
|
wantarray ? ($val, $expiry) : $val; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub delete :method { |
63
|
1
|
|
|
1
|
1
|
3
|
my ($self) = @_; |
64
|
1
|
|
|
|
|
3
|
undef $self->[VALUE]; |
65
|
1
|
|
|
|
|
3
|
return undef; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
{ |
69
|
|
|
|
|
|
|
my %_obj; |
70
|
|
|
|
|
|
|
sub cache_with_expiry(&) { |
71
|
18
|
|
|
18
|
1
|
8062
|
my $code = shift; |
72
|
18
|
|
66
|
|
|
77
|
my $obj = $_obj{$code+0} ||= __PACKAGE__->new; |
73
|
18
|
|
|
|
|
38
|
$obj->get_or_set($code); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
78
|
|
|
|
|
|
|
__END__ |