line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::Cache::LRU::Expires; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
637
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
4
|
1
|
|
|
1
|
|
646
|
use Tie::Cache::LRU 0.21; |
|
1
|
|
|
|
|
13006
|
|
|
1
|
|
|
|
|
31
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
8
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
426
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION="0.55"; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub TIEHASH { |
11
|
1
|
|
|
1
|
|
41
|
my $class = shift; |
12
|
1
|
|
|
|
|
7
|
my $self = { |
13
|
|
|
|
|
|
|
LRU => undef, |
14
|
|
|
|
|
|
|
LRUOBJ => undef, |
15
|
|
|
|
|
|
|
ENTRIES => 100, |
16
|
|
|
|
|
|
|
EXPIRES => 3600, |
17
|
|
|
|
|
|
|
@_, |
18
|
|
|
|
|
|
|
}; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
3
|
tie %{$self->{LRU}}, 'Tie::Cache::LRU', $self->{ENTRIES}; |
|
1
|
|
|
|
|
13
|
|
21
|
1
|
|
|
|
|
60
|
$self->{LRUOBJ}=tied %{$self->{LRU}}; |
|
1
|
|
|
|
|
3
|
|
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
|
|
4
|
return bless $self, $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub FETCH { |
27
|
62
|
|
|
62
|
|
9001230
|
my $self =shift; |
28
|
62
|
|
|
|
|
78
|
my $key =shift; |
29
|
|
|
|
|
|
|
|
30
|
62
|
|
|
|
|
195
|
my $value=$self->{LRUOBJ}->FETCH($key); |
31
|
|
|
|
|
|
|
|
32
|
62
|
100
|
|
|
|
1091
|
if (defined $value) { |
33
|
41
|
|
|
|
|
53
|
my $curtime=time(); |
34
|
41
|
100
|
|
|
|
88
|
if ($curtime <= $value->{BORDER}) { |
35
|
32
|
|
|
|
|
200
|
return $value->{VALUE}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
else { |
38
|
9
|
|
|
|
|
36
|
return undef; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
21
|
|
|
|
|
52
|
return undef; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub STORE { |
47
|
23
|
|
|
23
|
|
2000691
|
my $self = shift; |
48
|
23
|
|
|
|
|
34
|
my $key = shift; |
49
|
23
|
|
|
|
|
27
|
my $val = shift; |
50
|
|
|
|
|
|
|
|
51
|
23
|
|
|
|
|
85
|
my $value={ |
52
|
|
|
|
|
|
|
VALUE => $val, |
53
|
|
|
|
|
|
|
BORDER => time()+$self->{EXPIRES} |
54
|
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
23
|
|
|
|
|
85
|
$self->{LRUOBJ}->STORE($key,$value); |
57
|
23
|
|
|
|
|
828
|
return $val; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub DELETE { |
61
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
62
|
0
|
|
|
|
|
0
|
my $key = shift; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
$self->{LRUOBJ}->DELETE($key); |
65
|
0
|
|
|
|
|
0
|
return undef; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub FIRSTKEY { |
69
|
1
|
|
|
1
|
|
33
|
return undef; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub NEXTKEY { |
73
|
0
|
|
|
0
|
|
0
|
return undef; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
0
|
|
0
|
sub DESTROY { |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub CLEAR { |
80
|
1
|
|
|
1
|
|
64
|
my ($self) = @_; |
81
|
1
|
|
|
|
|
17
|
$self->{LRUOBJ}->CLEAR(); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub EXISTS { |
85
|
3
|
|
|
3
|
|
5000228
|
my $self = shift; |
86
|
3
|
|
|
|
|
10
|
my $key = shift; |
87
|
3
|
100
|
|
|
|
65
|
if ($self->{LRUOBJ}->EXISTS($key)) { |
88
|
2
|
|
|
|
|
24
|
my $curtime=time(); |
89
|
2
|
|
|
|
|
218
|
my $value=$self->{LRUOBJ}->FETCH($key); |
90
|
2
|
100
|
|
|
|
64
|
if ($curtime <= $value->{BORDER}) { |
91
|
1
|
|
|
|
|
4
|
return 1; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
1
|
|
|
|
|
7
|
return 0; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
else { |
98
|
1
|
|
|
|
|
8
|
return 0; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub lru_size { |
103
|
11
|
|
|
11
|
0
|
245
|
my $self = shift; |
104
|
11
|
|
|
|
|
25
|
my $lru = $self->{LRUOBJ}; |
105
|
11
|
|
|
|
|
46
|
return $lru->curr_size(); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=pod |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 NAME |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Tie::Cache::LRU::Expires - Extends Tie::Cache::LRU with expiring |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SYNOPSIS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
use Tie::Cache::LRU::Expires; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
tie %cache, 'Tie::Cache::LRU::Expires', EXPIRES => 10, ENTRIES => 1000; |
119
|
|
|
|
|
|
|
$cache_obj = tied %cache; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
for(1..1000) { |
122
|
|
|
|
|
|
|
$cache{$_}="test $_"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
sleep 4; |
125
|
|
|
|
|
|
|
for(1000..1500) { |
126
|
|
|
|
|
|
|
$cache{$_}="test $_"; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
print $cache_obj->lru_size(),"\n"; # access to the |
130
|
|
|
|
|
|
|
# number of entries |
131
|
|
|
|
|
|
|
# used in the LRU |
132
|
|
|
|
|
|
|
# cache. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sleep 4; |
135
|
|
|
|
|
|
|
for(1..10) { print $cache{$_},"\n"; } |
136
|
|
|
|
|
|
|
for(1100..1110) { print $cache{$_},"\n"; } |
137
|
|
|
|
|
|
|
sleep 4; |
138
|
|
|
|
|
|
|
for(1..10) { print $cache{$_},"\n"; } # expired (undefs). |
139
|
|
|
|
|
|
|
for(1100..1110) { print $cache{$_},"\n"; } |
140
|
|
|
|
|
|
|
sleep 4; |
141
|
|
|
|
|
|
|
for(1100..1110) { print $cache{$_},"\n"; } # now also expired. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Clearing the cache |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
%cache=(); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 DESCRIPTION |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
This is an expiring LRU cache, using Tie::Cache::LRU. Each entry |
152
|
|
|
|
|
|
|
in this cache expires after 'EXPIRES' seconds (default 3600). The |
153
|
|
|
|
|
|
|
cache is in RAM (see Tie::Cache::LRU). ENTRIES provides the maximum |
154
|
|
|
|
|
|
|
number of entries in the Tie::Cache::LRU cache. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
It works by checking if a cached entry hasn't expired. If it has, |
157
|
|
|
|
|
|
|
undef is returned, otherwise it's value. If the entry wasn't cached, |
158
|
|
|
|
|
|
|
undef is also returned (of course). Expired entries will eventually |
159
|
|
|
|
|
|
|
drop of the LRU; or, if referenced will (as can be expected, otherwise |
160
|
|
|
|
|
|
|
they wouldn't be referenced) be refreshed. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
NB! If entries keep being referenced and are expired, but never refreshed, |
163
|
|
|
|
|
|
|
they will never leave the LRU! |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head1 SEE ALSO |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
L. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 USAGE |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
See SYNOPSIS. Too simple to explain. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 AUTHOR |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Hans Oesterholt-Dijkema |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 LICENSE |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Artistic |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=cut |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
1; |