blib/lib/Cache/Bounded.pm

filter: Statementstmt 100% 34 / 34 Branchbran 75.0% 6 / 8 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 4 / 4 totaltotal 96.4% 55 / 57 SCAR 17.9
CC
6
Cov
96%
new
4.0
set
3.0
BEGIN
1.0
CRAP 6.0
↑ Summary
1 package Cache::Bounded;
2 2 $Cache::Bounded::VERSION='1.08';
3  
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new {
8 2 ▶ my $class = shift @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
9 2 my $self = {};
10 2 bless($self,$class);
11  
12 2 $self->{cache} = {};
13 2 $self->{count} = 0;
14 2 $self->{interval} = 1000;
15 2 $self->{size} = 500000;
16  
17 2 ▶ if ( UNIVERSAL::isa($_[0],'HASH') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], "HASH")) 1 1 2
18 1 ▶ $self->{interval} = $_[0]->{interval} if $_[0]->{interval} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"interval"} > 1 1 0 1
19 1 ▶ $self->{size} = $_[0]->{size} if $_[0]->{size} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"size"} > 1 1 0 1
20 }
21  
22 2 return $self;
23 }
24  
25 sub get {
26 2500 ▶ my $self = shift @_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
27 2500 my $key = shift @_;
28 2500 return $self->{cache}->{$key};
29 }
30  
31 sub purge {
32 1 ▶ my $self = shift @_;
Subroutine purge: called CC=1 SCAR=0.0
Pod documented
33 1 $self->{count} = 0;
34 1 $self->{cache} = {};
35 }
36  
37 sub set {
38 28 ▶ my $self = shift @_;
Subroutine set: called CC=3 SCAR=11.0
Pod documented
39 28 my $key = shift @_;
40 28 my $data = shift @_;
41 28 $self->{count}++;
42  
43 28 ▶ if ( $self->{count} > $self->{interval} && scalar(keys %{$self->{cache}}) > $self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"count"} > $self->{"interval"} and scalar keys %{$$self{"cache"};} > $self->{"size"}) 1 27 28
Condition100%3/3
$self->{"count"} > $self->{"interval"} and scalar keys %{$$self{"cache"};} > $self->{"size"}
!ll&&!rl&&r
2611
MC/DC100%2/2
$self->{"count"} > $self->{"interval"} and scalar keys %{$$self{"cache"};} > $self->{"size"}
$self->{"count"} > $self->{"interval"} scalar keys %{$$self{"cache"};} > $self->{"size"}
Truth table100%3/3
A and B
A= $self->{"count"} > $self->{"interval"}B= scalar keys %{$$self{"cache"};} > $self->{"size"}
ABresult
0X0
100
111
44 1 $self->purge();
45 }
46  
47 28 $self->{cache}->{$key} = $data;
48 28 return 1;
49 }
50  
51 2 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 <bennie@cpan.org>
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
↑ Summary