CacheFactory.pm

filter: Statementstmt 62.2% 219 / 352 Branchbran 50.6% 75 / 148 Conditioncond 25.0% 8 / 32 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 63.8% 46 / 72 Podpod 100% 54 / 54 totaltotal 59.6% 404 / 677 CC 93 SCAR 27.1
CC
93
Cov
57%
set
41.0
set_policy
31.9
set_default_expires_in
30.0
CRAP 15.1
↑ Summary CacheFactory/Expiry/Size.pm »
1 ###############################################################################
2 # Purpose : Generic Cache Factory with various policy factories.
3 # Author : Sam Graham
4 # Created : 23 Jun 2008
5 # CVS : $Id: CacheFactory.pm,v 1.25 2010-02-16 12:25:40 illusori Exp $
6 ###############################################################################
7  
8 package Cache::CacheFactory;
9  
10 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 ▶use Cache::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use Cache::CacheFactory::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use Cache::CacheFactory::Expiry;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use Cache::CacheFactory::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 11 ▶use base qw/Cache::Cache/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 11 $Cache::CacheFactory::VERSION = '1.10';
24  
25 11 $Cache::CacheFactory::NO_MAX_SIZE = -1;
26  
27 11 @Cache::CacheFactory::EXPORT = qw();
28 11 @Cache::CacheFactory::EXPORT_OK = qw(
29 best_available_storage_policy
30 best_available_pruning_policy
31 best_available_validity_policy
32 $NO_MAX_SIZE
33 );
34 11 %Cache::CacheFactory::EXPORT_TAGS = (
35 best_available => [ qw(
36 best_available_storage_policy
37 best_available_pruning_policy
38 best_available_validity_policy
39 ) ],
40 );
41  
42  
43 sub new
44 12 ▶{
Subroutine new: called CC=19 SCAR=33.6
Pod documented
45 12 my $class = shift;
46 12 my ( $self, %options );
47  
48 0 ▶ %options = ref( $_[ 0 ] ) ? %{$_[ 0 ]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 12 12
49  
50 12 $self = { policies => {}, compat => {}, };
51 12 ▶ bless $self, ( ref( $class ) || $class );
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
52  
53 #
54 # Compat options with Cache::Cache subclasses.
55 12 ▶ $self->{ namespace } = $options{ namespace } || 'Default';
Condition100%2/2
$options{'namespace'} || 'Default'
l!l
57
MC/DC100%1/1
$options{'namespace'} || 'Default'
$options{'namespace'}
Truth table100%2/2
A
A= $options{'namespace'}
Aresult
11
00
56  
57 #
58 # Compat with Cache::Cache.
59 $self->{ compat }->{ default_expires_in } = $options{ default_expires_in }
60 12 ▶ if exists $options{ default_expires_in };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"default_expires_in"} 0 12 12
61  
62 #
63 # Cache-wide settings.
64 $self->set_positional_set( $options{ positional_set } )
65 12 ▶ if exists $options{ positional_set };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"positional_set"} 0 12 12
66  
67 # Control first-run eligibility for auto-purging.
68 $self->set_last_auto_purge( $options{ last_auto_purge } )
69 12 ▶ if defined( $options{ last_auto_purge } );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"last_auto_purge"} 0 12 12
70  
71 # Auto-purge intervals.
72 $self->set_auto_purge_interval( $options{ auto_purge_interval } )
73 12 ▶ if exists $options{ auto_purge_interval };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"auto_purge_interval"} 0 12 12
74 $self->set_auto_purge_on_set_interval(
75 $options{ auto_purge_on_set_interval } )
76 12 ▶ if exists $options{ auto_purge_on_set_interval };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"auto_purge_on_set_interval"} 0 12 12
77 $self->set_auto_purge_on_get_interval(
78 $options{ auto_purge_on_get_interval } )
79 12 ▶ if exists $options{ auto_purge_on_get_interval };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"auto_purge_on_get_interval"} 0 12 12
80  
81 # Auto-purge toggles.
82 $self->set_auto_purge_on_set( $options{ auto_purge_on_set } )
83 12 ▶ if exists $options{ auto_purge_on_set };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"auto_purge_on_set"} 0 12 12
84 $self->set_auto_purge_on_get( $options{ auto_purge_on_get } )
85 12 ▶ if exists $options{ auto_purge_on_get };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"auto_purge_on_get"} 0 12 12
86  
87 # Do we quietly (or silently) fail on missing policies?
88 $self->{ nonfatal_missing_policies } = 1
89 12 ▶ if $options{ nonfatal_missing_policies };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"nonfatal_missing_policies"} 0 12 12
90 $self->{ nonwarning_missing_policies } = 1
91 12 ▶ if $options{ nonwarning_missing_policies };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"nonwarning_missing_policies"} 0 12 12
92  
93 # Do we deeply clone our data when setting it?
94 $self->{ no_deep_clone } = 1
95 12 ▶ if $options{ no_deep_clone };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"no_deep_clone"} 1 11 12
96  
97 #
98 # Grab our policies from the options.
99 12 $self->set_storage_policies( $options{ storage } );
100 $self->set_pruning_policies( $options{ pruning } )
101 12 ▶ if $options{ pruning };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"pruning"} 4 8 12
102 $self->set_validity_policies( $options{ validity } )
103 12 ▶ if $options{ validity };
Branch: 1 branch
BranchTrueFalseTotal
if $options{"validity"} 2 10 12
104  
105 12 ▶ if( $#{$self->{ policies }->{ storage }->{ order }} == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"policies"}{"storage"}{"order"};} == -1) 0 12 12
106 {
107 # OK, we've got no storage policies, we only get this
108 # far if nonfatal_missing_policies has been set.
109 # Either way it's a fatal error for a cache, so we
110 # return an undef.
111 $self->warning( "No valid storage policies supplied" )
112 0 ▶ unless $self->{ nonwarning_missing_policies };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"nonwarning_missing_policies"} 0 0 0
113 0 return( undef );
114 }
115  
116 12 return( $self );
117 }
118  
119 sub new_cache_entry_object
120 81 ▶{
Subroutine new_cache_entry_object: called CC=1 SCAR=0.0
Pod documented
121 # my ( $self ) = @_;
122 81 return( Cache::CacheFactory::Object->new() );
123 }
124  
125 sub set
126 105 ▶{
Subroutine set: called CC=19 SCAR=37.1
Pod documented
127 105 my $self = shift;
128 105 my ( $param, $object, $key, $data, $mode );
129  
130 # Aiii, backwards-compat with Cache::Cache->set().
131 105 ▶ if( $self->{ compat }->{ positional_set } and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'compat'}{'positional_set'} and $self->{'compat'}{'positional_set'} ne 'auto' || $_[0] ne 'key') { } 0 105 105
Condition16%1/6
$self->{"compat"}{"positional_set"} and $self->{"compat"}{"positional_set"} ne "auto" || $_[0] ne "key"
!ll&&!rl&&r
10500
$self->{"compat"}{"positional_set"} ne "auto" or $_[0] ne "key"
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"compat"}{"positional_set"} and $self->{"compat"}{"positional_set"} ne "auto" || $_[0] ne "key"
$self->{"compat"}{"positional_set"} $self->{"compat"}{"positional_set"} ne "auto" $_[0] ne "key"
Truth table25%1/4
A and B or C
A= $self->{"compat"}{"positional_set"}B= $self->{"compat"}{"positional_set"} ne "auto"C= $_[0] ne "key"
ABCresult
0XX0
1000
11X1
1011
132 ( ( $self->{ compat }->{ positional_set } ne 'auto' ) or
133 ( $_[ 0 ] ne 'key' ) ) )
134 {
135 0 my ( $next_arg, $expires_in );
136  
137 0 $key = shift;
138 0 $data = shift;
139 0 $expires_in = shift;
140 0 $param = {};
141 0 ▶ if( defined( $next_arg = shift ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($next_arg = shift())) 0 0 0
142 {
143 # Hackery to support mode from add()/replace().
144 0 ▶ if( $next_arg eq 'mode' )
Branch: 1 branch
BranchTrueFalseTotal
if ($next_arg eq 'mode') { } 0 0 0
145 {
146 0 $mode = shift;
147 }
148 else
149 {
150 0 $param->{ expires_in } = $expires_in;
151 # TODO: warn if expires set and not time policy?
152 }
153 }
154 0 ▶ $mode = shift if defined( $next_arg = shift ) and $next_arg eq 'mode';
Branch: 1 branch
BranchTrueFalseTotal
if defined($next_arg = shift()) and $next_arg eq "mode" 0 0 0
Condition0%0/3
defined($next_arg = shift()) and $next_arg eq "mode"
!ll&&!rl&&r
000
MC/DC0%0/2
defined($next_arg = shift()) and $next_arg eq "mode"
defined($next_arg = shift()) $next_arg eq "mode"
Truth table0%0/3
A and B
A= defined($next_arg = shift())B= $next_arg eq "mode"
ABresult
0X0
100
111
155 }
156 else
157 {
158 0 ▶ $param = ref( $_[ 0 ] ) ? { %{$_[ 0 ]} } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 105 105
159 105 ▶ if( exists( $param->{ key } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{'key'}) { } 105 0 105
160 {
161 105 $key = $param->{ key };
162 105 delete $param->{ key };
163 }
164 else
165 {
166 0 warn "No key supplied to ${self}->set(), are you calling it " .
167 "with compat-style positional parameters but haven't set " .
168 "the positional_set option?";
169 0 return;
170 }
171 105 ▶ if( exists( $param->{ data } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{'data'}) { } 105 0 105
172 {
173 105 $data = $param->{ data };
174 105 delete $param->{ data };
175 }
176 else
177 {
178 0 warn "No data supplied to ${self}->set(), are you calling it " .
179 "with compat-style positional parameters but haven't set " .
180 "the positional_set option?";
181 0 return;
182 }
183 105 ▶ if( exists( $param->{ mode } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"mode"}) 48 57 105
184 {
185 48 $mode = $param->{ mode };
186 48 delete $param->{ mode };
187 }
188 }
189  
190 105 ▶ if( $mode )
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 48 57 105
191 {
192 48 ▶ if( $self->exists( $key ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists($key)) { } 18 30 48
193 {
194 18 ▶ return if $mode eq 'add';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "add" 9 9 18
195 }
196 else
197 {
198 30 ▶ return if $mode eq 'replace';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "replace" 15 15 30
199 }
200 }
201  
202 81 ▶ $param->{ created_at } = time() unless $param->{ created_at };
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"created_at"} 81 0 81
203 81 ▶ $param->{ no_deep_clone } = 1 if $self->{ no_deep_clone };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_deep_clone"} 4 77 81
204  
205 # Create Cache::CacheFactory::Object instance.
206 81 $object = $self->new_cache_entry_object();
207  
208 # Initialize it from the param.
209 81 $object->initialize( $key, $data, $param );
210  
211 81 $self->foreach_driver( 'validity', 'set_object_validity',
212 $key, $object, $param );
213 81 $self->foreach_driver( 'pruning', 'set_object_pruning',
214 $key, $object, $param );
215 81 ▶ if( $param->{ no_deep_clone } )
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'no_deep_clone'}) { } 4 77 81
216 {
217 # Since most Cache::Cache's do their own deep cloning
218 # we try a bit of a hack to try to bypass that.
219 $self->foreach_policy( 'storage',
220 sub
221 4 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.3
222 4 my ( $self, $policy, $storage ) = @_;
223  
224 # Only try this hack on things that subclass behaviour
225 # we understand.
226 4 ▶ if( $storage->isa( 'Cache::BaseCache' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($storage->isa("Cache::BaseCache")) 4 0 4
227 {
228 4 my ( $backend );
229  
230 4 ▶ if( $backend = $storage->_get_backend() )
Branch: 1 branch
BranchTrueFalseTotal
if ($backend = $storage->_get_backend) 4 0 4
231 {
232 4 $object->set_size( undef );
233 4 $object->set_key( undef );
234  
235 4 $backend->store( $storage->get_namespace(),
236 $key, $object );
237 4 return;
238 }
239 }
240  
241 # Ok, we couldn't figure out how to do our dirty hack...
242 0 $storage->set_object( $key, $object, $param );
243 4 } );
244 }
245 else
246 {
247 77 $self->foreach_driver( 'storage', 'set_object',
248 $key, $object, $param );
249 }
250  
251 81 $self->auto_purge( 'set' );
252 }
253  
254 sub get
255 149 ▶{
Subroutine get: called CC=8 SCAR=20.8
Pod documented
256 149 my ( $self, $key ) = @_;
257 149 my ( $object );
258  
259 149 my $storage_policies = $self->{ policies }->{ storage };
260 149 my $validity_policies = $self->{ policies }->{ validity };
261 149 foreach my $storage_policy ( @{$storage_policies->{ order }} )
262 {
263 149 my $storage = $storage_policies->{ drivers }->{ $storage_policy };
264 149 ▶ next unless defined( $object = $storage->get_object( $key ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined($object = $storage->get_object($key)) 67 82 149
265  
266 82 foreach my $validity_policy ( @{$validity_policies->{ order }} )
267 {
268 11 ▶ next if $validity_policies->{ drivers }->{ $validity_policy }->is_valid( $self, $storage, $object );
Branch: 1 branch
BranchTrueFalseTotal
if $validity_policies->{"drivers"}{$validity_policy}->is_valid($self, $storage, $object) 7 4 11
269 # TODO: should remove from this storage. optionally?
270 4 undef $object;
271 4 last;
272 }
273 82 ▶ last if defined $object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $object 78 4 82
274 }
275  
276 # Check of auto_purge_on_get isn't strictly neccessary but
277 # it saves the cost of a method call in the failure case.
278 149 ▶ $self->auto_purge( 'get' ) if $self->{ auto_purge_on_get };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_purge_on_get"} 0 149 149
279  
280 149 ▶ return( $object->get_data() ) if defined $object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $object 78 71 149
281 71 return( undef );
282 }
283  
284 sub get_object
285 0 ▶{
Subroutine get_object: not called CC=1 SCAR=6.9
Pod documented
286 0 my ( $self, $key ) = @_;
287 0 my ( $object );
288  
289 $self->foreach_policy( 'storage',
290 sub
291 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
292 0 my ( $self, $policy, $storage ) = @_;
293  
294 0 $object = $storage->get_object( $key );
295 0 ▶ $self->last() if defined $object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $object 0 0 0
296 0 } );
297  
298 0 return( $object );
299 }
300  
301 sub set_object
302 0 ▶{
Subroutine set_object: not called CC=3 SCAR=24.8
Pod documented
303 0 my ( $self, $key, $object ) = @_;
304  
305 # Backwards compat with Cache::Object objects.
306 0 ▶ unless( $object->isa( 'Cache::CacheFactory::Object' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($object->isa("Cache::CacheFactory::Object")) 0 0 0
307 {
308 0 my ( $param );
309  
310 0 $param = {};
311 0 ▶ $param->{ no_deep_clone } = 1 if $self->{ no_deep_clone };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_deep_clone"} 0 0 0
312 0 $object = Cache::CacheFactory::Object->new_from_old( $object, $param );
313 # TODO: compat with expires_at
314 }
315  
316 0 $self->foreach_driver( 'storage', 'set_object', $key, $object );
317 }
318  
319 sub remove
320 42 ▶{
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
321 42 my ( $self, $key ) = @_;
322  
323 42 $self->foreach_driver( 'storage', 'remove', $key );
324 }
325  
326 #
327 # CacheFactory extensions.
328 sub exists
329 60 ▶{
Subroutine exists: called CC=2 SCAR=7.0
Pod documented
330 60 my ( $self, $key ) = @_;
331 60 my ( $exists );
332  
333 $self->foreach_policy( 'storage',
334 sub
335 60 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.3
336 60 my ( $self, $policy, $storage ) = @_;
337  
338 # If they've implemented an exists method, use it,
339 # otherwise just do it the slow way.
340 60 ▶ if( $storage->can( 'exists' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($storage->can('exists')) { } 0 60 60
341 {
342 0 $exists = $storage->exists( $key );
343 }
344 else
345 {
346 60 $exists = defined( $storage->get_object( $key ) );
347 }
348  
349 60 ▶ return $self->last() if $exists;
Branch: 1 branch
BranchTrueFalseTotal
if $exists 27 33 60
350 60 } );
351  
352 60 ▶ return( $exists ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$exists ? : 27 33 60
353 }
354  
355  
356 #
357 # These following provide Cache::Memcached style interface.
358 # get_multi(), incr() and decr() cannot be "properly" implemented
359 # to use underlying functions because our object wrapper prevents
360 # the operations being single calls to the storage policy's
361 # implementation (if they have one), this then directly negates
362 # the purpose of these methods existing in the first place.
363 sub delete
364 12 ▶{
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
365 12 my ( $self, $key ) = @_;
366  
367 12 $self->remove( $key );
368 }
369  
370 sub add
371 24 ▶{
Subroutine add: called CC=1 SCAR=0.0
Pod documented
372 24 my $self = shift;
373  
374 24 $self->set( @_, mode => 'add' );
375 }
376  
377 sub replace
378 24 ▶{
Subroutine replace: called CC=1 SCAR=0.0
Pod documented
379 24 my $self = shift;
380  
381 24 $self->set( @_, mode => 'replace' );
382 }
383  
384 #
385 #sub get_multi
386 #{
387 # my ( $self, @keys ) = @_;
388 #}
389  
390 #sub incr
391 #{
392 # my ( $self, $key, $value ) = @_;
393 #}
394  
395 #sub decr
396 #{
397 # my ( $self, $key, $value ) = @_;
398 #}
399  
400 sub Clear
401 0 ▶{
Subroutine Clear: not called CC=1 SCAR=6.9
Pod documented
402 0 my ( $self, @args ) = @_;
403  
404 0 $self->foreach_driver( 'storage', 'Clear', @args );
405 }
406  
407 sub clear
408 10 ▶{
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
409 10 my ( $self, @args ) = @_;
410  
411 10 $self->foreach_driver( 'storage', 'clear', @args );
412 }
413  
414 sub Purge
415 0 ▶{
Subroutine Purge: not called CC=1 SCAR=6.9
Pod documented
416 0 my ( $self, @args ) = @_;
417  
418 0 $self->purge( @args );
419 }
420  
421 sub purge
422 10 ▶{
Subroutine purge: called CC=1 SCAR=0.0
Pod documented
423 10 my ( $self, @args ) = @_;
424  
425 10 $self->foreach_driver( 'pruning', 'purge', $self, @args );
426 }
427  
428 sub auto_purge
429 81 ▶{
Subroutine auto_purge: called CC=3 SCAR=18.1
Pod documented
430 81 my ( $self, $set_or_get ) = @_;
431  
432 81 ▶ return unless $self->{ "auto_purge_on_${set_or_get}" };
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"auto_purge_on_$set_or_get"} 81 0 81
433  
434 return if $self->{ last_auto_purge } >=
435 0 ▶ time() - $self->{ "auto_purge_${set_or_get}_interval" };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_auto_purge"} >= time - $$self{"auto_purge_${set_or_get}_interval"} 0 0 0
436  
437 # Set timestamp before purge in case we bomb out.
438 # Ideally we should do some manner of locking to prevent
439 # concurrent purges.
440 # Maybe that's the application's business instead.
441 0 $self->{ last_auto_purge } = time();
442  
443 0 $self->purge();
444  
445 # Update timestamp after purge so we don't spinlock if the purge
446 # takes longer than the interval.
447 0 $self->{ last_auto_purge } = time();
448 }
449  
450 sub Size
451 4 ▶{
Subroutine Size: called CC=1 SCAR=0.0
Pod documented
452 4 my ( $self, @args ) = @_;
453 4 my ( $size );
454  
455 4 $size = 0;
456 $self->foreach_policy( 'storage',
457 sub
458 4 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
459 4 my ( $self, $policy, $driver ) = @_;
460  
461 # Cache::FastMemoryCache 0.01 dies on Size(), workaround.
462 4 ▶ return if $driver->isa( 'Cache::FastMemoryCache' );
Branch: 1 branch
BranchTrueFalseTotal
if $driver->isa("Cache::FastMemoryCache") 1 3 4
463 3 $size += $driver->Size( @args );
464 4 } );
465  
466 4 return( $size );
467 }
468  
469 sub size
470 4 ▶{
Subroutine size: called CC=1 SCAR=0.0
Pod documented
471 4 my ( $self ) = @_;
472 4 my ( $size );
473  
474 4 $size = 0;
475 $self->foreach_policy( 'storage',
476 sub
477 4 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
478 4 my ( $self, $policy, $driver ) = @_;
479  
480 4 $size += $driver->size();
481 4 } );
482  
483 4 return( $size );
484 }
485  
486 sub get_namespaces
487 4 ▶{
Subroutine get_namespaces: called CC=1 SCAR=0.0
Pod documented
488 4 my ( $self ) = @_;
489 4 my ( %namespaces );
490  
491 4 %namespaces = ();
492 $self->foreach_policy( 'storage',
493 sub
494 4 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
495 4 my ( $self, $policy, $driver ) = @_;
496  
497 # Cache::NullCache->get_namespaces() dies, workaround it.
498 4 ▶ return $self->last() if $driver->isa( 'Cache::NullCache' );
Branch: 1 branch
BranchTrueFalseTotal
if $driver->isa("Cache::NullCache") 1 3 4
499 3 foreach my $namespace ( $driver->get_namespaces() )
500 {
501 3 $namespaces{ $namespace }++;
502 }
503 4 } );
504  
505 4 return( keys( %namespaces ) );
506 }
507  
508 sub get_keys
509 8 ▶{
Subroutine get_keys: called CC=1 SCAR=0.0
Pod documented
510 8 my ( $self ) = @_;
511 8 my ( %keys );
512  
513 8 %keys = ();
514 $self->foreach_policy( 'storage',
515 sub
516 8 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
517 8 my ( $self, $policy, $driver ) = @_;
518  
519 8 foreach my $key ( $driver->get_keys() )
520 {
521 18 $keys{ $key }++;
522 }
523 8 } );
524  
525 8 return( keys( %keys ) );
526 }
527  
528 sub get_identifiers
529 4 ▶{
Subroutine get_identifiers: called CC=1 SCAR=0.0
Pod documented
530 4 my ( $self ) = @_;
531  
532 4 return( $self->get_keys() );
533 }
534  
535  
536  
537 sub set_positional_set
538 0 ▶{
Subroutine set_positional_set: not called CC=1 SCAR=6.9
Pod documented
539 0 my ( $self, $positional_set ) = @_;
540  
541 0 $self->{ compat }->{ positional_set } = $positional_set;
542 }
543  
544 sub get_positional_set
545 0 ▶{
Subroutine get_positional_set: not called CC=1 SCAR=6.9
Pod documented
546 0 my ( $self ) = @_;
547  
548 0 return( $self->{ compat }->{ positional_set } );
549 }
550  
551 sub set_default_expires_in
552 0 ▶{
Subroutine set_default_expires_in: not called CC=5 SCAR=34.0
Pod documented
553 0 my ( $self, $default_expires_in ) = @_;
554 0 my ( $time_pruning, $time_validity );
555  
556 0 $time_pruning = $self->get_policy_driver( 'pruning', 'time' );
557 0 $time_validity = $self->get_policy_driver( 'validity', 'time' );
558  
559 0 ▶ unless( $time_pruning or $time_validity )
Branch: 1 branch
BranchTrueFalseTotal
unless ($time_pruning or $time_validity) 0 0 0
Condition0%0/3
$time_pruning or $time_validity
l!l&&r!l&&!r
000
MC/DC0%0/2
$time_pruning or $time_validity
$time_pruning $time_validity
Truth table0%0/3
A or B
A= $time_pruningB= $time_validity
ABresult
1X1
011
000
560 {
561 0 carp "Cannot set_default_expires_in() when neither a pruning nor " .
562 "a validity policy of 'time' is set.";
563 0 return;
564 }
565  
566 0 ▶ $time_pruning->set_default_expires_in( $default_expires_in )
Branch: 1 branch
BranchTrueFalseTotal
if $time_pruning 0 0 0
567 if $time_pruning;
568 0 ▶ $time_validity->set_default_expires_in( $default_expires_in )
Branch: 1 branch
BranchTrueFalseTotal
if $time_validity 0 0 0
569 if $time_validity;
570 }
571  
572 sub get_default_expires_in
573 0 ▶{
Subroutine get_default_expires_in: not called CC=5 SCAR=34.0
Pod documented
574 0 my ( $self ) = @_;
575 0 my ( $time_pruning, $time_validity );
576  
577 0 $time_pruning = $self->get_policy_driver( 'pruning', 'time' );
578 0 $time_validity = $self->get_policy_driver( 'validity', 'time' );
579  
580 0 ▶ unless( $time_pruning or $time_validity )
Branch: 1 branch
BranchTrueFalseTotal
unless ($time_pruning or $time_validity) 0 0 0
Condition0%0/3
$time_pruning or $time_validity
l!l&&r!l&&!r
000
MC/DC0%0/2
$time_pruning or $time_validity
$time_pruning $time_validity
Truth table0%0/3
A or B
A= $time_pruningB= $time_validity
ABresult
1X1
011
000
581 {
582 0 carp "Cannot get_default_expires_in() when neither a pruning nor " .
583 "a validity policy of 'time' is set.";
584 0 return( undef );
585 }
586  
587 # If they have both set, we go with the validity one since that's
588 # generally the one that has more immediate effect.
589 # If they're setting it via default_expires_in then both should
590 # be the same anyway...
591 0 ▶ return( $time_validity->get_default_expires_in() ) if $time_validity;
Branch: 1 branch
BranchTrueFalseTotal
if $time_validity 0 0 0
592 0 ▶ return( $time_pruning->get_default_expires_in() ) if $time_pruning;
Branch: 1 branch
BranchTrueFalseTotal
if $time_pruning 0 0 0
593 }
594  
595 sub limit_size
596 0 ▶{
Subroutine limit_size: not called CC=2 SCAR=17.9
Pod documented
597 0 my ( $self, $size ) = @_;
598 0 my ( $size_policy );
599  
600 0 $size_policy = $self->get_policy_driver( 'pruning', 'size' );
601  
602 0 ▶ unless( $size_policy )
Branch: 1 branch
BranchTrueFalseTotal
unless ($size_policy) 0 0 0
603 {
604 0 carp "Cannot limit_size() when no 'size' pruning policy is set.";
605 0 return;
606 }
607  
608 0 $size_policy->limit_size( $self, $size );
609 }
610  
611 sub set_last_auto_purge
612 0 ▶{
Subroutine set_last_auto_purge: not called CC=2 SCAR=17.9
Pod documented
613 0 my ( $self, $last_auto_purge ) = @_;
614  
615 $self->{ last_auto_purge } =
616 0 ▶ ( $last_auto_purge eq 'now' ) ? time() : $last_auto_purge;
Branch: 1 branch
BranchTrueFalseTotal
$last_auto_purge eq 'now' ? : 0 0 0
617 }
618  
619 sub get_last_auto_purge
620 0 ▶{
Subroutine get_last_auto_purge: not called CC=1 SCAR=6.9
Pod documented
621 0 my ( $self ) = @_;
622  
623 0 return( $self->{ last_auto_purge } );
624 }
625  
626 sub set_auto_purge_on_set
627 0 ▶{
Subroutine set_auto_purge_on_set: not called CC=1 SCAR=6.9
Pod documented
628 0 my ( $self, $auto_purge_on_set ) = @_;
629  
630 0 $self->{ auto_purge_on_set } = $auto_purge_on_set;
631 }
632  
633 sub get_auto_purge_on_set
634 0 ▶{
Subroutine get_auto_purge_on_set: not called CC=1 SCAR=6.9
Pod documented
635 0 my ( $self ) = @_;
636  
637 0 return( $self->{ auto_purge_on_set } );
638 }
639  
640 sub set_auto_purge_on_get
641 0 ▶{
Subroutine set_auto_purge_on_get: not called CC=1 SCAR=6.9
Pod documented
642 0 my ( $self, $auto_purge_on_get ) = @_;
643  
644 0 $self->{ auto_purge_on_get } = $auto_purge_on_get;
645 }
646  
647 sub get_auto_purge_on_get
648 0 ▶{
Subroutine get_auto_purge_on_get: not called CC=1 SCAR=6.9
Pod documented
649 0 my ( $self ) = @_;
650  
651 0 return( $self->{ auto_purge_on_get } );
652 }
653  
654 sub set_auto_purge_interval
655 0 ▶{
Subroutine set_auto_purge_interval: not called CC=1 SCAR=6.9
Pod documented
656 0 my ( $self, $auto_purge_interval ) = @_;
657  
658 0 $self->set_auto_purge_on_set_interval( $auto_purge_interval );
659 0 $self->set_auto_purge_on_get_interval( $auto_purge_interval );
660 }
661  
662 sub get_auto_purge_interval
663 0 ▶{
Subroutine get_auto_purge_interval: not called CC=2 SCAR=17.9
Pod documented
664 0 my ( $self ) = @_;
665  
666 0 ▶ return( $self->get_auto_purge_on_get_interval() ||
Condition0%0/3
$self->get_auto_purge_on_get_interval || $self->get_auto_purge_on_set_interval
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get_auto_purge_on_get_interval || $self->get_auto_purge_on_set_interval
$self->get_auto_purge_on_get_interval $self->get_auto_purge_on_set_interval
Truth table0%0/3
A || B
A= $self->get_auto_purge_on_get_intervalB= $self->get_auto_purge_on_set_interval
ABresult
1X1
011
000
667 $self->get_auto_purge_on_set_interval() );
668 }
669  
670 sub set_auto_purge_on_set_interval
671 0 ▶{
Subroutine set_auto_purge_on_set_interval: not called CC=1 SCAR=6.9
Pod documented
672 0 my ( $self, $auto_purge_interval ) = @_;
673  
674 0 $self->{ auto_purge_on_set_interval } = $auto_purge_interval;
675 }
676  
677 sub get_auto_purge_on_set_interval
678 0 ▶{
Subroutine get_auto_purge_on_set_interval: not called CC=1 SCAR=6.9
Pod documented
679 0 my ( $self ) = @_;
680  
681 0 return( $self->{ auto_purge_on_set_interval } );
682 }
683  
684 sub set_auto_purge_on_get_interval
685 0 ▶{
Subroutine set_auto_purge_on_get_interval: not called CC=1 SCAR=6.9
Pod documented
686 0 my ( $self, $auto_purge_interval ) = @_;
687  
688 0 $self->{ auto_purge_on_get_interval } = $auto_purge_interval;
689 }
690  
691 sub get_auto_purge_on_get_interval
692 0 ▶{
Subroutine get_auto_purge_on_get_interval: not called CC=1 SCAR=6.9
Pod documented
693 0 my ( $self ) = @_;
694  
695 0 return( $self->{ auto_purge_on_get_interval } );
696 }
697  
698 sub set_namespace
699 8 ▶{
Subroutine set_namespace: called CC=1 SCAR=0.0
Pod documented
700 8 my ( $self, $namespace ) = @_;
701  
702 8 $self->{ namespace } = $namespace;
703 8 $self->foreach_driver( 'storage', 'set_namespace', $namespace );
704 }
705  
706 sub get_namespace
707 2 ▶{
Subroutine get_namespace: called CC=1 SCAR=0.0
Pod documented
708 2 my ( $self ) = @_;
709  
710 2 return( $self->{ namespace } );
711 }
712  
713 # Coerce the policy arg into a hashref and ordered param list.
714 sub _normalize_policies
715 18 ▶{
Subroutine _normalize_policies: called CC=6 SCAR=30.0
716 18 my ( $self, $policies ) = @_;
717  
718 return( {
719 18 ▶ order => [ $policies ],
Branch: 1 branch
BranchTrueFalseTotal
unless ref $policies 15 3 18
720 param => { $policies => {} },
721 } )
722 unless ref( $policies );
723 return( {
724 3 ▶ order => [ keys( %{$policies} ) ],
Branch: 1 branch
BranchTrueFalseTotal
if ref $policies eq "HASH" 3 0 3
725 param => $policies,
726 } )
727 if ref( $policies ) eq 'HASH';
728 0 ▶ if( ref( $policies ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $policies eq "ARRAY") 0 0 0
729 {
730 0 my ( $ret );
731  
732 $self->error( "Policy arg wasn't even-sized arrayref" )
733 0 ▶ unless $#{$policies} % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$policies;} % 2 0 0 0
734  
735 0 $ret = { order => [], param => {} };
736 0 ▶ for( my $i = 0; $i <= $#{$policies}; $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$policies;}) 0 0 0
737 {
738 0 push @{$ret->{ order }}, $policies->[ $i ];
739 0 $ret->{ param }->{ $policies->[ $i ] } = $policies->[ $i + 1 ];
740 }
741  
742 0 return( $ret );
743 }
744 0 $self->error( "Unknown policy format: " . ref( $policies ) );
745 }
746  
747 sub set_policy
748 18 ▶{
Subroutine set_policy: called CC=13 SCAR=34.6
Pod documented
749 18 my ( $self, $policytype, $policies ) = @_;
750 18 my ( $factoryclass );
751  
752 18 ▶ $self->error( "No $policytype policy set" ) unless $policies;
Branch: 1 branch
BranchTrueFalseTotal
unless $policies 0 18 18
753  
754 18 $policies = $self->_normalize_policies( $policies );
755 18 $self->{ policies }->{ $policytype } = $policies;
756  
757 18 ▶ $factoryclass = 'Cache::CacheFactory::' .
Branch: 1 branch
BranchTrueFalseTotal
$policytype eq 'storage' ? : 12 6 18
758 ( $policytype eq 'storage' ? 'Storage' : 'Expiry' );
759  
760 # Handle compat param.
761 $policies->{ param }->{ time }->{ default_expires_in } =
762 $self->{ compat }->{ default_expires_in }
763 if exists $self->{ compat }->{ default_expires_in } and
764 $policies->{ param }->{ time } and
765 18 ▶ not exists $policies->{ param }->{ time }->{ default_expires_in };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"compat"}{"default_expires_in"} and $policies->{"param"}{"time"} and not exists $policies->{"param"}{"time"}{"default_expires_in"} 0 18 18
Condition33%2/6
exists $self->{"compat"}{"default_expires_in"} and $policies->{"param"}{"time"} and not exists $policies->{"param"}{"time"}{"default_expires_in"}
!ll&&!rl&&r
1800
exists $self->{"compat"}{"default_expires_in"} and $policies->{"param"}{"time"}
!ll&&!rl&&r
1800
MC/DC0%0/3
exists $self->{"compat"}{"default_expires_in"} and $policies->{"param"}{"time"} and not exists $policies->{"param"}{"time"}{"default_expires_in"}
exists $self->{"compat"}{"default_expires_in"} $policies->{"param"}{"time"} not exists $policies->{"param"}{"time"}{"default_expires_in"}
Truth table25%1/4
A and B and not(C)
A= exists $self->{"compat"}{"default_expires_in"}B= $policies->{"param"}{"time"}C= not exists $policies->{"param"}{"time"}{"default_expires_in"}
ABCresult
0XX0
10X0
1100
1111
766  
767 18 $policies->{ drivers } = {};
768 18 foreach my $policy ( @{$policies->{ order }} )
769 {
770 18 my ( $driver, $param );
771  
772 18 $param = $policies->{ param }->{ $policy };
773 18 delete $policies->{ param }->{ $policy };
774  
775 # Ensure we set the namespace if one isn't set explicitly.
776 $param->{ namespace } = $self->{ namespace }
777 18 ▶ if $policytype eq 'storage' and not exists $param->{ namespace };
Branch: 1 branch
BranchTrueFalseTotal
if $policytype eq "storage" and not exists $param->{"namespace"} 12 6 18
Condition66%2/3
$policytype eq "storage" and not exists $param->{"namespace"}
!ll&&!rl&&r
6012
MC/DC50%1/2
$policytype eq "storage" and not exists $param->{"namespace"}
$policytype eq "storage" not exists $param->{"namespace"}
Truth table66%2/3
A and not(B)
A= $policytype eq "storage"B= not exists $param->{"namespace"}
ABresult
0X0
100
111
778  
779 18 $driver = $factoryclass->new( $policy, $param );
780 18 ▶ if( $driver )
Branch: 1 branch
BranchTrueFalseTotal
if ($driver) { } 18 0 18
781 {
782 18 $policies->{ drivers }->{ $policy } = $driver;
783 }
784 else
785 {
786 0 my ( $driver_module, $error );
787  
788 0 $driver_module = $factoryclass->get_registered_class( $policy );
789 0 $error = "Unable to load driver for $policytype policy: $policy";
790 0 ▶ if( $driver_module )
Branch: 1 branch
BranchTrueFalseTotal
if ($driver_module) { } 0 0 0
791 {
792 0 $error .= "; is $driver_module installed?";
793 }
794 else
795 {
796 0 $error .= "; is '$policy' a typo, or a custom policy that " .
797 "hasn't been registered with $factoryclass?";
798 }
799 0 ▶ if( $self->{ nonfatal_missing_policies } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'nonfatal_missing_policies'}) { } 0 0 0
800 {
801 $self->warning( $error )
802 0 ▶ unless $self->{ nonwarning_missing_policies };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"nonwarning_missing_policies"} 0 0 0
803 }
804 else
805 {
806 0 $self->error( $error );
807 }
808 # Prune it from the policy run order.
809 $policies->{ order } =
810 0 [ grep { $_ ne $policy } @{$policies->{ order }} ];
811 }
812  
813 }
814 }
815  
816 sub get_policy_driver
817 2 ▶{
Subroutine get_policy_driver: called CC=1 SCAR=0.0
Pod documented
818 2 my ( $self, $policytype, $policy ) = @_;
819  
820 2 return( $self->{ policies }->{ $policytype }->{ drivers }->{ $policy } );
821 }
822 sub get_policy_drivers
823 0 ▶{
Subroutine get_policy_drivers: not called CC=1 SCAR=6.9
Pod documented
824 0 my ( $self, $policytype ) = @_;
825  
826 0 return( $self->{ policies }->{ $policytype }->{ drivers } );
827 }
828  
829 #
830 #
831 # Next few methods run a closure against each policy or invoke a
832 # method against each policy's driver. It's a bit inefficient but
833 # saves on duplicating the same ordering and looping code everywhere
834 # and keeps me sane(ish). Oh for a native ordered-hashref.
835 sub last
836 28 ▶{
Subroutine last: called CC=1 SCAR=0.0
Pod documented
837 # my ( $self ) = @_;
838 28 $_[ 0 ]->{ _last } = 1;
839 }
840  
841 sub foreach_policy
842 92 ▶{
Subroutine foreach_policy: called CC=3 SCAR=11.0
Pod documented
843 92 my ( $self, $policytype, $closure ) = @_;
844  
845 92 my $policies = $self->{ policies }->{ $policytype };
846 92 foreach my $policy ( @{$policies->{ order }} )
847 {
848 92 $closure->( $self, $policy, $policies->{ drivers }->{ $policy } );
849 92 ▶ next unless $self->{ _last };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_last"} 64 28 92
850 28 delete $self->{ _last };
851 28 return;
852 }
853 }
854  
855 sub foreach_driver
856 309 ▶{
Subroutine foreach_driver: called CC=3 SCAR=11.8
Pod documented
857 309 my ( $self, $policytype, $method, @args ) = @_;
858  
859 309 my $policies = $self->{ policies }->{ $policytype };
860 309 foreach my $policy ( @{$policies->{ order }} )
861 {
862 156 $policies->{ drivers }->{ $policy }->$method( @args );
863 156 ▶ next unless $self->{ _last };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_last"} 156 0 156
864 0 delete $self->{ _last };
865 0 return;
866 }
867 }
868  
869 sub set_storage_policies
870 12 ▶{
Subroutine set_storage_policies: called CC=1 SCAR=0.0
Pod documented
871 12 my ( $self, $policies ) = @_;
872  
873 12 $self->set_policy( 'storage', $policies );
874 }
875  
876 sub set_pruning_policies
877 4 ▶{
Subroutine set_pruning_policies: called CC=1 SCAR=0.0
Pod documented
878 4 my ( $self, $policies ) = @_;
879  
880 4 $self->set_policy( 'pruning', $policies );
881 }
882  
883 sub set_validity_policies
884 2 ▶{
Subroutine set_validity_policies: called CC=1 SCAR=0.0
Pod documented
885 2 my ( $self, $policies ) = @_;
886  
887 2 $self->set_policy( 'validity', $policies );
888 }
889  
890 sub _error_message
891 0 ▶{
Subroutine _error_message: not called CC=1 SCAR=6.9
892 0 my $self = shift;
893 0 my ( $error );
894  
895 0 $error = join( '', @_ );
896 0 return( "Cache error: $error" );
897 }
898  
899 sub error
900 0 ▶{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
901 0 my $self = shift;
902 0 die( $self->_error_message( @_ ) );
903 }
904  
905 sub warning
906 0 ▶{
Subroutine warning: not called CC=1 SCAR=6.9
Pod documented
907 0 my $self = shift;
908 0 warn( $self->_error_message( @_ ) );
909 }
910  
911 #
912 #
913 # Non-OO functions.
914 #
915  
916 sub _best_available_policy
917 3 ▶{
Subroutine _best_available_policy: called CC=4 SCAR=14.5
918 3 my ( $policytype, @policies ) = @_;
919 3 my ( $factoryclass );
920  
921 3 ▶ $factoryclass = 'Cache::CacheFactory::' .
Branch: 1 branch
BranchTrueFalseTotal
$policytype eq 'storage' ? : 1 2 3
922 ( $policytype eq 'storage' ? 'Storage' : 'Expiry' );
923 3 ▶ while( my $policy = shift( @policies ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $policy = shift @policies) 3 0 3
924 {
925 3 ▶ return( $policy ) if $factoryclass->get_registered_class( $policy );
Branch: 1 branch
BranchTrueFalseTotal
if $factoryclass->get_registered_class($policy) 3 0 3
926 }
927 0 return( undef );
928 }
929  
930 sub best_available_storage_policy
931 1 ▶{
Subroutine best_available_storage_policy: called CC=1 SCAR=0.0
Pod documented
932 1 return( _best_available_policy( 'storage', @_ ) );
933 }
934  
935 sub best_available_pruning_policy
936 1 ▶{
Subroutine best_available_pruning_policy: called CC=1 SCAR=0.0
Pod documented
937 1 return( _best_available_policy( 'pruning', @_ ) );
938 }
939  
940 sub best_available_validity_policy
941 1 ▶{
Subroutine best_available_validity_policy: called CC=1 SCAR=0.0
Pod documented
942 1 return( _best_available_policy( 'validity', @_ ) );
943 }
944  
945  
946 11 1;
947  
948 __END__
↑ Summary CacheFactory/Expiry/Size.pm »