line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cache::Bounded; |
2
|
|
|
|
|
|
|
$Cache::Bounded::VERSION='1.08'; |
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
2480
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
330
|
|
5
|
2
|
|
|
2
|
|
43
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1486
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new { |
8
|
2
|
|
|
2
|
1
|
29
|
my $class = shift @_; |
9
|
2
|
|
|
|
|
6
|
my $self = {}; |
10
|
2
|
|
|
|
|
6
|
bless($self,$class); |
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
|
|
14
|
$self->{cache} = {}; |
13
|
2
|
|
|
|
|
6
|
$self->{count} = 0; |
14
|
2
|
|
|
|
|
8
|
$self->{interval} = 1000; |
15
|
2
|
|
|
|
|
5
|
$self->{size} = 500000; |
16
|
|
|
|
|
|
|
|
17
|
2
|
100
|
|
|
|
17
|
if ( UNIVERSAL::isa($_[0],'HASH') ) { |
18
|
1
|
50
|
|
|
|
6
|
$self->{interval} = $_[0]->{interval} if $_[0]->{interval} > 1; |
19
|
1
|
50
|
|
|
|
8
|
$self->{size} = $_[0]->{size} if $_[0]->{size} > 1; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
7
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub get { |
26
|
2500
|
|
|
2500
|
1
|
918351
|
my $self = shift @_; |
27
|
2500
|
|
|
|
|
4672
|
my $key = shift @_; |
28
|
2500
|
|
|
|
|
10446
|
return $self->{cache}->{$key}; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub purge { |
32
|
1
|
|
|
1
|
1
|
3
|
my $self = shift @_; |
33
|
1
|
|
|
|
|
3
|
$self->{count} = 0; |
34
|
1
|
|
|
|
|
3
|
$self->{cache} = {}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub set { |
38
|
28
|
|
|
28
|
1
|
921
|
my $self = shift @_; |
39
|
28
|
|
|
|
|
35
|
my $key = shift @_; |
40
|
28
|
|
|
|
|
29
|
my $data = shift @_; |
41
|
28
|
|
|
|
|
34
|
$self->{count}++; |
42
|
|
|
|
|
|
|
|
43
|
28
|
100
|
100
|
|
|
80
|
if ( $self->{count} > $self->{interval} && scalar(keys %{$self->{cache}}) > $self->{size}) { |
|
2
|
|
|
|
|
12
|
|
44
|
1
|
|
|
|
|
4
|
$self->purge(); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
28
|
|
|
|
|
75
|
$self->{cache}->{$key} = $data; |
48
|
28
|
|
|
|
|
50
|
return 1; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 NAME: |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Cache::Bounded - A size-aware in-memory cache optimized for speed. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 SYNOPSIS: |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Cache::Bounded is designed for caching items into memory in a very fast |
60
|
|
|
|
|
|
|
but rudimentarily size-aware fashion. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 DESCRIPTION: |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Most intelligent caches take either a size-aware or use-aware approach. |
65
|
|
|
|
|
|
|
They do so by either anlysing the size of all the elements in the cache or |
66
|
|
|
|
|
|
|
their frequency of usage before determining which elements to drop from |
67
|
|
|
|
|
|
|
the cache. Unfortunately, the processing overhead for this logic (usually |
68
|
|
|
|
|
|
|
applied on insert) will often slow these caches singnificantly when |
69
|
|
|
|
|
|
|
frequent insertions are needed. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
This module was designed address when this speed-penalty becomes a |
72
|
|
|
|
|
|
|
problem. Specifically, it is a rudimentarily size-aware cache that is |
73
|
|
|
|
|
|
|
optimized to be very fast. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
For its size analysis, this module merely checks the number of elements in |
76
|
|
|
|
|
|
|
the cache against a raw size limit. (The default limit is 500,000) |
77
|
|
|
|
|
|
|
Additionally, to aid speed, the "size" check doesn't occur on every |
78
|
|
|
|
|
|
|
insertion. Only after a count of a certain number of insertions (default |
79
|
|
|
|
|
|
|
1,000) is the size check performed. If the size limit has been exceeded, |
80
|
|
|
|
|
|
|
the entire cache is purged. (Since there is no usage analysis, there is no |
81
|
|
|
|
|
|
|
other logical depreciation that can be applied) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This produces a very fast in-memory cache that you can tune to approximate |
84
|
|
|
|
|
|
|
size based upon your data elements. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 USAGE: |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $cache = new Cache::Bounded; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$cache->set($key,$value); |
91
|
|
|
|
|
|
|
my $value = $cache->get($key); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 Methods |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head3 new($ref) |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $cache = new Cache::Bounded ({ interval=>1000 size=>500000 }); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Instances the object as is typical with an OO module. You may also pass a |
100
|
|
|
|
|
|
|
hashref with configurations to tune the cache. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Configurable values are: |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=over |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item interval |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
The number of inserts before the size of the cache is checked. Setting |
109
|
|
|
|
|
|
|
this to a lower number reduces the "sloppiness" of the size limit. |
110
|
|
|
|
|
|
|
However, it also slows cache inserts. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
The default of this value is 1,000. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item size |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The number of entries allowed in the cache. Once this is exceeded the |
117
|
|
|
|
|
|
|
cache will be purged at the next size check. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
The default of this value is 500,000. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=back |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head3 get($key) |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Returns the cached value associated with the given key. If no value has |
126
|
|
|
|
|
|
|
been cached for that key, the returned value is undefined. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head3 set($key,$value) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Caches the given value for the given key. The cache size is checked during |
131
|
|
|
|
|
|
|
the set method. If a purge occurs, the value is cached post-purge. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head3 purge() |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
This dumps the currently in-memory cache. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 KNOWN ISSUES: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head3 Memory Allocation |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Due to perl's methodology of allocating memory, you will not see memory |
142
|
|
|
|
|
|
|
freed back to general usage until perl exits after instancing this module. |
143
|
|
|
|
|
|
|
On each purge of the internal cache, the memory is retained by perl and |
144
|
|
|
|
|
|
|
reallocated internally as the cache grows again. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Consequently after the initial population and purge of the cache, the |
147
|
|
|
|
|
|
|
memory allocated should be of a relatively constant size. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head3 Scalar Values |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
In the name of speed, there is no checking to see if the data being stored |
152
|
|
|
|
|
|
|
is complex or not. Technically you should be able to store complex memory |
153
|
|
|
|
|
|
|
structures, though this module is not designed for it and the ability is |
154
|
|
|
|
|
|
|
not guarenteed. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Use scalar data. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 BUGS AND SOURCE |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Bug tracking for this module: https://rt.cpan.org/Dist/Display.html?Name=Cache-Bounded |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Source hosting: http://www.github.com/bennie/perl-Cache-Bounded |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 VERSION |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Cache::Bounded v1.08 (2014/03/10) |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
(c) 2004-2014, Phillip Pollard |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 LICENSE |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This source code is released under the "Perl Artistic License 2.0," the text of |
175
|
|
|
|
|
|
|
which is included in the LICENSE file of this distribution. It may also be |
176
|
|
|
|
|
|
|
reviewed here: http://opensource.org/licenses/artistic-license-2.0 |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 AUTHORISHIP |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Original derived from Cache::Sloppy v1.3 2004/03/02 |
181
|
|
|
|
|
|
|
With permission granted from Health Market Science, Inc. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |