| 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 | 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::CacheFactory::Expiry::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use base qw/Cache::CacheFactory::Expiry::Base/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | $Cache::CacheFactory::Expiry::LastModified::VERSION = '1.10'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub read_startup_options | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_startup_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 1 | my ( $self, $param ) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Oh, no startup options. That's easy. | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub _set_object_metadata | ||||||||||||||||||||||||||||
| 27 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_set_object_metadata: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 28 | 1 | my ( $self, $policytype, $key, $object, $param ) = @_; | |||||||||||||||||||||||||||
| 29 | 1 | my ( $dependencies ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | if( $param->{ dependencies } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | $dependencies = ref( $param->{ dependencies } ) ? | ||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $param->{ dependencies } : [ $param->{ dependencies } ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # TODO: warn if files don't exist? | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | $object->set_policy_metadata( $policytype, 'lastmodified', | |||||||||||||||||||||||||||
| 40 | { dependencies => $dependencies, } ); | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub set_object_validity | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_object_validity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 1 | my ( $self, $key, $object, $param ) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | $self->_set_object_metadata( 'validity', $key, $object, $param ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub set_object_pruning | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_object_pruning: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my ( $self, $key, $object, $param ) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $self->_set_object_metadata( 'pruning', $key, $object, $param ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub should_keep | ||||||||||||||||||||||||||||
| 58 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
should_keep: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 4 | my ( $self, $cache, $storage, $policytype, $object ) = @_; | |||||||||||||||||||||||||||
| 60 | 4 | my ( $metadata, $dependencies, $timecreated ); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | $metadata = $object->get_policy_metadata( $policytype, 'lastmodified' ); | |||||||||||||||||||||||||||
| 63 | 4 | $dependencies = $metadata->{ dependencies }; | |||||||||||||||||||||||||||
| 64 | 4 | $timecreated = $object->get_created_at(); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | return( 1 ) unless $dependencies and $#{$dependencies} > -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dependencies and $#{$dependencies;} > -1
MC/DC0%0/2
$dependencies and $#{$dependencies;} > -1
$dependencies
$#{$dependencies;} > -1
Truth table33%1/3
A and B
A= $dependenciesB= $#{$dependencies;} > -1
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | foreach my $file ( @{$dependencies} ) | |||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | # TODO: options to cache the stat() calls. | ||||||||||||||||||||||||||||
| 71 | 4 | ▶ | return( 0 ) unless -e $file and (stat( $file ))[ 9 ] < $timecreated; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $file and (stat $file)[9] < $timecreated
MC/DC50%1/2
-e $file and (stat $file)[9] < $timecreated
-e $file
(stat $file)[9] < $timecreated
Truth table66%2/3
A and B
A= -e $fileB= (stat $file)[9] < $timecreated
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | return( 1 ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | 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<Cache::CacheFactory::Expiry::LastModified> | ||||||||||||||||||||||||||||
| 88 | is an expiry (pruning and validity) policy for | ||||||||||||||||||||||||||||
| 89 | L<Cache::CacheFactory>. | ||||||||||||||||||||||||||||
| 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<Cache::CacheFactory::Expiry::LastModified>. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 STORE OPTIONS | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | The following options may be set when storing a key, see the | ||||||||||||||||||||||||||||
| 103 | L<Cache::CacheFactory> 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<stat()>) | ||||||||||||||||||||||||||||
| 117 | to the C<created_at> 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<created_at> 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<created_at> 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<stat()> is expensive | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Calling C<stat()> 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<Cache::CacheFactory>, L<Cache::Cache>, L<Cache::CacheFactory::Object>, | ||||||||||||||||||||||||||||
| 160 | L<Cache::CacheFactory::Expiry::Base> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Original author: Sam Graham <libcache-cachefactory-perl BLAHBLAH illusori.co.uk> | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||