| 1 | ############################################################################### | ||||||||||||||||||||||||||||||||||
| 2 | # Purpose : Cache data structures against a file (serialised in files using Storable) | ||||||||||||||||||||||||||||||||||
| 3 | # Author : John Alden | ||||||||||||||||||||||||||||||||||
| 4 | # Created : 22 Apr 2005 (based on IFL::FileCache) | ||||||||||||||||||||||||||||||||||
| 5 | # CVS : $Id: Storable.pm,v 1.22 2006/05/09 09:02:32 mattheww Exp $ | ||||||||||||||||||||||||||||||||||
| 6 | ############################################################################### | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | package Cache::AgainstFile::Storable; | ||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Cache::AgainstFile::Base; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Storable qw(store retrieve retrieve_fd lock_store lock_retrieve); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use File::Spec::Functions qw(canonpath catfile rel2abs); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use FileHandle; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use constant IS_WINDOWS => ($^O eq 'MSWin32' ? 1 : 0); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 18 | 2 | if (IS_WINDOWS) { require Win32 } | |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use constant HAVE_FILE_POLICY => eval { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||
| 21 | 2 | require File::Policy; | |||||||||||||||||||||||||||||||||
| 22 | 0 | import File::Policy qw(check_safe); | |||||||||||||||||||||||||||||||||
| 23 | 0 | 1; | |||||||||||||||||||||||||||||||||
| 24 | 2 | }; | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 27 | 2 | $VERSION = sprintf"%d.%03d", q$Revision: 1.22 $ =~ /: (\d+)\.(\d+)/; | |||||||||||||||||||||||||||||||||
| 28 | 2 | @ISA = qw(Cache::AgainstFile::Base); | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||||||||
| 31 | # Public interface | ||||||||||||||||||||||||||||||||||
| 32 | # | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 35 | 9 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 36 | 9 | my ($loader, $options) = @_; | |||||||||||||||||||||||||||||||||
| 37 | 9 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 9 | ▶ | my $dir = $self->{options}->{CacheDir} || croak("You must supply a cache directory for caching with Storable"); | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'options'}{'CacheDir'} || croak('You must supply a cache directory for caching with Storable')
MC/DC100%1/1
$self->{'options'}{'CacheDir'} || croak('You must supply a cache directory for caching with Storable')
$self->{'options'}{'CacheDir'}
Truth table100%2/2
A
A= $self->{'options'}{'CacheDir'}
| |||||||||||||||||||||||||||||||||||
| 40 | 8 | ▶ | check_safe($dir,"w") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 41 | 8 | _create_dir_if_required($dir); | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | #Select locking implementation | ||||||||||||||||||||||||||||||||||
| 44 | 8 | ▶ | my $locking = $options->{Locking} || 'AtomicWrite'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'Locking'} || 'AtomicWrite'
MC/DC100%1/1
$options->{'Locking'} || 'AtomicWrite'
$options->{'Locking'}
Truth table100%2/2
A
A= $options->{'Locking'}
| |||||||||||||||||||||||||||||||||||
| 45 | 8 | ▶ | if($locking eq 'Flock') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 46 | 1 | $self->{write} = \&_write_locked; | |||||||||||||||||||||||||||||||||
| 47 | 1 | $self->{read} = \&_read_locked; | |||||||||||||||||||||||||||||||||
| 48 | } elsif ($locking eq 'AtomicWrite') { | ||||||||||||||||||||||||||||||||||
| 49 | 6 | $self->{write} = \&_write_atomic; | |||||||||||||||||||||||||||||||||
| 50 | 6 | $self->{read} = \&_read; | |||||||||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||||||||
| 52 | 1 | croak("Unrecognised locking model '$locking'"); | |||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 7 | return $self; | |||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | sub get | ||||||||||||||||||||||||||||||||||
| 59 | 36 | ▶ | { | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=15 SCAR=27.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 60 | 36 | my ($self, $filename, @opts) = @_; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 36 | ▶ | check_safe($filename,"r") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 36 | my $cache_dir = $self->{options}{CacheDir}; | |||||||||||||||||||||||||||||||||
| 65 | 36 | my $cache_filename = catfile($cache_dir, $self->_filename2cache($filename)); | |||||||||||||||||||||||||||||||||
| 66 | 36 | TRACE("cache get - cache filename is '$cache_filename'"); | |||||||||||||||||||||||||||||||||
| 67 | 36 | my $stale = 0; | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | # In some (as yet undetermined) circumstances the cachefile directory | ||||||||||||||||||||||||||||||||||
| 70 | # can disappear, which causes application errors | ||||||||||||||||||||||||||||||||||
| 71 | 36 | _create_dir_if_required($cache_dir); | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | # If cachefile doesn't exist, it won't open, implying staleness. | ||||||||||||||||||||||||||||||||||
| 74 | 36 | my $cache_fh = new FileHandle; | |||||||||||||||||||||||||||||||||
| 75 | 36 | ▶ | check_safe($cache_filename,"r") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | 36 | ▶ | unless ($cache_fh->open($cache_filename)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 77 | 25 | undef $cache_fh; | |||||||||||||||||||||||||||||||||
| 78 | 25 | $stale = 1; | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | # Compare file mtimes to check staleness | ||||||||||||||||||||||||||||||||||
| 82 | 36 | my $file_mtime; | |||||||||||||||||||||||||||||||||
| 83 | 36 | ▶ | unless ($self->{options}->{NoStat} && !$stale) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"options"}{"NoStat"} and not $stale
MC/DC100%2/2
$self->{"options"}{"NoStat"} and not $stale
$self->{"options"}{"NoStat"}
not $stale
Truth table100%3/3
A and not(B)
A= $self->{"options"}{"NoStat"}B= not $stale
| |||||||||||||||||||||||||||||||||||
| 84 | 34 | $file_mtime = (stat($filename))[9]; | |||||||||||||||||||||||||||||||||
| 85 | 34 | ▶ | my $cache_mtime = ($cache_fh->stat)[9] if $cache_fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 86 | 34 | ▶ | $stale = (!defined $file_mtime) || (!defined $cache_mtime) || ($file_mtime != $cache_mtime); | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
!(defined $file_mtime && defined $cache_mtime) || $file_mtime != $cache_mtime
defined $file_mtime && defined $cache_mtime
MC/DC66%2/3
!(defined $file_mtime && defined $cache_mtime) || $file_mtime != $cache_mtime
defined $file_mtime
defined $cache_mtime
$file_mtime != $cache_mtime
Truth table75%3/4
not(A && B) || C
A= defined $file_mtimeB= defined $cache_mtimeC= $file_mtime != $cache_mtime
| |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | 36 | ▶ | TRACE("Cache " . ($stale?"is":"is not") . " stale"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | #Read from cache | ||||||||||||||||||||||||||||||||||
| 91 | 36 | my $data; | |||||||||||||||||||||||||||||||||
| 92 | 36 | ▶ | if (!$stale) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | 8 | $data = eval { $self->{read}->($cache_filename, $cache_fh) }; | |||||||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | 0 | warn "Storable couldn't retrieve $cache_filename: $@"; | |||||||||||||||||||||||||||||||||
| 96 | 0 | $stale = 1; | |||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | 36 | ▶ | $cache_fh->close if $cache_fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | #Write to cache | ||||||||||||||||||||||||||||||||||
| 102 | 36 | ▶ | if ($stale) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | 28 | TRACE("writing cache"); | |||||||||||||||||||||||||||||||||
| 104 | 28 | $data = $self->{loader}->($filename, @opts); | |||||||||||||||||||||||||||||||||
| 105 | 28 | ▶ | $file_mtime = (stat($filename))[9] unless(defined $file_mtime); #Need mtime now | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 28 | $self->{write}->($cache_filename, $data, $file_mtime); | |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | 36 | return $data; | |||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | sub count { | ||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 113 | 8 | my ($self) = shift; | |||||||||||||||||||||||||||||||||
| 114 | 8 | my $files_in_cache = $self->_cache_files; | |||||||||||||||||||||||||||||||||
| 115 | 7 | return scalar @$files_in_cache; | |||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | sub size { | ||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 119 | 5 | my ($self) = shift; | |||||||||||||||||||||||||||||||||
| 120 | 5 | my $cache_dir = $self->{options}{CacheDir}; | |||||||||||||||||||||||||||||||||
| 121 | 5 | my $files_in_cache = $self->_cache_files; | |||||||||||||||||||||||||||||||||
| 122 | 5 | my $sum = 0; | |||||||||||||||||||||||||||||||||
| 123 | 5 | foreach(@$files_in_cache) {$sum += -s catfile($cache_dir, $_)} | |||||||||||||||||||||||||||||||||
| 124 | 5 | return $sum; | |||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||||||||
| 128 | # Protected methods referenced from Base class | ||||||||||||||||||||||||||||||||||
| 129 | # | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 7 | ▶ | sub _remove { | ||||||||||||||||||||||||||||||||
|
Subroutine
_remove: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 132 | 7 | my($self, $keys) = @_; | |||||||||||||||||||||||||||||||||
| 133 | 7 | my $cache_dir = $self->{options}{CacheDir}; | |||||||||||||||||||||||||||||||||
| 134 | 7 | foreach(@$keys) | |||||||||||||||||||||||||||||||||
| 135 | { | ||||||||||||||||||||||||||||||||||
| 136 | 12 | my $filename = $self->_filename2cache($_); | |||||||||||||||||||||||||||||||||
| 137 | 12 | TRACE("Deleting cache for $_ ($filename)"); | |||||||||||||||||||||||||||||||||
| 138 | 12 | unlink catfile($cache_dir, $filename); | |||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | 11 | ▶ | sub _accessed { | ||||||||||||||||||||||||||||||||
|
Subroutine
_accessed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 143 | 11 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 144 | 11 | my $cache_dir = $self->{options}{CacheDir}; | |||||||||||||||||||||||||||||||||
| 145 | 11 | my $files_in_cache = $self->_cache_files; | |||||||||||||||||||||||||||||||||
| 146 | my %accessed = map | ||||||||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||||||||
| 148 | 11 | my $cache_file = catfile($cache_dir, $_); | |||||||||||||||||||||||||||||||||
| 149 | 17 | $self->_cache2filename($_) => (stat($cache_file))[8] | |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | @$files_in_cache; | ||||||||||||||||||||||||||||||||||
| 152 | 11 | return \%accessed; | |||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||
| 155 | 1 | ▶ | sub _stale { | ||||||||||||||||||||||||||||||||
|
Subroutine
_stale: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||
| 156 | 1 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 157 | 1 | my $cache_dir = $self->{options}{CacheDir}; | |||||||||||||||||||||||||||||||||
| 158 | 1 | my $files_in_cache = $self->_cache_files; | |||||||||||||||||||||||||||||||||
| 159 | my @out = | ||||||||||||||||||||||||||||||||||
| 160 | map | ||||||||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||||||||
| 162 | 1 | $self->_cache2filename($_) | |||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||
| 164 | grep | ||||||||||||||||||||||||||||||||||
| 165 | { | ||||||||||||||||||||||||||||||||||
| 166 | 1 | my $cache_file = catfile($cache_dir, $_); | |||||||||||||||||||||||||||||||||
| 167 | 1 | my $src_mt = (stat ($self->_cache2filename($_)))[9]; | |||||||||||||||||||||||||||||||||
| 168 | 1 | my $cache_mt = (stat ($cache_file))[9]; | |||||||||||||||||||||||||||||||||
| 169 | 1 | ▶ | (!defined $src_mt) || (!defined $cache_mt) || ($src_mt != $cache_mt) | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
!(defined $src_mt && defined $cache_mt) || $src_mt != $cache_mt
defined $src_mt && defined $cache_mt
MC/DC0%0/3
!(defined $src_mt && defined $cache_mt) || $src_mt != $cache_mt
defined $src_mt
defined $cache_mt
$src_mt != $cache_mt
Truth table25%1/4
not(A && B) || C
A= defined $src_mtB= defined $cache_mtC= $src_mt != $cache_mt
| |||||||||||||||||||||||||||||||||||
| 170 | } @$files_in_cache; | ||||||||||||||||||||||||||||||||||
| 171 | 1 | @out; | |||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||||||||
| 175 | # Private methods | ||||||||||||||||||||||||||||||||||
| 176 | # | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | 25 | ▶ | sub _cache_files { | ||||||||||||||||||||||||||||||||
|
Subroutine
_cache_files: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 179 | 25 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 180 | 25 | my $cache_dir = $self->{options}{CacheDir}; | |||||||||||||||||||||||||||||||||
| 181 | 25 | local *FH; | |||||||||||||||||||||||||||||||||
| 182 | 25 | ▶ | check_safe($cache_dir,"r") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 183 | 25 | ▶ | opendir (FH, $cache_dir) or die("unable to open directory $cache_dir - $!"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 184 | 24 | my @files = grep {$_ !~ /^\./} readdir(FH); | |||||||||||||||||||||||||||||||||
| 185 | 24 | closedir FH; | |||||||||||||||||||||||||||||||||
| 186 | 24 | DUMP("cache files", \@files); | |||||||||||||||||||||||||||||||||
| 187 | 24 | return \@files; | |||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | # | ||||||||||||||||||||||||||||||||||
| 191 | # Subroutines | ||||||||||||||||||||||||||||||||||
| 192 | # | ||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||
| 194 | 2 | ▶ | sub _read_locked { | ||||||||||||||||||||||||||||||||
|
Subroutine
_read_locked: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||
| 195 | 2 | my($cache_filename, $fh) = @_; | |||||||||||||||||||||||||||||||||
| 196 | # we don't want the filehandle. Suppose it might need to be closed | ||||||||||||||||||||||||||||||||||
| 197 | # under Win32? Close it anyway | ||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | $fh->close if $fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 199 | 2 | ▶ | check_safe($cache_filename,"r") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 200 | 2 | my $ref_data = lock_retrieve($cache_filename); | |||||||||||||||||||||||||||||||||
| 201 | 2 | TRACE("Fetched from cache file: $cache_filename"); | |||||||||||||||||||||||||||||||||
| 202 | 2 | return $$ref_data; | |||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||
| 205 | 2 | ▶ | sub _write_locked { | ||||||||||||||||||||||||||||||||
|
Subroutine
_write_locked: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 206 | 2 | my ($cache_filename, $data, $mtime) = @_; | |||||||||||||||||||||||||||||||||
| 207 | 2 | ▶ | check_safe($cache_filename,"w") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 208 | 2 | lock_store(\$data, $cache_filename); | |||||||||||||||||||||||||||||||||
| 209 | 2 | TRACE("wrote cache file: $cache_filename"); | |||||||||||||||||||||||||||||||||
| 210 | 2 | _backtouch($cache_filename, $mtime); | |||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||
| 213 | 26 | ▶ | sub _write_atomic { | ||||||||||||||||||||||||||||||||
|
Subroutine
_write_atomic: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||
| 214 | 26 | my ($cache_filename, $data, $mtime) = @_; | |||||||||||||||||||||||||||||||||
| 215 | 26 | ▶ | check_safe($cache_filename,"w") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 216 | 26 | my $temp_filename = $cache_filename . ".tmp$$"; | |||||||||||||||||||||||||||||||||
| 217 | 26 | store(\$data, $temp_filename); | |||||||||||||||||||||||||||||||||
| 218 | 26 | TRACE("wrote temp file: $temp_filename"); | |||||||||||||||||||||||||||||||||
| 219 | 26 | ▶ | (_backtouch($temp_filename, $mtime)) or die "couldn't set utime on $temp_filename: $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 220 | 26 | ▶ | rename($temp_filename, $cache_filename) or die("Unable to rename temporary file '$temp_filename' to cache file '$cache_filename'"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 221 | 26 | TRACE("moved to cache file: $cache_filename"); | |||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||
| 224 | 28 | ▶ | sub _backtouch { | ||||||||||||||||||||||||||||||||
|
Subroutine
_backtouch: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||
| 225 | 28 | my ($file, $utime) = @_; | |||||||||||||||||||||||||||||||||
| 226 | 28 | ▶ | (defined $utime) or confess "need utime"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 227 | # Might not work in race condition? Exception NOT thrown, returns false on failure. | ||||||||||||||||||||||||||||||||||
| 228 | 28 | ▶ | check_safe($file,"w") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 229 | 28 | return utime (time(), $utime, $file); | |||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||
| 232 | 6 | ▶ | sub _read { | ||||||||||||||||||||||||||||||||
|
Subroutine
_read: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||||||||
| 233 | 6 | my($cache_filename, $fh) = @_; | |||||||||||||||||||||||||||||||||
| 234 | 6 | my $ref_data; | |||||||||||||||||||||||||||||||||
| 235 | 6 | ▶ | check_safe($cache_filename,"r") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 236 | 6 | ▶ | if (!$fh) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 237 | 0 | TRACE("Reading $cache_filename..."); | |||||||||||||||||||||||||||||||||
| 238 | 0 | $ref_data = retrieve($cache_filename); | |||||||||||||||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||||||||||||||||
| 240 | 6 | TRACE("Reading $cache_filename (from filehandle)..."); | |||||||||||||||||||||||||||||||||
| 241 | 6 | $ref_data = retrieve_fd($$fh); | |||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||
| 243 | 6 | return $$ref_data; | |||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||
| 247 | 44 | ▶ | sub _create_dir_if_required { | ||||||||||||||||||||||||||||||||
|
Subroutine
_create_dir_if_required: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 248 | 44 | my ($dir) = @_; | |||||||||||||||||||||||||||||||||
| 249 | 44 | ▶ | if(! -d $dir) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 250 | 4 | eval { | |||||||||||||||||||||||||||||||||
| 251 | 4 | require File::Path; | |||||||||||||||||||||||||||||||||
| 252 | 4 | File::Path::mkpath($dir); | |||||||||||||||||||||||||||||||||
| 253 | }; | ||||||||||||||||||||||||||||||||||
| 254 | 4 | ▶ | croak "Unable to create directory $dir: $@" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||
| 259 | # escape and normalise filename | ||||||||||||||||||||||||||||||||||
| 260 | 61 | ▶ | sub _filename2cache { | ||||||||||||||||||||||||||||||||
|
Subroutine
_filename2cache: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 261 | 61 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||||||||
| 262 | 61 | TRACE({Level => 2}, "filename = $filename"); | |||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||
| 264 | #Remove redundant slashes | ||||||||||||||||||||||||||||||||||
| 265 | 61 | $filename = canonpath($filename); | |||||||||||||||||||||||||||||||||
| 266 | 61 | TRACE({Level => 2}, " - canonpath => $filename"); | |||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||
| 268 | #Make absolute | ||||||||||||||||||||||||||||||||||
| 269 | 61 | my $cache_file = rel2abs($filename); | |||||||||||||||||||||||||||||||||
| 270 | 61 | TRACE({Level => 2}, " - rel2abs => $cache_file"); | |||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||
| 272 | 61 | if (IS_WINDOWS) { | |||||||||||||||||||||||||||||||||
| 273 | # resolve C:/LONGNA~1 to C:/LongName | ||||||||||||||||||||||||||||||||||
| 274 | $cache_file = Win32::GetFullPathName($cache_file); | ||||||||||||||||||||||||||||||||||
| 275 | TRACE({Level => 2}, " - fullpathname => $cache_file"); | ||||||||||||||||||||||||||||||||||
| 276 | # normalise path separator | ||||||||||||||||||||||||||||||||||
| 277 | $cache_file =~ tr:\\:/:; | ||||||||||||||||||||||||||||||||||
| 278 | TRACE({Level => 2}, " - normalise slashes => $cache_file"); | ||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | # escape control chars, special characters, etc e.g. '/' -> '%2F' | ||||||||||||||||||||||||||||||||||
| 282 | 61 | $cache_file =~ s|([^\w\.\-])| sprintf("%%%02X", ord($1)) |eg; | |||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | # normalise case on case-insensitive filesystems | ||||||||||||||||||||||||||||||||||
| 285 | 61 | ▶ | $cache_file = lc $cache_file if File::Spec->case_tolerant; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||
| 287 | 61 | TRACE({Level => 2}," => cache filename = $cache_file"); | |||||||||||||||||||||||||||||||||
| 288 | 61 | TRACE("filename2cache $filename -> $cache_file"); | |||||||||||||||||||||||||||||||||
| 289 | 61 | return $cache_file; | |||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | # unescape filename | ||||||||||||||||||||||||||||||||||
| 293 | 32 | ▶ | sub _cache2filename { | ||||||||||||||||||||||||||||||||
|
Subroutine
_cache2filename: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 294 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 295 | 32 | my $cache_file = shift; | |||||||||||||||||||||||||||||||||
| 296 | 32 | (my $filename = $cache_file) =~ s|%([0-9A-Fa-f]{2})| chr(hex($1)) |eg; | |||||||||||||||||||||||||||||||||
| 297 | 32 | TRACE("cache2filename $cache_file -> $filename"); | |||||||||||||||||||||||||||||||||
| 298 | 32 | return $filename; | |||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||
| 301 | # | ||||||||||||||||||||||||||||||||||
| 302 | # Log::Trace stubs | ||||||||||||||||||||||||||||||||||
| 303 | # | ||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||
| 305 | 48 | ▶ | sub TRACE {} | ||||||||||||||||||||||||||||||||
|
Subroutine
TRACE: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub DUMP {} | ||||||||||||||||||||||||||||||||
|
Subroutine
DUMP: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||
| 312 | Cache::AgainstFile::Storable - cache data structures parsed from files in Storable files | ||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||
| 314 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||
| 316 | use Cache::AgainstFile; | ||||||||||||||||||||||||||||||||||
| 317 | my $cache = new Cache::AgainstFile( | ||||||||||||||||||||||||||||||||||
| 318 | \&loader, | ||||||||||||||||||||||||||||||||||
| 319 | { | ||||||||||||||||||||||||||||||||||
| 320 | Method => 'Storable', | ||||||||||||||||||||||||||||||||||
| 321 | CacheDir => '/var/tmp/cache/myapp', | ||||||||||||||||||||||||||||||||||
| 322 | # ... | ||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||
| 324 | ); | ||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||
| 326 | $data = $cache->get($filename); | ||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||
| 328 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | Data structures parsed from files are cached in "shadow" storable files. | ||||||||||||||||||||||||||||||||||
| 331 | If parsing is significantly more expensive than file I/O (e.g. with XML files), | ||||||||||||||||||||||||||||||||||
| 332 | then this will offer some benefit. | ||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | This backend is suitable for non-persistent environments (e.g. CGI scripts) | ||||||||||||||||||||||||||||||||||
| 335 | where you want the cache to outlive the process. For persistent environments, | ||||||||||||||||||||||||||||||||||
| 336 | the Memory backend may be more suitable as it saves on file I/O. | ||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||
| 338 | count() and size() are relatively expensive operations involving scanning the cache directory. | ||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||
| 340 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | =over 4 | ||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | =item CacheDir | ||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||
| 346 | Directory in which to store cache files. This is mandatory. | ||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||
| 348 | =item MaxATime | ||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||
| 350 | Purge items older than this. | ||||||||||||||||||||||||||||||||||
| 351 | Value is in seconds (default=undefined=infinity) | ||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | =item MaxItems | ||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||
| 355 | Purge oldest items from the cache to reduce the number of items in the cache to be at most this number. | ||||||||||||||||||||||||||||||||||
| 356 | Value should be an integer (default=undefined=infinity) | ||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||
| 358 | =item NoStat | ||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||
| 360 | Don't stat files to validate the cache - items are served from the cache until they are purged. | ||||||||||||||||||||||||||||||||||
| 361 | Valid values are 0|1 (default=0, i.e. files are statted) | ||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | =item Locking | ||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||
| 365 | Valid values are Flock and AtomicWrite (default is AtomicWrite). | ||||||||||||||||||||||||||||||||||
| 366 | If neither of these are to your taste, consider using Cache::AgainstFile::CacheModule with another caching module. | ||||||||||||||||||||||||||||||||||
| 367 | Some other file caching modules on CPAN are: | ||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||
| 369 | =over 4 | ||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||
| 371 | =item Cache::FileCache | ||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | This uses atomic writes. | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | =item Cache::File | ||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||
| 377 | This uses File::NFSLock for locking (no locking is also an option) | ||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | =back | ||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | =back | ||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||
| 383 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||
| 385 | $Revision: 1.22 $ on $Date: 2006/05/09 09:02:32 $ by $Author: mattheww $ | ||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||
| 387 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||
| 389 | John Alden & Piers Kent <cpan _at_ bbc _dot_ co _dot_ uk> | ||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||
| 391 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||
| 393 | (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||
| 395 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||
| 397 | =cut | ||||||||||||||||||||||||||||||||||