line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cache::Memory::Simple; |
2
|
3
|
|
|
3
|
|
119641
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
119
|
|
3
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
93
|
|
4
|
3
|
|
|
3
|
|
3449
|
use Time::HiRes; |
|
3
|
|
|
|
|
7820
|
|
|
3
|
|
|
|
|
15
|
|
5
|
3
|
|
|
3
|
|
378
|
use 5.008001; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
1595
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.03'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
7
|
|
|
7
|
1
|
7991
|
my ($class) = @_; |
10
|
7
|
|
|
|
|
28
|
bless {}, $class; |
11
|
|
|
|
|
|
|
} |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub get { |
14
|
18
|
|
|
18
|
1
|
400303
|
my ($self, $key) = @_; |
15
|
18
|
|
|
|
|
40
|
my $val = $self->{$key}; |
16
|
18
|
100
|
|
|
|
52
|
if (defined $val->[0]) { |
17
|
6
|
100
|
|
|
|
32
|
if ($val->[0] > Time::HiRes::time() ) { |
18
|
4
|
|
|
|
|
47
|
return $val->[1]; |
19
|
|
|
|
|
|
|
} else { |
20
|
2
|
|
|
|
|
16
|
delete $self->{$key}; # remove expired data |
21
|
2
|
|
|
|
|
22
|
return undef; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} else { |
24
|
12
|
|
|
|
|
49
|
return $val->[1]; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub get_or_set { |
29
|
0
|
|
|
0
|
1
|
0
|
my ($self, $key, $code, $expiration) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
0
|
if (my $val = $self->get($key)) { |
32
|
0
|
|
|
|
|
0
|
return $val; |
33
|
|
|
|
|
|
|
} else { |
34
|
0
|
|
|
|
|
0
|
my $val = $code->(); |
35
|
0
|
|
|
|
|
0
|
$self->set($key, $val, $expiration); |
36
|
0
|
|
|
|
|
0
|
return $val; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub set { |
41
|
10
|
|
|
10
|
1
|
32
|
my ($self, $key, $val, $expiration) = @_; |
42
|
10
|
100
|
|
|
|
48
|
$self->{$key} = [defined($expiration) |
43
|
|
|
|
|
|
|
? $expiration + Time::HiRes::time() |
44
|
|
|
|
|
|
|
: undef, |
45
|
|
|
|
|
|
|
$val]; |
46
|
10
|
|
|
|
|
51
|
return $val; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub delete :method { |
50
|
1
|
|
|
1
|
1
|
2
|
my ($self, $key) = @_; |
51
|
1
|
|
|
|
|
4
|
delete $self->{$key}; |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
0
|
1
|
0
|
sub remove { shift->delete(@_) } # alias |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub delete_all { |
56
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
57
|
1
|
|
|
|
|
5
|
delete $self->{$_} for keys %$self; |
58
|
1
|
|
|
|
|
3
|
return; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub purge { |
62
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
63
|
1
|
|
|
|
|
2
|
for my $key (keys %{$self}) { |
|
1
|
|
|
|
|
3
|
|
64
|
3
|
|
|
|
|
13
|
my $entry = $self->{$key}->[0]; |
65
|
3
|
100
|
100
|
|
|
12
|
if (defined($entry) && $entry < Time::HiRes::time() ) { |
66
|
1
|
|
|
|
|
12
|
delete $self->{$key}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub count { |
72
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
73
|
2
|
|
|
|
|
3
|
return 0+keys %{$self}; |
|
2
|
|
|
|
|
8
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
__END__ |