CacheFactory/Expiry/Time.pm

filter: Statementstmt 90.5% 48 / 53 Branchbran 50.0% 15 / 30 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 10 / 12 Podpod 100% 6 / 6 totaltotal 72.9% 81 / 111 CC 18 SCAR 15.4
CC
18
Cov
73%
read_startup_options
6.8
should_keep
6.2
get_default_expires_in
6.0
CRAP 4.7
« CacheFactory/Expiry/Size.pm ↑ Summary CacheFactory/Expiry/LastModified.pm »
1 ###############################################################################
2 # Purpose : Cache Time Expiry Policy Class.
3 # Author : Sam Graham
4 # Created : 25 Jun 2008
5 # CVS : $Id: Time.pm,v 1.8 2010-02-16 12:25:41 illusori Exp $
6 ###############################################################################
7  
8 package Cache::CacheFactory::Expiry::Time;
9  
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Cache::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Cache::BaseCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Cache::CacheFactory::Expiry::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use base qw/Cache::CacheFactory::Expiry::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 $Cache::CacheFactory::Expiry::Time::VERSION = '1.10';
21  
22 sub read_startup_options
23 2 ▶{
Subroutine read_startup_options: called CC=5 SCAR=19.2
Pod documented
24 2 my ( $self, $param ) = @_;
25  
26 $self->set_default_expires_in( $param->{ default_expires_in } )
27 2 ▶ if exists( $param->{ default_expires_in } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"default_expires_in"} 0 2 2
28  
29 $self->{ default_prune_after } = $param->{ default_prune_after }
30 2 ▶ if exists( $param->{ default_prune_after } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"default_prune_after"} 0 2 2
31 $self->{ default_valid_until } =
32 ( $param->{ default_valid_until } eq 'forever' ) ?
33 $Cache::Cache::EXPIRES_NEVER : $param->{ default_valid_until }
34 2 ▶ if exists( $param->{ default_valid_until } );
Branch: 2 branches
BranchTrueFalseTotal
if exists $param->{"default_valid_until"} 0 2 2
$param->{'default_valid_until'} eq 'forever' ? : 0 0 0
35 }
36  
37 sub set_default_expires_in
38 0 ▶{
Subroutine set_default_expires_in: not called CC=1 SCAR=6.9
Pod documented
39 0 my ( $self, $default_expires_in ) = @_;
40  
41 # Compat with Cache::Cache.
42 0 $self->{ default_prune_after } = $default_expires_in;
43 0 $self->{ default_valid_until } = $default_expires_in;
44 }
45  
46 sub get_default_expires_in
47 0 ▶{
Subroutine get_default_expires_in: not called CC=2 SCAR=17.9
Pod documented
48 0 my ( $self ) = @_;
49  
50 0 ▶ return( $self->{ default_prune_after } || $self->{ default_valid_until } );
Condition0%0/3
$self->{'default_prune_after'} || $self->{'default_valid_until'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'default_prune_after'} || $self->{'default_valid_until'}
$self->{'default_prune_after'} $self->{'default_valid_until'}
Truth table0%0/3
A || B
A= $self->{'default_prune_after'}B= $self->{'default_valid_until'}
ABresult
1X1
011
000
51 }
52  
53 sub set_object_validity
54 2 ▶{
Subroutine set_object_validity: called CC=5 SCAR=17.0
Pod documented
55 2 my ( $self, $key, $object, $param ) = @_;
56 2 my ( $valid_until );
57  
58 # Failover in order:
59 # supplied valid_until param
60 # supplied expires_in param
61 # default valid_until param (includes default expires_in)
62 # EXPIRES_NEVER as last resort.
63 $valid_until = $self->{ default_valid_until }
64 2 ▶ if exists( $self->{ default_valid_until } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"default_valid_until"} 0 2 2
65 $valid_until = $param->{ expires_in }
66 2 ▶ if exists( $param->{ expires_in } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"expires_in"} 0 2 2
67 $valid_until =
68 ( $param->{ valid_until } eq 'forever' ) ?
69 $Cache::Cache::EXPIRES_NEVER : $param->{ valid_until }
70 2 ▶ if exists( $param->{ valid_until } );
Branch: 2 branches
BranchTrueFalseTotal
if exists $param->{"valid_until"} 2 0 2
$param->{'valid_until'} eq 'forever' ? : 0 2 2
71  
72 2 $valid_until = Cache::BaseCache::Build_Expires_At(
73 $object->get_created_at(),
74 $Cache::Cache::EXPIRES_NEVER,
75 $valid_until );
76  
77 2 $object->set_policy_metadata( 'validity', 'time',
78 { valid_until => $valid_until, } );
79 }
80  
81 sub set_object_pruning
82 2 ▶{
Subroutine set_object_pruning: called CC=4 SCAR=14.3
Pod documented
83 2 my ( $self, $key, $object, $param ) = @_;
84 2 my ( $prune_after );
85  
86 # Failover in order:
87 # supplied prune_after param
88 # supplied expires_in param
89 # default prune_after param (includes default expires_in)
90 # EXPIRES_NEVER as last resort.
91 $prune_after = $self->{ default_prune_after }
92 2 ▶ if exists( $self->{ default_prune_after } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"default_prune_after"} 0 2 2
93 $prune_after = $param->{ expires_in }
94 2 ▶ if exists( $param->{ expires_in } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"expires_in"} 0 2 2
95 $prune_after = $param->{ prune_after }
96 2 ▶ if exists( $param->{ prune_after } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"prune_after"} 2 0 2
97  
98 2 $prune_after = Cache::BaseCache::Build_Expires_At(
99 $object->get_created_at(),
100 $Cache::Cache::EXPIRES_NEVER,
101 $prune_after );
102  
103 2 $object->set_policy_metadata( 'pruning', 'time',
104 { prune_after => $prune_after, } );
105 }
106  
107 sub should_keep
108 11 ▶{
Subroutine should_keep: called CC=6 SCAR=18.3
Pod documented
109 11 my ( $self, $cache, $storage, $policytype, $object ) = @_;
110 11 my ( $metadata, $expires, $when );
111  
112 11 $metadata = $object->get_policy_metadata( $policytype, 'time' );
113 11 ▶ $expires = $metadata->{ valid_until } || $metadata->{ prune_after };
Condition66%2/3
$metadata->{'valid_until'} || $metadata->{'prune_after'}
l!l&&r!l&&!r
740
MC/DC0%0/2
$metadata->{'valid_until'} || $metadata->{'prune_after'}
$metadata->{'valid_until'} $metadata->{'prune_after'}
Truth table66%2/3
A || B
A= $metadata->{'valid_until'}B= $metadata->{'prune_after'}
ABresult
1X1
011
000
114 11 $when = time();
115  
116 11 ▶ return( 1 ) unless defined( $expires );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expires 0 11 11
117 11 ▶ return( 0 ) if $expires eq $Cache::Cache::EXPIRES_NOW;
Branch: 1 branch
BranchTrueFalseTotal
if $expires eq $Cache::Cache::EXPIRES_NOW 0 11 11
118 11 ▶ return( 1 ) if $expires eq $Cache::Cache::EXPIRES_NEVER;
Branch: 1 branch
BranchTrueFalseTotal
if $expires eq $Cache::Cache::EXPIRES_NEVER 0 11 11
119 11 ▶ return( 0 ) if $when >= $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $when >= $expires 3 8 11
120 8 return( 1 );
121 }
122  
123 2 1;
124  
125 =pod
126  
127 =head1 NAME
128  
129 Cache::CacheFactory::Expiry::Time - Time-based expiry policy for Cache::CacheFactory.
130  
131 =head1 DESCRIPTION
132  
133 L<Cache::CacheFactory::Expiry::Time>
134 is a time-based expiry (pruning and validity) policy for
135 L<Cache::CacheFactory>.
136  
137 It provides similar functionality and backwards-compatibility with
138 the C<$expires_in> and C<$default_expires_in> properties of
139 L<Cache::Cache>.
140  
141 =head1 INTERVAL SPECIFICATIONS
142  
143 You can use any of the syntaxes provided by L<Cache::Cache> to
144 specify an interval for expiry times, for example:
145  
146 $Cache::Cache::EXPIRES_NEVER
147 $Cache::Cache::EXPIRES_NOW
148 '4 seconds'
149 '1 m'
150 'now'
151 'never'
152  
153 For a full list take a look at the C<set()> section of the
154 L<Cache::Cache> documentation.
155  
156 =head1 STARTUP OPTIONS
157  
158 The following startup options may be supplied to
159 L<Cache::CacheFactory::Expiry::Time>,
160 see the L<Cache::CacheFactory> documentation for
161 how to pass options to a policy.
162  
163 =over
164  
165 =item default_prune_after => $interval
166  
167 For a pruning policy this sets the default interval after which an
168 item becomes eligible to be pruned.
169  
170 =item default_valid_until => $interval
171  
172 For a validity policy this sets the default time interval after
173 which an item should be considered invalid.
174  
175 =item default_expires_in => $interval
176  
177 This option provides backwards-compatibility with L<Cache::Cache>,
178 it sets C<default_prune_after> for pruning policies and
179 C<default_valid_until> for validity policies.
180  
181 =back
182  
183 =head1 STORE OPTIONS
184  
185 The following options may be set when storing a key, see the
186 L<Cache::CacheFactory> documentation for
187 details on how to do this.
188  
189 =over
190  
191 =item prune_after => $interval
192  
193 For a pruning policy this sets the interval after which the
194 item becomes eligible to be pruned. If not supplied then
195 the value of C<default_prune_after> in the startup options
196 will be used.
197  
198 =item valid_until => $interval
199  
200 For a validity policy this sets the time interval after
201 which the item should be considered invalid. If not supplied then
202 the value of C<default_valid_until> in the startup options
203 will be used.
204  
205 =item expires_in => $interval
206  
207 This option provides backwards-compatibility with L<Cache::Cache>,
208 it behaves as C<prune_after> for pruning policies and C<valid_until>
209 for validity policies.
210  
211 =back
212  
213 =head1 METHODS
214  
215 You should generally call these via the L<Cache::CacheFactory> interface
216 rather than directly.
217  
218 =over
219  
220 =item $policy->set_default_expires_in( $default_expires_in );
221  
222 =item $default_expires_in = $policy->get_default_expires_in();
223  
224 Set or get the C<default_expires_in> option.
225  
226 =back
227  
228 =head1 SEE ALSO
229  
230 L<Cache::CacheFactory>, L<Cache::Cache>, L<Cache::CacheFactory::Object>,
231 L<Cache::CacheFactory::Expiry::Base>
232  
233 =head1 AUTHORS
234  
235 Original author: Sam Graham <libcache-cachefactory-perl BLAHBLAH illusori.co.uk>
236  
237 Last author: $Author: illusori $
238  
239 =head1 COPYRIGHT
240  
241 Copyright 2008-2010 Sam Graham.
242  
243 This library is free software; you can redistribute it and/or
244 modify it under the same terms as Perl itself.
245  
246 =cut
« CacheFactory/Expiry/Size.pm ↑ Summary CacheFactory/Expiry/LastModified.pm »