| 1 | ############################################################################### | ||||||||||||||||||||||||||||||||||
| 2 | # Purpose : Cache data structures in memory against a file | ||||||||||||||||||||||||||||||||||
| 3 | # Author : John Alden | ||||||||||||||||||||||||||||||||||
| 4 | # Created : 22 Apr 2005 (based on IFL::FileCache) | ||||||||||||||||||||||||||||||||||
| 5 | # CVS : $Id: Memory.pm,v 1.15 2005/11/10 15:13:57 johna Exp $ | ||||||||||||||||||||||||||||||||||
| 6 | ############################################################################### | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | package Cache::AgainstFile::Memory; | ||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Cache::AgainstFile::Base; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 1 | $VERSION = sprintf"%d.%03d", q$Revision: 1.15 $ =~ /: (\d+)\.(\d+)/; | |||||||||||||||||||||||||||||||||
| 15 | 1 | @ISA = qw(Cache::AgainstFile::Base); | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use constant HAVE_FILE_POLICY => eval { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||
| 18 | 1 | require File::Policy; | |||||||||||||||||||||||||||||||||
| 19 | 0 | import File::Policy qw(check_safe); | |||||||||||||||||||||||||||||||||
| 20 | 0 | 1; | |||||||||||||||||||||||||||||||||
| 21 | 1 | }; | |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||||||||
| 25 | # Public interface | ||||||||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 29 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 30 | 4 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | 4 | $self->{'cache'} = {}; | |||||||||||||||||||||||||||||||||
| 33 | 4 | $self->{'modified'} = {}; #Time file was last modified (days ago) | |||||||||||||||||||||||||||||||||
| 34 | 4 | $self->{'accessed'} = {}; #Time file was last accessed (days ago) | |||||||||||||||||||||||||||||||||
| 35 | 4 | $self->{'stat'} = {}; #Timestamp of last stat (seconds since epoch) | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 4 | return $self; | |||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | 36 | ▶ | sub get { | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 41 | 36 | my ($self, $filename, @opts) = @_; | |||||||||||||||||||||||||||||||||
| 42 | 36 | my $stale = (! exists $self->{cache}{$filename}); | |||||||||||||||||||||||||||||||||
| 43 | 36 | my $source_mtime; | |||||||||||||||||||||||||||||||||
| 44 | 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
| |||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||
| 46 | #Are we within the grace period since our last stat? | ||||||||||||||||||||||||||||||||||
| 47 | 34 | ▶ | my $grace = $self->{options}->{Grace} || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'options'}{'Grace'} || 0
MC/DC0%0/1
$self->{'options'}{'Grace'} || 0
$self->{'options'}{'Grace'}
Truth table50%1/2
A
A= $self->{'options'}{'Grace'}
| |||||||||||||||||||||||||||||||||||
| 48 | 34 | ▶ | my $last_checked = $self->{'stat'}{$filename} || 0; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'stat'}{$filename} || 0
MC/DC100%1/1
$self->{'stat'}{$filename} || 0
$self->{'stat'}{$filename}
Truth table100%2/2
A
A= $self->{'stat'}{$filename}
| |||||||||||||||||||||||||||||||||||
| 49 | 34 | ▶ | unless($grace > time - $last_checked) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||||||||
| 51 | 34 | TRACE("stat: $filename"); | |||||||||||||||||||||||||||||||||
| 52 | 34 | ▶ | check_safe($filename, "r") if(HAVE_FILE_POLICY); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | 34 | $source_mtime = (stat($filename))[9]; | |||||||||||||||||||||||||||||||||
| 54 | 34 | my $last_modified = $self->{modified}{$filename}; | |||||||||||||||||||||||||||||||||
| 55 | 34 | ▶ | $stale |= (!defined $source_mtime) || (!defined $last_modified) || ($source_mtime ne $last_modified); | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
!(defined $source_mtime && defined $last_modified) || $source_mtime ne $last_modified
defined $source_mtime && defined $last_modified
MC/DC66%2/3
!(defined $source_mtime && defined $last_modified) || $source_mtime ne $last_modified
defined $source_mtime
defined $last_modified
$source_mtime ne $last_modified
Truth table75%3/4
not(A && B) || C
A= defined $source_mtimeB= defined $last_modifiedC= $source_mtime ne $last_modified
| |||||||||||||||||||||||||||||||||||
| 56 | 34 | $self->{'stat'}{$filename} = time; | |||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | 36 | my $data; | |||||||||||||||||||||||||||||||||
| 60 | 36 | ▶ | if($stale) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||||||||
| 62 | 27 | TRACE("stale memory: $filename"); | |||||||||||||||||||||||||||||||||
| 63 | 27 | $data = $self->{loader}->($filename, @opts); | |||||||||||||||||||||||||||||||||
| 64 | 27 | ▶ | if (defined $source_mtime) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 27 | $self->{cache}{$filename} = $data; | |||||||||||||||||||||||||||||||||
| 66 | 27 | $self->{modified}{$filename} = $source_mtime; | |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | else | ||||||||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||||||||
| 71 | 9 | TRACE("not stale memory: $filename"); | |||||||||||||||||||||||||||||||||
| 72 | 9 | $data = $self->{cache}{$filename}; | |||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | 36 | $self->{accessed}{$filename} = time(); | |||||||||||||||||||||||||||||||||
| 75 | 36 | return $data; | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | sub count { | ||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 79 | 7 | my $self = shift(); | |||||||||||||||||||||||||||||||||
| 80 | 7 | return scalar keys %{$self->{cache}}; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub size { | ||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 84 | 2 | my $self = shift(); | |||||||||||||||||||||||||||||||||
| 85 | 2 | eval { | |||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | no warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 87 | 2 | require Devel::Size; | |||||||||||||||||||||||||||||||||
| 88 | 0 | local $Devel::Size::warn = 0; | |||||||||||||||||||||||||||||||||
| 89 | 0 | return Devel::Size::total_size($self->{cache}); | |||||||||||||||||||||||||||||||||
| 90 | }; | ||||||||||||||||||||||||||||||||||
| 91 | 2 | return undef; | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||||
| 95 | # Protected methods referenced from Base class | ||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 6 | ▶ | sub _remove { | ||||||||||||||||||||||||||||||||
|
Subroutine
_remove: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 99 | 6 | my($self, $keys) = @_; | |||||||||||||||||||||||||||||||||
| 100 | 6 | foreach(@$keys) | |||||||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||||||||
| 102 | 11 | TRACE("clearing $_"); | |||||||||||||||||||||||||||||||||
| 103 | 11 | delete $self->{cache}{$_}; | |||||||||||||||||||||||||||||||||
| 104 | 11 | delete $self->{modified}{$_}; | |||||||||||||||||||||||||||||||||
| 105 | 11 | delete $self->{accessed}{$_}; | |||||||||||||||||||||||||||||||||
| 106 | 11 | delete $self->{'stat'}{$_}; | |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 10 | ▶ | sub _accessed { | ||||||||||||||||||||||||||||||||
|
Subroutine
_accessed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 111 | 10 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 112 | 10 | my %atimes = %{$self->{accessed}}; | |||||||||||||||||||||||||||||||||
| 113 | 10 | return \%atimes; | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub _stale { | ||||||||||||||||||||||||||||||||
|
Subroutine
_stale: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||
| 117 | 1 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 118 | 1 | my $modified = $self->{modified}; | |||||||||||||||||||||||||||||||||
| 119 | return grep { | ||||||||||||||||||||||||||||||||||
| 120 | 1 | my $src = (stat($_))[9]; | |||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | (!defined $src) || ($src ne $modified->{$_}) | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
!defined($src) || $src ne $modified->{$_}
MC/DC0%0/2
!defined($src) || $src ne $modified->{$_}
!defined($src)
$src ne $modified->{$_}
Truth table33%1/3
not(A) || B
A= !defined($src)B= $src ne $modified->{$_}
| |||||||||||||||||||||||||||||||||||
| 122 | } keys %$modified; | ||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||||||||
| 126 | # Log::Trace stubs | ||||||||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | sub TRACE {} | ||||||||||||||||||||||||||||||||||
| 130 | sub DUMP {} | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||
| 135 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | Cache::AgainstFile::Memory - cache data parsed from files in memory | ||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | use Cache::AgainstFile; | ||||||||||||||||||||||||||||||||||
| 142 | my $cache = new Cache::AgainstFile( | ||||||||||||||||||||||||||||||||||
| 143 | \&loader, | ||||||||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||||||||
| 145 | Method => 'Memory', | ||||||||||||||||||||||||||||||||||
| 146 | MaxItems => 16, | ||||||||||||||||||||||||||||||||||
| 147 | # ... | ||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | $data = $cache->get($filename); | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | Data structures parsed from files are cached in memory. | ||||||||||||||||||||||||||||||||||
| 157 | This is particularly suited to persistent environments such as modperl or other daemon processes. | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | For short-lived processes such as CGI scripts, the Storable backend might be more appropriate. | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | Note that the C<size()> method uses Devel::Size if available, otherwise it returns undef. | ||||||||||||||||||||||||||||||||||
| 162 | Devel::Size can consume a reasonable amount of memory working out how much memory you are using! | ||||||||||||||||||||||||||||||||||
| 163 | This memory is released after the operation but it will have expanded your process' memory footprint in the process. | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =over 4 | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | =item Grace | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | How long to defer statting the file (in seconds). | ||||||||||||||||||||||||||||||||||
| 172 | Be careful if you use this in modperl environments as it will result in some children having a | ||||||||||||||||||||||||||||||||||
| 173 | new version of the cached item, and some still having the old version. | ||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | =item NoStat | ||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||
| 177 | Don't stat files to validate the cache - items are served from the cache until they are purged. | ||||||||||||||||||||||||||||||||||
| 178 | Valid values are 0|1 (default=0, i.e. files are statted) | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | Setting this to 1 is equivalent to setting Grace to an infinite value. | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | =item MaxATime | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | Purge items older than this. | ||||||||||||||||||||||||||||||||||
| 185 | Value is in seconds (default=undefined=infinity) | ||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||
| 187 | =item MaxItems | ||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||
| 189 | Purge oldest items to reduce cache to this size. | ||||||||||||||||||||||||||||||||||
| 190 | Value should be an integer (default=undefined=infinity) | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | =back | ||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||
| 194 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||
| 196 | $Revision: 1.15 $ on $Date: 2005/11/10 15:13:57 $ by $Author: johna $ | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | John Alden & Piers Kent <cpan _at_ bbc _dot_ co _dot_ uk> | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||
| 206 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||||||||