Cache-Static
view release on metacpan or search on metacpan
0.95 2006/01/17 16:05:58 GMT
-----------------------------
- extraneous/duplicate log message cleanup
- allow directories to disappear from underneath us (call _mkdir_p more)
- recursed components are now cached for top level deps
- set, get_if_same, (and _get, _is_same) allow overriding namespace
from init() e.g.:
function(... args ...) => function(... args ..., namespace => $ns)
- all directories created under $ROOT now default to mode 777,
and all files created are chmod'd to mode 666
this makes things work, but is not the right way, long term...
- init() now re-loads global AND per-namespace configs
- added support for namespaces, per-namespace configs
- stopped overloading .dep files. timestamp files (for extensions)
are now in $ROOT/timestamps, with a .ts extension - note carefully
that these are NOT namespace dependent, so an update is global, as
they would be with a modification of a depended file.
- move .dep files back into cache/ dir with .dep extension
(saves excess directories, easier to navigate)
- md5_path() no longer translates '+' to '-', this works around a
$key = make_key($key) unless($key isa Cache::Static::Key);
you should get the same results regardless of if you called make_key,
but calling make_key and saving the results saves you one MD5 lookup
(think about memoization tho)
ALSO - it'd be nice to have the deps encoded in the key object too (?)
THINK: do we want to expose is_same?
every other function should be underscored!!!
-- get a better solution for permission stuff.
making everything 777/666 is not right.
plus all the extra chmod calls (which are slow and a race condition)
is there umask in perl? YES, see perldoc -f umask
-- lock around set (EXCL) / get (LOCK_SH)
actually, need to lock around basically everything in HMU::cache_it...
but this should not be hacked in, should be an API level change...
get_if_same_or_lock() ... set() ?
probably do this:
get_if_same():
take an optional lock argument (default to what conf says)
set():
lib/Cache/Static.pm view on Meta::CPAN
}
############################
### /glue for extensions ###
############################
#try to set up the logfile with lenient permissions
eval {
open(FH, ">>$LOGFILE");
close(FH);
chmod 0666, $LOGFILE;
};
#number of levels of directory in cache
#TODO: move this to config file
my $CACHE_LEVELS = 3;
sub get_if_same {
### uncomment the below line to disable Cache::Static
# return undef;
my ($key, $depsref, %args) = @_;
lib/Cache/Static.pm view on Meta::CPAN
#if we overrode the namespace, or if the dir got rm -rf'd out
#from under us, this comes in handy...
_mkdir_p("$ROOT/$ns/tmp");
#write out the content
my $tmpf = $key;
$tmpf =~ s/\///g;
open(FH, ">$ROOT/$ns/tmp/$tmpf") || die "couldn't open $ROOT/$ns/tmp/$tmpf: $!";
(print FH $content) || die "couldn't print: $!";
close(FH) || die "couldn't close: $!";
chmod 0666, "$ROOT/$ns/tmp/$tmpf";
#move the new cache file in place
(rename "$ROOT/$ns/tmp/$tmpf", "$ROOT/$ns/cache/$key") ||
die "couldn't rename content to $ROOT/$ns/cache/$key";
if($deps) {
#write out the deps
my $frozen_deps = join('', map { $a=$_; $a.="\n"; $a } @$deps);
open(FH, ">$ROOT/$ns/tmp/$tmpf.dep") || die "couldn't open: $!";
(print FH $frozen_deps) || die "couldn't print: $!";
close(FH) || die "couldn't close: $!";
chmod 0666, "$ROOT/$ns/tmp/$tmpf.dep";
#move the new .dep file in place
(rename "$ROOT/$ns/tmp/$tmpf.dep", "$ROOT/$ns/cache/$key.dep") ||
die "couldn't rename deps to $ROOT/$ns/cache/$key.dep: $!";
}
}; if($@) {
_log(2, "Cache::Static::set couldn't save new value (in namespace: $ns) : $@");
} else {
_log(3, "Cache::Static::set refreshed $key in namespace: $ns");
lib/Cache/Static.pm view on Meta::CPAN
#mkdir -p
_mkdir_p($dir);
die "couldn't make/walk directories: $@" if($@);
#touch/write to the file
open(FH, ">$file") || die "couldn't open $file: $!";
if($output) {
print FH $output || die "couldn't print $output to $file: $!";
}
close(FH) || die "couldn't close $file: $!";
chmod 0666, $file;
}; if($@) {
_log(2, "Cache::Static::_mkdirs_and_touch: couldn't update timestamps: $@");
}
}
sub _log {
my $severity = shift;
return unless($severity <= $CONF{log_level});
my $args = join(' ', @_);
$args =~ s/\n/ /mg;
lib/Cache/Static.pm view on Meta::CPAN
}
sub _mkdir_p {
my $dir = shift;
my @dirs = grep (/./, split(/\//, $dir));
my $dir_so_far = '/';
foreach my $d (@dirs) {
$dir_so_far .= "$d/";
unless(-e $dir_so_far) {
mkdir($dir_so_far) || die "couldn't create $dir_so_far: $!";
chmod(0777, $dir_so_far) || die "couldn't change perms on $dir_so_far: $!";
}
}
}
1;
__END__
=head1 NAME
( run in 0.302 second using v1.01-cache-2.11-cpan-8d75d55dd25 )