Cache-AgainstFile
view release on metacpan or search on metacpan
lib/Cache/AgainstFile/CacheModule.pm view on Meta::CPAN
474849505152535455565758596061626364656667686970717273747576
(
$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
434445464748495051525354555657585960616263my
$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
110111112113114115116117118119120121122123124125126127128129130sub
_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
7475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
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
139140141142143144145146147148149150151152153154155156157158159
}
}
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
424344454647484950515253545556575859606162touch(
$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
404142434445464748495051525354555657585960goto
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
686970717273747576777879808182838485868788#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 )