Cache-AgainstFile

 view release on metacpan or  search on metacpan

lib/Cache/AgainstFile/CacheModule.pm  view on Meta::CPAN

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                ($last_modified, $data) = @$record;
                unless($self->{options}->{NoStat})
                {
                        my $grace = $self->{options}->{Grace} || 0;
                        my $last_checked = $self->{'stat'}{$filename} || 0;
         
                        #Are we within the grace period since our last stat?
                        unless($grace > time - $last_checked)
                        {
                                TRACE("stat: $filename");
                                $data = undef if((stat($filename))[9] > $last_modified); #stat and maybe mark as stale
                                $self->{'stat'}{$filename} = time;
                        }
                }
        }
        unless(defined $data)
        {
                TRACE("stale: $filename");
                $data = $self->{loader}->($filename, @opts);
                $last_modified = (stat($filename))[9];
                $self->_set($filename, [$last_modified, $data]);
        }
        else
        {
                TRACE("not stale: $filename");
        }
        return $data;  
}
 
#Forward all other methods to backend

lib/Cache/AgainstFile/Memory.pm  view on Meta::CPAN

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
my $source_mtime;
unless ($self->{options}->{NoStat} && !$stale)
{
        #Are we within the grace period since our last stat?
        my $grace = $self->{options}->{Grace} || 0;
        my $last_checked = $self->{'stat'}{$filename} || 0;
        unless($grace > time - $last_checked)
        {
                TRACE("stat: $filename");
                check_safe($filename, "r") if(HAVE_FILE_POLICY);
                $source_mtime = (stat($filename))[9];
                my $last_modified = $self->{modified}{$filename};
                $stale |= (!defined $source_mtime) || (!defined $last_modified) || ($source_mtime ne $last_modified);
                $self->{'stat'}{$filename} = time;
        }
}
my $data;
if($stale)
{
        TRACE("stale memory: $filename");
        $data = $self->{loader}->($filename, @opts);

lib/Cache/AgainstFile/Memory.pm  view on Meta::CPAN

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
sub _accessed {
        my($self) = @_;
        my %atimes = %{$self->{accessed}};
        return \%atimes;
}
 
sub _stale {
        my($self) = @_;
        my $modified = $self->{modified};
        return grep {
                my $src = (stat($_))[9];
                (!defined $src) || ($src ne $modified->{$_})
        } keys %$modified;
}
 
#
# Log::Trace stubs
#
 
sub TRACE {}
sub DUMP {}

lib/Cache/AgainstFile/Storable.pm  view on Meta::CPAN

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        my $cache_fh = new FileHandle;
        check_safe($cache_filename,"r") if(HAVE_FILE_POLICY);
        unless ($cache_fh->open($cache_filename)) {
                undef $cache_fh;
                $stale = 1;
        }      
 
        # Compare file mtimes to check staleness               
        my $file_mtime;
        unless ($self->{options}->{NoStat} && !$stale) {
                $file_mtime = (stat($filename))[9];
                my $cache_mtime = ($cache_fh->stat)[9] if $cache_fh;
                $stale = (!defined $file_mtime) || (!defined $cache_mtime) || ($file_mtime != $cache_mtime);
        }
        TRACE("Cache " . ($stale?"is":"is not") . " stale");
 
        #Read from cache
        my $data;
        if (!$stale) {
                $data = eval { $self->{read}->($cache_filename, $cache_fh) };
                if ($@) {
                        warn "Storable couldn't retrieve $cache_filename: $@";
                        $stale = 1;
                }
        }
        $cache_fh->close if $cache_fh;
         
        #Write to cache
        if ($stale) {
                TRACE("writing cache");
                $data = $self->{loader}->($filename, @opts);
                $file_mtime = (stat($filename))[9] unless(defined $file_mtime); #Need mtime now
                $self->{write}->($cache_filename, $data, $file_mtime);
        }
        return $data;
}
 
 
sub count {
        my ($self) = shift;
        my $files_in_cache = $self->_cache_files;
        return scalar @$files_in_cache;

lib/Cache/AgainstFile/Storable.pm  view on Meta::CPAN

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        }
}
 
