line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################################################### |
2
|
|
|
|
|
|
|
# File - ExpireLRU.pm |
3
|
|
|
|
|
|
|
# Created 12 Feb, 2000, Brent B. Powers |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Purpose - This package implements LRU expiration. It does this by |
6
|
|
|
|
|
|
|
# using a bunch of different data structures. Tuning |
7
|
|
|
|
|
|
|
# support is included, but costs performance. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# ToDo - Test the further tie stuff |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# Copyright(c) 2000 Brent B. Powers and B2Pi LLC |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# You may copy and distribute this program under the same terms as |
14
|
|
|
|
|
|
|
# Perl itself. |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
########################################################################### |
17
|
|
|
|
|
|
|
package Memoize::ExpireLRU; |
18
|
|
|
|
|
|
|
$Memoize::ExpireLRU::VERSION = '0.55_02'; # TRIAL |
19
|
1
|
|
|
1
|
|
2632
|
use 5.006; |
|
1
|
|
|
|
|
2
|
|
20
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
21
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
27
|
|
22
|
1
|
|
|
1
|
|
479
|
use AutoLoader qw(AUTOLOAD); |
|
1
|
|
|
|
|
1020
|
|
|
1
|
|
|
|
|
4
|
|
23
|
1
|
|
|
1
|
|
26
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1025
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our $DEBUG = 0; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# Usage: memoize func , |
28
|
|
|
|
|
|
|
# TIE => [ |
29
|
|
|
|
|
|
|
# Memoize::ExpireLRU, |
30
|
|
|
|
|
|
|
# CACHESIZE => n, |
31
|
|
|
|
|
|
|
# TUNECACHESIZE => m, |
32
|
|
|
|
|
|
|
# INSTANCE => IDString |
33
|
|
|
|
|
|
|
# TIE => [...] |
34
|
|
|
|
|
|
|
# ] |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
############################################# |
37
|
|
|
|
|
|
|
## |
38
|
|
|
|
|
|
|
## This used to all be a bit more reasonable, but then it turns out |
39
|
|
|
|
|
|
|
## that Memoize doesn't call FETCH if EXISTS returns true and it's in |
40
|
|
|
|
|
|
|
## scalar context. Thus, everything really has to be done in the |
41
|
|
|
|
|
|
|
## EXISTS code. Harumph. |
42
|
|
|
|
|
|
|
## |
43
|
|
|
|
|
|
|
############################################# |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
our @AllTies; |
46
|
|
|
|
|
|
|
our $EndDebug = 0; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
1; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub TIEHASH { |
51
|
3
|
|
|
3
|
|
409
|
my ($package, %args, %cache, @index, @Tune, @Stats); |
52
|
3
|
|
|
|
|
8
|
($package, %args)= @_; |
53
|
3
|
|
|
|
|
27
|
my($self) = bless \%args => $package; |
54
|
|
|
|
|
|
|
$self->{CACHESIZE} or |
55
|
3
|
50
|
|
|
|
10
|
croak "Memoize::ExpireLRU: CACHESIZE must be specified >0; aborting"; |
56
|
3
|
|
100
|
|
|
9
|
$self->{TUNECACHESIZE} ||= 0; |
57
|
3
|
100
|
|
|
|
5
|
delete($self->{TUNECACHESIZE}) unless $self->{TUNECACHESIZE}; |
58
|
3
|
|
|
|
|
4
|
$self->{C} = \%cache; |
59
|
3
|
|
|
|
|
2
|
$self->{I} = \@index; |
60
|
3
|
50
|
|
|
|
5
|
defined($self->{INSTANCE}) or $self->{INSTANCE} = "$self"; |
61
|
3
|
|
|
|
|
6
|
foreach (@AllTies) { |
62
|
3
|
50
|
|
|
|
8
|
if ($_->{INSTANCE} eq $self->{INSTANCE}) { |
63
|
0
|
|
|
|
|
0
|
croak "Memoize::ExpireLRU: Attempt to register the same routine twice; aborting"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
3
|
100
|
|
|
|
6
|
if ($self->{TUNECACHESIZE}) { |
67
|
2
|
|
|
|
|
2
|
$EndDebug = 1; |
68
|
2
|
|
|
|
|
5
|
for (my $i = 0; $i < $args{TUNECACHESIZE}; $i++) { |
69
|
11
|
|
|
|
|
15
|
$Stats[$i] = 0; |
70
|
|
|
|
|
|
|
} |
71
|
2
|
|
|
|
|
2
|
$self->{T} = \@Stats; |
72
|
2
|
|
|
|
|
2
|
$self->{TI} = \@Tune; |
73
|
2
|
|
|
|
|
4
|
$self->{cm} = $args{ch} = $args{th} = 0; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
3
|
50
|
|
|
|
5
|
if ($self->{TIE}) { |
78
|
0
|
|
|
|
|
0
|
my($module, $modulefile, @opts, $rc, %tcache); |
79
|
0
|
|
|
|
|
0
|
($module, @opts) = @{$args{TIE}}; |
|
0
|
|
|
|
|
0
|
|
80
|
0
|
|
|
|
|
0
|
$modulefile = $module . '.pm'; |
81
|
0
|
|
|
|
|
0
|
$modulefile =~ s{::}{/}g; |
82
|
0
|
|
|
|
|
0
|
eval { require $modulefile }; |
|
0
|
|
|
|
|
0
|
|
83
|
0
|
0
|
|
|
|
0
|
if ($@) { |
84
|
0
|
|
|
|
|
0
|
croak "Memoize::ExpireLRU: Couldn't load hash tie module `$module': $@; aborting"; |
85
|
|
|
|
|
|
|
} |
86
|
0
|
|
|
|
|
0
|
$rc = (tie %tcache => $module, @opts); |
87
|
0
|
0
|
|
|
|
0
|
unless ($rc) { |
88
|
0
|
|
|
|
|
0
|
croak "Memoize::ExpireLRU: Couldn't tie hash to `$module': $@; aborting"; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
## Preload our cache |
92
|
0
|
|
|
|
|
0
|
foreach (keys %tcache) { |
93
|
0
|
|
|
|
|
0
|
$self->{C}->{$_} = $tcache{$_} |
94
|
|
|
|
|
|
|
} |
95
|
0
|
|
|
|
|
0
|
$self->{TiC} = \%tcache; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
4
|
push(@AllTies, $self); |
99
|
3
|
|
|
|
|
7
|
return $self; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub EXISTS { |
103
|
46
|
|
|
46
|
|
1723
|
my($self, $key) = @_; |
104
|
|
|
|
|
|
|
|
105
|
46
|
50
|
|
|
|
66
|
$DEBUG and print STDERR " >> $self->{INSTANCE} >> EXISTS: $key\n"; |
106
|
|
|
|
|
|
|
|
107
|
46
|
100
|
|
|
|
53
|
if (exists $self->{C}->{$key}) { |
108
|
24
|
|
|
|
|
28
|
my($t, $i);#, %t, %r); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
## Adjust the positions in the index cache |
111
|
|
|
|
|
|
|
## 1. Find the old entry in the array (and do the stat's) |
112
|
24
|
|
|
|
|
31
|
$i = _find($self->{I}, $self->{C}->{$key}->{t}, $key); |
113
|
24
|
50
|
|
|
|
37
|
if (!defined($i)) { |
114
|
0
|
|
|
|
|
0
|
print STDERR "Cache trashed (unable to find $key)\n"; |
115
|
0
|
|
|
|
|
0
|
DumpCache($self->{INSTANCE}); |
116
|
0
|
|
|
|
|
0
|
ShowStats(); |
117
|
0
|
|
|
|
|
0
|
die "Aborting..."; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
## 2. Remove the old entry from the array |
121
|
24
|
|
|
|
|
14
|
$t = splice(@{$self->{I}}, $i, 1); |
|
24
|
|
|
|
|
31
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
## 3. Update the timestamp of the new array entry, as |
124
|
|
|
|
|
|
|
## well as that in the cache |
125
|
24
|
|
|
|
|
22
|
$self->{C}->{$key}->{t} = $t->{t} = time; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
## 4. Store the updated entry back into the array as the MRU |
128
|
24
|
|
|
|
|
17
|
unshift(@{$self->{I}}, $t); |
|
24
|
|
|
|
|
20
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
## 5. Adjust stats |
131
|
24
|
100
|
|
|
|
34
|
if (defined($self->{T})) { |
132
|
21
|
50
|
|
|
|
27
|
$self->{T}->[$i]++ if defined($self->{T}); |
133
|
21
|
|
|
|
|
17
|
$self->{ch}++; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
24
|
50
|
|
|
|
25
|
if ($DEBUG) { |
137
|
0
|
|
|
|
|
0
|
print STDERR " Cache hit at $i"; |
138
|
0
|
0
|
|
|
|
0
|
print STDERR " ($self->{ch})" if defined($self->{T}); |
139
|
0
|
|
|
|
|
0
|
print STDERR ".\n"; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
24
|
|
|
|
|
35
|
return 1; |
143
|
|
|
|
|
|
|
} else { |
144
|
22
|
100
|
|
|
|
29
|
if (exists($self->{TUNECACHESIZE})) { |
145
|
16
|
|
|
|
|
9
|
$self->{cm}++; |
146
|
16
|
50
|
|
|
|
17
|
$DEBUG and print STDERR " Cache miss ($self->{cm}).\n"; |
147
|
|
|
|
|
|
|
## Ughhh. A linear search |
148
|
16
|
|
|
|
|
12
|
my($i, $j); |
149
|
16
|
|
|
|
|
15
|
for ($i = $j = $self->{CACHESIZE}; $i <= $#{$self->{T}}; $i++) { |
|
56
|
|
|
|
|
81
|
|
150
|
|
|
|
|
|
|
next unless defined($self->{TI}) |
151
|
|
|
|
|
|
|
&& defined($self->{TI}->[$i- $j]) |
152
|
|
|
|
|
|
|
&& defined($self->{TI}->[$i - $j]->{k}) |
153
|
45
|
100
|
66
|
|
|
207
|
&& $self->{TI}->[$i - $j]->{k} eq $key; |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
154
|
5
|
|
|
|
|
6
|
$self->{T}->[$i]++; |
155
|
5
|
|
|
|
|
4
|
$self->{th}++; |
156
|
5
|
50
|
|
|
|
6
|
$DEBUG and print STDERR " TestCache hit at $i. ($self->{th})\n"; |
157
|
5
|
|
|
|
|
3
|
splice(@{$self->{TI}}, $i - $j, 1); |
|
5
|
|
|
|
|
8
|
|
158
|
5
|
|
|
|
|
11
|
return 0; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} else { |
161
|
6
|
50
|
|
|
|
8
|
$DEBUG and print STDERR " Cache miss.\n"; |
162
|
|
|
|
|
|
|
} |
163
|
17
|
|
|
|
|
23
|
return 0; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub STORE { |
168
|
22
|
|
|
22
|
|
190
|
my ($self, $key, $value) = @_; |
169
|
22
|
50
|
|
|
|
26
|
$DEBUG and print STDERR " >> $self->{INSTANCE} >> STORE: $key $value\n"; |
170
|
|
|
|
|
|
|
|
171
|
22
|
|
|
|
|
17
|
my(%r, %t); |
172
|
22
|
|
|
|
|
35
|
$t{t} = $r{t} = time; |
173
|
22
|
|
|
|
|
22
|
$r{v} = $value; |
174
|
22
|
|
|
|
|
21
|
$t{k} = $key; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Store the value into the hash |
177
|
22
|
|
|
|
|
30
|
$self->{C}->{$key} = \%r; |
178
|
|
|
|
|
|
|
## As well as the tied cache, if it exists |
179
|
22
|
50
|
|
|
|
30
|
$self->{TC}->{$key} = $value if defined($self->{TC}); |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# By definition, this item is the MRU, so add it to the beginning |
182
|
|
|
|
|
|
|
# of the LRU queue. Since this is a STORE, we know it doesn't already |
183
|
|
|
|
|
|
|
# exist. |
184
|
22
|
|
|
|
|
15
|
unshift(@{$self->{I}}, \%t); |
|
22
|
|
|
|
|
27
|
|
185
|
|
|
|
|
|
|
## Update the tied cache |
186
|
22
|
50
|
|
|
|
26
|
$self->{TC}->{$key} = $value if defined($self->{TC}); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
## Do we have too many entries? |
189
|
22
|
|
|
|
|
16
|
while (scalar(@{$self->{I}}) > $self->{CACHESIZE}) { |
|
35
|
|
|
|
|
49
|
|
190
|
|
|
|
|
|
|
## Chop off whatever is at the end |
191
|
|
|
|
|
|
|
## Get the key |
192
|
13
|
|
|
|
|
9
|
$key = pop(@{$self->{I}}); |
|
13
|
|
|
|
|
13
|
|
193
|
13
|
|
|
|
|
21
|
delete($self->{C}->{$key->{k}}); |
194
|
13
|
50
|
|
|
|
17
|
delete($self->{TC}->{$key->{k}}) if defined($self->{TC}); |
195
|
|
|
|
|
|
|
## Throw it to the beginning of the test cache |
196
|
13
|
100
|
|
|
|
16
|
unshift(@{$self->{TI}}, $key) if defined($self->{T}); |
|
11
|
|
|
|
|
19
|
|
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
## Now, what about the Tuning Index |
200
|
22
|
100
|
|
|
|
26
|
if (defined($self->{T})) { |
201
|
16
|
100
|
|
|
|
11
|
if (scalar(@{$self->{TI}}) > $self->{TUNECACHESIZE} - $self->{CACHESIZE}) { |
|
16
|
|
|
|
|
22
|
|
202
|
1
|
|
|
|
|
2
|
$#{$self->{TI}} = $self->{TUNECACHESIZE} - $self->{CACHESIZE} - 1; |
|
1
|
|
|
|
|
3
|
|
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
22
|
|
|
|
|
44
|
$value; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub FETCH { |
210
|
24
|
|
|
24
|
|
85
|
my($self, $key) = @_; |
211
|
|
|
|
|
|
|
|
212
|
24
|
50
|
|
|
|
26
|
$DEBUG and print STDERR " >> $self->{INSTANCE} >> FETCH: $key\n"; |
213
|
|
|
|
|
|
|
|
214
|
24
|
|
|
|
|
78
|
return $self->{C}->{$key}->{v}; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub _find ( $$$ ) { |
218
|
24
|
|
|
24
|
|
21
|
my($Aref, $time, $key) = @_; |
219
|
24
|
|
|
|
|
14
|
my($t, $b, $n, $l); |
220
|
|
|
|
|
|
|
|
221
|
24
|
|
|
|
|
13
|
$t = $#{$Aref}; |
|
24
|
|
|
|
|
26
|
|
222
|
24
|
|
|
|
|
18
|
$n = $b = 0; |
223
|
24
|
|
|
|
|
15
|
$l = -2; |
224
|
|
|
|
|
|
|
|
225
|
24
|
|
|
|
|
39
|
while ($time != $Aref->[$n]->{t}) { |
226
|
0
|
0
|
|
|
|
0
|
if ($time < $Aref->[$n]->{t}) { |
227
|
0
|
|
|
|
|
0
|
$b = $n; |
228
|
|
|
|
|
|
|
} else { |
229
|
0
|
|
|
|
|
0
|
$t = $n; |
230
|
|
|
|
|
|
|
} |
231
|
0
|
0
|
|
|
|
0
|
if ($t <= $b) { |
232
|
|
|
|
|
|
|
## Trouble, we're out. |
233
|
0
|
0
|
|
|
|
0
|
if ($Aref->[$t]->{t} == $time) { |
|
|
0
|
|
|
|
|
|
234
|
0
|
|
|
|
|
0
|
$n = $t; |
235
|
|
|
|
|
|
|
} elsif ($Aref->[$b]->{t} == $time) { |
236
|
0
|
|
|
|
|
0
|
$n = $b; |
237
|
|
|
|
|
|
|
} else { |
238
|
|
|
|
|
|
|
## Really big trouble |
239
|
|
|
|
|
|
|
## Complain loudly |
240
|
0
|
|
|
|
|
0
|
print "Trouble\n"; |
241
|
0
|
|
|
|
|
0
|
return undef; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
} else { |
244
|
0
|
|
|
|
|
0
|
$n = $b + (($t - $b) >> 1); |
245
|
0
|
0
|
|
|
|
0
|
$n++ if $l == $n; |
246
|
0
|
|
|
|
|
0
|
$l = $n; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
## Drop down in the array until the time isn't the time |
250
|
24
|
|
33
|
|
|
35
|
while (($n > 0) && ($time == $Aref->[$n-1]->{t})) { |
251
|
0
|
|
|
|
|
0
|
$n--; |
252
|
|
|
|
|
|
|
} |
253
|
24
|
|
66
|
|
|
78
|
while (($time == $Aref->[$n]->{t}) && ($key ne $Aref->[$n]->{k})) { |
254
|
45
|
|
|
|
|
116
|
$n++; |
255
|
|
|
|
|
|
|
} |
256
|
24
|
50
|
|
|
|
32
|
if ($key ne $Aref->[$n]->{k}) { |
257
|
|
|
|
|
|
|
## More big trouble |
258
|
0
|
|
|
|
|
0
|
print "More trouble\n"; |
259
|
0
|
|
|
|
|
0
|
return undef; |
260
|
|
|
|
|
|
|
} |
261
|
24
|
|
|
|
|
23
|
return $n; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
END { |
265
|
1
|
50
|
|
1
|
|
66
|
print STDERR ShowStats() if $EndDebug; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
__END__ |