blib/lib/IPC/Cache.pm

filter: Statementstmt 98.7% 163 / 165 Branchbran 62.5% 30 / 48 Conditioncond 43.4% 10 / 23 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 100% 22 / 22 Podpod 100% 7 / 7 totaltotal 83.2% 233 / 280 CC 36 SCAR 13.6
CC
36
Cov
86%
new
7.6
get
6.1
set
5.1
CRAP 3.9
↑ Summary test/test_get.pl »
1 #!/usr/bin/perl -w
2  
3 package IPC::Cache;
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IPC::ShareLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Storable qw(freeze thaw dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 $VERSION = '0.02';
13  
14 2 my $sEXPIRES_NOW = 0;
15 2 my $sEXPIRES_NEVER = -1;
16 2 my $sSUCCESS = 1;
17 2 my $sTRUE = 1;
18 2 my $sFALSE = 0;
19  
20  
21 # IPC::ShareLite converts a four character text string to the shared memory key
22  
23 2 my $sDEFAULT_CACHE_KEY = "IPCC";
24  
25  
26 # if a namespace is not specified, use this as a default
27  
28 2 my $sDEFAULT_NAMESPACE = "_default";
29  
30  
31  
32 # create a new Cache object that can be used to persist
33 # data across processes
34  
35 sub new
36 4 ▶{
Subroutine new: called CC=7 SCAR=20.3
Pod documented
37 4 my ($proto, $options) = @_;
38 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
39 4 my $self = {};
40 4 bless ($self, $class);
41  
42  
43 # this instance will use the namespace specified or the default
44  
45 4 ▶ my $namespace = $options->{namespace} || $sDEFAULT_NAMESPACE;
Condition66%2/3
$options->{'namespace'} || $sDEFAULT_NAMESPACE
l!l&&r!l&&!r
220
MC/DC0%0/2
$options->{'namespace'} || $sDEFAULT_NAMESPACE
$options->{'namespace'} $sDEFAULT_NAMESPACE
Truth table66%2/3
A || B
A= $options->{'namespace'}B= $sDEFAULT_NAMESPACE
ABresult
1X1
011
000
46  
47 4 $self->{_namespace} = $namespace;
48  
49  
50 # remember the expiration delta to be used for all objects if specified
51  
52 4 ▶ $self->{_expires_in} = $options->{expires_in} || $sEXPIRES_NEVER;
Condition33%1/3
$options->{'expires_in'} || $sEXPIRES_NEVER
l!l&&r!l&&!r
040
MC/DC0%0/2
$options->{'expires_in'} || $sEXPIRES_NEVER
$options->{'expires_in'} $sEXPIRES_NEVER
Truth table33%1/3
A || B
A= $options->{'expires_in'}B= $sEXPIRES_NEVER
ABresult
1X1
011
000
53  
54 # create a new share associated with the cache key
55  
56 4 ▶ my $cache_key = $options->{cache_key} || $sDEFAULT_CACHE_KEY;
Condition33%1/3
$options->{'cache_key'} || $sDEFAULT_CACHE_KEY
l!l&&r!l&&!r
400
MC/DC0%0/2
$options->{'cache_key'} || $sDEFAULT_CACHE_KEY
$options->{'cache_key'} $sDEFAULT_CACHE_KEY
Truth table33%1/3
A || B
A= $options->{'cache_key'}B= $sDEFAULT_CACHE_KEY
ABresult
1X1
011
000
57  
58 4 ▶ my $share = new IPC::ShareLite( -key => $cache_key, -create => 1 ) or
Branch: 1 branch
BranchTrueFalseTotal
my $share = "IPC::ShareLite"->new("-key", $cache_key, "-create", 1) or croak("Couldn't create new IPC::ShareLite") 4 0 4
59 croak("Couldn't create new IPC::ShareLite");
60  
61 # store the share for this instance
62  
63 4 $self->{_share} = $share;
64  
65
66 # atomically initialize the segment as frozen data
67  
68 4 $self->_lock();
69  
70 4 my $frozen_data = $self->_get_frozen_data();
71  
72 4 ▶ if (not $frozen_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frozen_data) 1 3 4
73 1 my %data;
74 1 $frozen_data = freeze(\%data);
75 1 $self->_set_frozen_data($frozen_data);
76 }
77  
78 4 $self->_unlock();
79  
80  
81 4 return $self;
82 }
83  
84  
85 # store an object in the cache associated with the identifier
86  
87 sub set
88 3 ▶{
Subroutine set: called CC=5 SCAR=16.3
Pod documented
89 3 my ($self, $identifier, $object, $expires_in) = @_;
90  
91 3 ▶ $identifier or
Branch: 1 branch
BranchTrueFalseTotal
$identifier or croak("identifier required") 3 0 3
92 croak("identifier required");
93  
94 my $namespace = $self->{_namespace} or
95 3 ▶ croak("namespace required");
Branch: 1 branch
BranchTrueFalseTotal
my $namespace = $self->{"_namespace"} or croak("namespace required") 3 0 3
96  
97  
98 # expiration time is based on a delta from the current time
99 # if expires_in is defined, the object will expire in that number of seconds from now
100 # else if expires_in is undefined, it will expire based on the global _expires_in
101
102 3 my $expires_at;
103  
104 3 ▶ if (defined $expires_in) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $expires_in) { } 2 1 3
elsif ($self->{'_expires_in'} ne $sEXPIRES_NEVER) { } 0 1 1
105 2 $expires_at = time() + $expires_in;
106 } elsif ($self->{_expires_in} ne $sEXPIRES_NEVER) {
107 0 $expires_at = time() + $self->{_expires_in};
108 } else {
109 1 $expires_at = $sEXPIRES_NEVER;
110 }
111  
112 # atomically add the new object to the cache in this instance's namespace
113  
114 3 $self->_lock();
115  
116 3 my $frozen_data = $self->_get_frozen_data();
117  
118 3 my %data = %{ thaw($frozen_data) };
119  
120 3 $data{$namespace}->{$identifier} = { object => $object, expires_at => $expires_at };
121  
122 3 $frozen_data = freeze(\%data);
123  
124 3 $self->_set_frozen_data($frozen_data);
125  
126 3 $self->_unlock();
127  
128 3 return $sSUCCESS;
129 }
130  
131  
132  
133 # retrieve an object from the cache associated with the identifier
134  
135 sub get
136 6 ▶{
Subroutine get: called CC=6 SCAR=18.0
Pod documented
137 6 my ($self, $identifier) = @_;
138  
139 6 ▶ $identifier or
Branch: 1 branch
BranchTrueFalseTotal
$identifier or croak("identifier required") 6 0 6
140 croak("identifier required");
141
142 my $namespace = $self->{_namespace} or
143 6 ▶ croak("namespace required");
Branch: 1 branch
BranchTrueFalseTotal
my $namespace = $self->{"_namespace"} or croak("namespace required") 6 0 6
144  
145 # atomically (necessary for read-only?) check the cache for the specified object
146  
147 6 my $cloned_object = undef;
148  
149 6 $self->_lock();
150  
151 6 my $frozen_data = $self->_get_frozen_data();
152
153 6 my %data = %{ thaw($frozen_data) };
154  
155 6 ▶ if (exists $data{$namespace}->{$identifier}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$namespace}{$identifier}) 5 1 6
156  
157 5 my $object = $data{$namespace}->{$identifier}->{object};
158
159 5 my $expires_at = $data{$namespace}->{$identifier}->{expires_at};
160
161 # if the object has expired, remove it from the cache
162
163 5 ▶ if (_s_should_expire($expires_at)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_s_should_expire($expires_at)) { } 2 3 5
164 2 delete $data{$namespace}->{$identifier};
165 } else {
166 # if the object is a reference, clone it before returning it (may be unnecessary?)
167 3 ▶ $cloned_object = (ref $object) ? dclone($object) : $object;
Branch: 1 branch
BranchTrueFalseTotal
ref $object ? : 0 3 3
168 }
169 }
170
171 6 $self->_unlock();
172  
173 6 return $cloned_object;
174 }
175  
176  
177 # clear all objects in this instance's namespace
178  
179 sub clear
180 1 ▶{
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
181 1 my ($self) = @_;
182  
183 1 my $namespace = $self->{_namespace};
184  
185 # atomically iterate over all of the key in this instance's namespace and delete them
186  
187 1 $self->_lock();
188  
189 1 my $frozen_data = $self->_get_frozen_data();
190
191 1 my %data = %{ thaw($frozen_data) };
192  
193 1 foreach my $identifier (keys %{$data{$namespace}}) {
194 1 delete $data{$namespace}->{$identifier};
195 }
196  
197 1 $frozen_data = freeze(\%data);
198  
199 1 $self->_set_frozen_data($frozen_data);
200  
201 1 $self->_unlock();
202  
203 1 return $sSUCCESS;
204 }
205  
206  
207  
208 # iterate over all the objects in this instance's namespace and delete those that have expired
209  
210 sub purge
211 1 ▶{
Subroutine purge: called CC=1 SCAR=0.0
Pod documented
212 1 my ($self) = @_;
213  
214 1 my $namespace = $self->{_namespace};
215  
216 1 my $time = time();
217  
218 # atomically iterate over all of the keys in this instance's namespace and delete those that have expired
219  
220 1 $self->_lock();
221  
222 1 my $frozen_data = $self->_get_frozen_data();
223
224 1 my %data = %{ thaw($frozen_data) };
225  
226 1 my $namespace_ref = $data{$namespace};
227  
228 1 _s_purge_namespace($namespace_ref, $time);
229  
230 1 $frozen_data = freeze(\%data);
231
232 1 $self->_set_frozen_data($frozen_data);
233  
234 1 $self->_unlock();
235  
236 1 return $sSUCCESS;
237 }
238  
239  
240  
241 # purge expired objects from all namespaces associated with this cache key
242  
243 sub _purge_all
244 1 ▶{
Subroutine _purge_all: called CC=2 SCAR=6.9
245 1 my ($self) = @_;
246  
247 1 my $time = time();
248  
249 # atomically iterate over all of the keys in all of this instance's namespaces and delete those that have expired
250  
251 1 $self->_lock();
252  
253 1 my $frozen_data = $self->_get_frozen_data();
254  
255 1 my %data = %{ thaw($frozen_data) };
256
257 1 foreach my $namespace (keys %data) {
258  
259 1 my $namespace_ref = $data{$namespace};
260  
261 1 _s_purge_namespace($namespace_ref, $time);
262  
263 }
264  
265 1 $frozen_data = freeze(\%data);
266  
267 1 $self->_set_frozen_data($frozen_data);
268  
269 1 $self->_unlock();
270  
271 1 return $sSUCCESS;
272 }
273  
274  
275 # iterate over all the objects in the specified namespace and delete those that have expired
276  
277 sub _s_purge_namespace
278 2 ▶{
Subroutine _s_purge_namespace: called CC=3 SCAR=11.0
279 2 my ($namespace_ref, $time) = @_;
280  
281 2 foreach my $identifier (keys %{$namespace_ref}) {
282
283 1 my $expires_at = $namespace_ref->{$identifier}->{expires_at};
284  
285 # if the object has expired, remove it from the cache
286  
287 1 ▶ if (_s_should_expire($expires_at, $time)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_s_should_expire($expires_at, $time)) 1 0 1
288 1 delete $namespace_ref->{$identifier};
289 }
290 }
291
292 2 return $sSUCCESS;
293 }
294  
295  
296 # determine whether an object should expire
297  
298 sub _s_should_expire
299 6 ▶{
Subroutine _s_should_expire: called CC=5 SCAR=16.2
300 6 my ($expires_at, $time) = @_;
301  
302 # time is optional
303  
304 6 ▶ $time = $time || time();
Condition100%2/2
$time || time
l!l
15
MC/DC100%1/1
$time || time
$time
Truth table100%2/2
A
A= $time
Aresult
11
00
305  
306 6 ▶ if ($expires_at == $sEXPIRES_NOW) {
Branch: 3 branches
BranchTrueFalseTotal
if ($expires_at == $sEXPIRES_NOW) { } 0 6 6
elsif ($expires_at == $sEXPIRES_NEVER) { } 2 4 6
elsif ($time >= $expires_at) { } 3 1 4
307 0 return $sTRUE;
308 } elsif ($expires_at == $sEXPIRES_NEVER) {
309 2 return $sFALSE;
310 } elsif ($time >= $expires_at) {
311 3 return $sTRUE;
312 } else {
313 1 return $sFALSE;
314 }
315 }
316  
317  
318 # use this cache instance's frozen data to get an estimate of the memory consumption
319  
320 sub _size
321 1 ▶{
Subroutine _size: called CC=1 SCAR=0.0
322 1 my ($self) = @_;
323  
324 1 $self->_lock();
325  
326 1 my $frozen_data = $self->_get_frozen_data();
327  
328 1 my $size = length $frozen_data;
329  
330 1 $self->_unlock();
331  
332 1 return $size;
333 }
334  
335 # set the frozen data in the share
336  
337 sub _set_frozen_data
338 7 ▶{
Subroutine _set_frozen_data: called CC=2 SCAR=7.0
339 7 my ($self, $frozen_data) = @_;
340  
341 my $share = $self->{_share} or
342 7 ▶ croak("Couldn't get share");
Branch: 1 branch
BranchTrueFalseTotal
my $share = $self->{"_share"} or croak("Couldn't get share") 7 0 7
343  
344 7 $share->store($frozen_data);
345  
346 7 return $sSUCCESS;
347 }
348  
349  
350 # get the frozen data from the share
351  
352 sub _get_frozen_data
353 17 ▶{
Subroutine _get_frozen_data: called CC=2 SCAR=7.1
354 17 my ($self) = @_;
355  
356 my $share = $self->{_share} or
357 17 ▶ croak("Couldn't get share");
Branch: 1 branch
BranchTrueFalseTotal
my $share = $self->{"_share"} or croak("Couldn't get share") 17 0 17
358  
359 17 return $share->fetch();
360 }
361  
362  
363 # lock on the shared memory
364  
365 sub _lock
366 17 ▶{
Subroutine _lock: called CC=3 SCAR=11.4
367 17 my ($self) = @_;
368  
369 my $share = $self->{_share} or
370 17 ▶ croak("Couldn't get share");
Branch: 1 branch
BranchTrueFalseTotal
my $share = $self->{"_share"} or croak("Couldn't get share") 17 0 17
371  
372 17 ▶ $share->lock(LOCK_EX) or
Branch: 1 branch
BranchTrueFalseTotal
$share->lock(2) or croak("Couldn't lock") 17 0 17
373 croak("Couldn't lock");
374  
375 17 return $sSUCCESS;
376 }
377  
378  
379 # unlock on the shared memory
380  
381 sub _unlock
382 17 ▶{
Subroutine _unlock: called CC=3 SCAR=11.4
383 17 my ($self) = @_;
384  
385 my $share = $self->{_share} or
386 17 ▶ croak("Couldn't get share");
Branch: 1 branch
BranchTrueFalseTotal
my $share = $self->{"_share"} or croak("Couldn't get share") 17 0 17
387  
388 17 ▶ $share->unlock() or
Branch: 1 branch
BranchTrueFalseTotal
$share->unlock or croak("Couldn't unlock") 17 0 17
389 croak("Couldn't unlock");
390  
391 17 return $sSUCCESS;
392 }
393  
394  
395  
396 # clear all objects in all namespaces and release the shared memory
397  
398 sub CLEAR
399 1 ▶{
Subroutine CLEAR: called CC=3 SCAR=12.0
400 1 my ($cache_key) = @_;
401  
402 1 ▶ $cache_key = $cache_key || $sDEFAULT_CACHE_KEY;
Condition33%1/3
$cache_key || $sDEFAULT_CACHE_KEY
l!l&&r!l&&!r
100
MC/DC0%0/2
$cache_key || $sDEFAULT_CACHE_KEY
$cache_key $sDEFAULT_CACHE_KEY
Truth table33%1/3
A || B
A= $cache_keyB= $sDEFAULT_CACHE_KEY
ABresult
1X1
011
000
403  
404 1 ▶ my $tmp_share = new IPC::ShareLite( -key => $cache_key, -create => 1, -destroy => 1 ) or
Branch: 1 branch
BranchTrueFalseTotal
my $tmp_share = "IPC::ShareLite"->new("-key", $cache_key, "-create", 1, "-destroy", 1) or croak("Couldn't create new IPC::ShareLite") 1 0 1
405 croak("Couldn't create new IPC::ShareLite");
406  
407 1 return $sSUCCESS;
408 }
409  
410  
411  
412 # purge all objects in all namespaces that have expired
413  
414 sub PURGE
415 1 ▶{
Subroutine PURGE: called CC=3 SCAR=11.8
Pod documented
416 1 my ($cache_key) = @_;
417  
418 1 ▶ $cache_key = $cache_key || $sDEFAULT_CACHE_KEY;
Condition33%1/3
$cache_key || $sDEFAULT_CACHE_KEY
l!l&&r!l&&!r
100
MC/DC0%0/2
$cache_key || $sDEFAULT_CACHE_KEY
$cache_key $sDEFAULT_CACHE_KEY
Truth table33%1/3
A || B
A= $cache_keyB= $sDEFAULT_CACHE_KEY
ABresult
1X1
011
000
419  
420 # note that this will not destroy the shared memory segment when it finishes
421  
422 1 ▶ my $tmp_cache = new IPC::Cache( { cache_key => $cache_key } ) or
Branch: 1 branch
BranchTrueFalseTotal
my $tmp_cache = "IPC::Cache"->new({"cache_key", $cache_key}) or croak("Couldn't instantiate new cache") 1 0 1
423 croak("Couldn't instantiate new cache");
424  
425 1 $tmp_cache->_purge_all();
426  
427 1 return $sSUCCESS;
428 }
429  
430  
431 # get an estimate of the total memory consumption of the cache
432  
433 sub SIZE
434 1 ▶{
Subroutine SIZE: called CC=3 SCAR=12.0
Pod documented
435 1 my ($cache_key) = @_;
436  
437 1 ▶ $cache_key = $cache_key || $sDEFAULT_CACHE_KEY;
Condition33%1/3
$cache_key || $sDEFAULT_CACHE_KEY
l!l&&r!l&&!r
100
MC/DC0%0/2
$cache_key || $sDEFAULT_CACHE_KEY
$cache_key $sDEFAULT_CACHE_KEY
Truth table33%1/3
A || B
A= $cache_keyB= $sDEFAULT_CACHE_KEY
ABresult
1X1
011
000
438  
439 1 ▶ my $tmp_cache = new IPC::Cache( { cache_key => $cache_key } ) or
Branch: 1 branch
BranchTrueFalseTotal
my $tmp_cache = "IPC::Cache"->new({"cache_key", $cache_key}) or croak("Couldn't instantiate new cache") 1 0 1
440 croak("Couldn't instantiate new cache");
441  
442 1 return $tmp_cache->_size();
443 }
444  
445  
446 2 1;
447  
448  
449 __END__
↑ Summary test/test_get.pl »