File Coverage

blib/lib/Cache/Bounded.pm
Criterion Covered Total %
statement 33 33 100.0
branch 8 10 80.0
condition n/a
subroutine 6 6 100.0
pod 4 4 100.0
total 51 53 96.2


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