Cache-CacheFactory
view release on metacpan or search on metacpan
lib/Cache/CacheFactory/Expiry/LastModified.pm view on Meta::CPAN
my ( $metadata, $dependencies, $timecreated );
$metadata = $object->get_policy_metadata( $policytype, 'lastmodified' );
$dependencies = $metadata->{ dependencies };
$timecreated = $object->get_created_at();
return( 1 ) unless $dependencies and $#{$dependencies} > -1;
foreach my $file ( @{$dependencies} )
{
# TODO: options to cache the stat() calls.
return( 0 ) unless -e $file and (stat( $file ))[ 9 ] < $timecreated;
}
return( 1 );
}
1;
=pod
=head1 NAME
lib/Cache/CacheFactory/Expiry/LastModified.pm view on Meta::CPAN
=over
=item dependencies => $filename
=item dependencies => [ $filename1, $filename2, ... ]
This marks the cache entry as depending on the provided filenames,
if any of these files are modified after the cache entry is created
the entry is considered invalid or is eligible for pruning.
This is done by comparing the last-modified time (as read via C<stat()>)
to the C<created_at> value for the cache entry, this will normally
be reliable but be aware that some programs (tar for example) will
falsify last-modified times, and it's also possible to manipulate
the C<created_at> time of a cache entry when first storing it.
Also if the process you are using to generate the content from source
is lengthy it is probably best to take a timestamp from before you
read the source files and supply this as C<created_at> value when
doing C<< $cache->set() >> - this will ensure that any modifications
to the source files between the time you read their content and you
lib/Cache/CacheFactory/Expiry/LastModified.pm view on Meta::CPAN
created_at => $time,
dependencies => $file,
);
=back
=head1 KNOWN ISSUES AND BUGS
=over
=item C<stat()> is expensive
Calling C<stat()> on a lot of files is quite expensive, especially
if you're doing it repeatedly. There really ought to be a mechanism
to say that you want to cache the results for a period. Ah, if only
someone had written a handy caching module...
This will probably make it into a future release.
=back
=head1 SEE ALSO
t/40-expiry-lastmodified.t view on Meta::CPAN
$time = time();
$fh = IO::File->new( "> $filename" );
unless( $fh )
{
diag( "Couldn't open $filename for write." );
return( 0 );
}
$fh->print( "touched at $time\n" );
$fh->close();
$mtime = (stat( $filename ))[ 9 ];
return( $mtime ) if $mtime >= $time;
diag( "$filename mtime is in the past - last-mod-time: " .
localtime( $mtime ) . " vs file-open-time: " .
localtime( $time ) . "." );
return( 0 );
}
( run in 1.407 second using v1.01-cache-2.11-cpan-49f99fa48dc )