sub _accessed {
        my($self) = @_;
        my $cache_dir = $self->{options}{CacheDir};
        my $files_in_cache = $self->_cache_files;
        my %accessed = map
        {
                my $cache_file = catfile($cache_dir, $_);
                $self->_cache2filename($_) => (stat($cache_file))[8]
        }
        @$files_in_cache;
        return \%accessed;
}
 
sub _stale {
        my($self) = @_;
        my $cache_dir = $self->{options}{CacheDir};
        my $files_in_cache = $self->_cache_files;
        my @out =

t/CacheAgainstFile-Memory.t  view on Meta::CPAN

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
touch($filename);
 
 
############################################################################
# Memory caching
############################################################################
my $mem_cache = new Cache::AgainstFile($callback, {Method => 'Memory'});
ASSERT($mem_cache->{method} eq 'Memory', "Memory backend");
test_basics($mem_cache, $filename, undef, $size_not_available);
$mem_cache = new Cache::AgainstFile($callback, {Method => 'Memory', 'NoStat' => 1});
test_nostat($mem_cache, $filename);
$mem_cache = new Cache::AgainstFile($callback, {Method => 'Memory', 'MaxItems' => 3});
test_max_items($mem_cache, $dir, 3);
$mem_cache = new Cache::AgainstFile($callback, {Method => 'Cache::AgainstFile::Memory', 'MaxATime' => (2.5 * SLEEP_INT)}); #fq backend
test_old_items($mem_cache, $dir, 5, 3);
 
############################################################################
# Clean up
############################################################################
END {
        TRACE("Cleaning up $dir");

t/CacheAgainstFile-Storable.t  view on Meta::CPAN

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
goto trans if($opt_d);
 
############################################################################
### Disk caching
############################################################################
my $disk_cache = new Cache::AgainstFile($callback, {Method => 'Storable', 'CacheDir' => $cache_dir});
ASSERT($disk_cache->{method} eq 'Storable', "Storable caching");
test_basics($disk_cache, $filename);
$disk_cache = new Cache::AgainstFile($callback, {Method => 'Storable', 'CacheDir' => $cache_dir."/", 'NoStat' => 1, Locking => 'Flock'});
test_nostat($disk_cache, $filename);
$disk_cache = new Cache::AgainstFile($callback, {Method => 'Storable', 'CacheDir' => $cache_dir, 'MaxItems' => 2, Locking => 'AtomicWrite'});
test_max_items($disk_cache, $dir, 2);
 
#delete the cache dir so we have to create it
rmtree($cache_dir);
$disk_cache = new Cache::AgainstFile($callback, {Method => 'Cache::AgainstFile::Storable', 'CacheDir' => $cache_dir, 'MaxATime' => (2.5 * SLEEP_INT)}); #fq backend
rmtree($cache_dir); #delete underneath cache
ASSERT(DIED(sub {
        $disk_cache->count()
}) && $@ =~ /unable to open directory/, "failed dir read raises exception");

t/CacheAgainstFile.lib  view on Meta::CPAN

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#Touch the file
sleep( SLEEP_INT );
touch( $filename );
$callcount = 0;
ASSERT($cache->get($filename) eq "filename:$filename" && $callcount == 1, "$method: touch => stat & cache miss (call count $callcount)");
 
#Sneak a sourcefile change in through vulnerable window
sleep (SLEEP_INT);
$callcount = 0;
touch ($filename);
$m_before = (stat($filename))[9];
$loaddelay = 5;
ASSERT($cache->get($filename) eq "filename:$filename" && $callcount == 1, "$method: touch => stat & cache miss (call count $callcount)");
# "While we were calculating", the sourcefile was modified
$m_before++;
(utime $m_before, $m_before, $filename) or die "couldn't posttouch $filename: $!";
$callcount = 0;
$cache->get($filename);
ASSERT($callcount == 1, "window-of-vulnerability check");
$loaddelay = 0;



( run in 0.326 second using v1.01-cache-2.11-cpan-00829025b61 )