File Coverage

blib/lib/Cache/CacheFactory/Expiry/LastModified.pm
Criterion Covered Total %
statement 31 33 93.9
branch 5 8 62.5
condition 3 6 50.0
subroutine 8 9 88.8
pod 4 4 100.0
total 51 60 85.0


line stmt bran cond sub pod time code
1             ###############################################################################
2             # Purpose : Cache LastModified Expiry Policy Class.
3             # Author : Sam Graham
4             # Created : 25 Jun 2008
5             # CVS : $Id: LastModified.pm,v 1.7 2010-02-16 12:25:41 illusori Exp $
6             ###############################################################################
7              
8             package Cache::CacheFactory::Expiry::LastModified;
9              
10 1     1   852 use warnings;
  1         2  
  1         38  
11 1     1   7 use strict;
  1         2  
  1         33  
12              
13 1     1   683 use Cache::CacheFactory::Expiry::Base;
  1         3  
  1         52  
14              
15 1     1   7 use base qw/Cache::CacheFactory::Expiry::Base/;
  1         2  
  1         535  
16              
17             $Cache::CacheFactory::Expiry::LastModified::VERSION = '1.10';
18              
19             sub read_startup_options
20             {
21 1     1 1 4 my ( $self, $param ) = @_;
22              
23             # Oh, no startup options. That's easy.
24             }
25              
26             sub _set_object_metadata
27             {
28 1     1   2 my ( $self, $policytype, $key, $object, $param ) = @_;
29 1         2 my ( $dependencies );
30              
31 1 50       5 if( $param->{ dependencies } )
32             {
33             $dependencies = ref( $param->{ dependencies } ) ?
34 1 50       5 $param->{ dependencies } : [ $param->{ dependencies } ];
35             }
36              
37             # TODO: warn if files don't exist?
38              
39 1         6 $object->set_policy_metadata( $policytype, 'lastmodified',
40             { dependencies => $dependencies, } );
41             }
42              
43             sub set_object_validity
44             {
45 1     1 1 3 my ( $self, $key, $object, $param ) = @_;
46              
47 1         4 $self->_set_object_metadata( 'validity', $key, $object, $param );
48             }
49              
50             sub set_object_pruning
51             {
52 0     0 1 0 my ( $self, $key, $object, $param ) = @_;
53              
54 0         0 $self->_set_object_metadata( 'pruning', $key, $object, $param );
55             }
56              
57             sub should_keep
58             {
59 4     4 1 6 my ( $self, $cache, $storage, $policytype, $object ) = @_;
60 4         6 my ( $metadata, $dependencies, $timecreated );
61              
62 4         18 $metadata = $object->get_policy_metadata( $policytype, 'lastmodified' );
63 4         7 $dependencies = $metadata->{ dependencies };
64 4         17 $timecreated = $object->get_created_at();
65              
66 4 50 33     29 return( 1 ) unless $dependencies and $#{$dependencies} > -1;
  4         17  
67              
68 4         5 foreach my $file ( @{$dependencies} )
  4         11  
69             {
70             # TODO: options to cache the stat() calls.
71 4 100 66     186 return( 0 ) unless -e $file and (stat( $file ))[ 9 ] < $timecreated;
72             }
73              
74 2         17 return( 1 );
75             }
76              
77             1;
78              
79             =pod
80              
81             =head1 NAME
82              
83             Cache::CacheFactory::Expiry::LastModified - File last-modified date dependencies expiry policy for Cache::CacheFactory.
84              
85             =head1 DESCRIPTION
86              
87             L
88             is an expiry (pruning and validity) policy for
89             L.
90              
91             It provides the ability to prune or invalidate cache entries by
92             comparing the create time of the entry to the last-modified time
93             of a list of files (AKA dependencies).
94              
95             =head1 STARTUP OPTIONS
96              
97             There are no startup options for
98             L.
99              
100             =head1 STORE OPTIONS
101              
102             The following options may be set when storing a key, see the
103             L documentation for
104             details on how to do this.
105              
106             =over
107              
108             =item dependencies => $filename
109              
110             =item dependencies => [ $filename1, $filename2, ... ]
111              
112             This marks the cache entry as depending on the provided filenames,
113             if any of these files are modified after the cache entry is created
114             the entry is considered invalid or is eligible for pruning.
115              
116             This is done by comparing the last-modified time (as read via C)
117             to the C value for the cache entry, this will normally
118             be reliable but be aware that some programs (tar for example) will
119             falsify last-modified times, and it's also possible to manipulate
120             the C time of a cache entry when first storing it.
121              
122             Also if the process you are using to generate the content from source
123             is lengthy it is probably best to take a timestamp from before you
124             read the source files and supply this as C value when
125             doing C<< $cache->set() >> - this will ensure that any modifications
126             to the source files between the time you read their content and you
127             stored the generated content will be correctly detected.
128              
129             For example:
130              
131             $time = time();
132             $data = expensive_read_and_build_data_from_file( $file );
133             $cache->set(
134             key => 'mykey',
135             data => $data,
136             created_at => $time,
137             dependencies => $file,
138             );
139              
140             =back
141              
142             =head1 KNOWN ISSUES AND BUGS
143              
144             =over
145              
146             =item C is expensive
147              
148             Calling C on a lot of files is quite expensive, especially
149             if you're doing it repeatedly. There really ought to be a mechanism
150             to say that you want to cache the results for a period. Ah, if only
151             someone had written a handy caching module...
152              
153             This will probably make it into a future release.
154              
155             =back
156              
157             =head1 SEE ALSO
158              
159             L, L, L,
160             L
161              
162             =head1 AUTHORS
163              
164             Original author: Sam Graham
165              
166             Last author: $Author: illusori $
167              
168             =head1 COPYRIGHT
169              
170             Copyright 2008-2010 Sam Graham.
171              
172             This library is free software; you can redistribute it and/or
173             modify it under the same terms as Perl itself.
174              
175             =cut