Cache
view release on metacpan or search on metacpan
lib/Cache/File.pm view on Meta::CPAN
if ($self->{locklevel} == LOCK_NONE) {
$self->{lock} = 1;
}
else {
# TODO: implement LOCK_LOCAL
my $oldmask = umask $self->cache_umask();
my $lock = File::NFSLock->new({
file => $self->{lockfile},
lock_type => LOCK_EX | ($tryonly? LOCK_NB : 0),
stale_lock_timeout => $STALE_LOCK_TIMEOUT,
});
umask $oldmask;
unless ($lock) {
$tryonly and return 0;
die "Failed to obtain lock on lockfile '".$self->{lockfile}."': ".
$File::NFSLock::errstr."\n";
}
$self->{lock} = $lock;
}
$self->{lockcount} = 1;
return 1;
}
sub trylock {
my Cache::File $self = shift;
return $self->lock(1);
}
sub unlock {
my Cache::File $self = shift;
$self->{lock} or croak "not locked";
return unless --$self->{lockcount} == 0;
# close heaps and save counts
$self->{openexp} = undef;
$self->{openage} = undef;
$self->{openuse} = undef;
$self->{openidx} = undef;
# unlock
$self->{lock}->unlock unless $self->{locklevel} == LOCK_NONE;
$self->{lock} = undef;
}
sub create_entry {
my Cache::File $self = shift;
my ($key, $time) = @_;
my $ageheap = $self->get_age_heap();
$ageheap->add($time, $key);
my $useheap = $self->get_use_heap();
$useheap->add($time, $key);
$self->set_index_entries($key, { age => $time, lastuse => $time });
}
sub update_last_use {
my Cache::File $self = shift;
my ($key, $time) = @_;
my $index_entries = $self->get_index_entries($key)
or warnings::warnif('Cache', "missing index entry for $key");
my $useheap = $self->get_use_heap();
$useheap->delete($$index_entries{lastuse}, $key);
$useheap->add($time, $key);
$$index_entries{lastuse} = $time;
$self->set_index_entries($key, $index_entries);
}
sub change_count {
my Cache::File $self = shift;
my ($count) = @_;
my $index = $self->get_index();
my $oldcount = $$index{$COUNT_KEY};
$$index{$COUNT_KEY} = $oldcount? $oldcount + $count : $count;
}
sub change_size {
my Cache::File $self = shift;
my ($size) = @_;
my $index = $self->get_index();
my $oldsize = $$index{$SIZE_KEY};
$$index{$SIZE_KEY} = $oldsize? $oldsize + $size : $size;
$self->check_size($$index{$SIZE_KEY}) if $size > 0;
}
sub get_index_entries {
my Cache::File $self = shift;
my ($key) = @_;
my $index = $self->get_index();
my $index_entry = $$index{$key}
or return undef;
my $index_entries = Storable::thaw($index_entry);
$$index_entries{age} and $$index_entries{lastuse}
or warnings::warnif('Cache', "invalid index entry for $_");
return $index_entries;
}
sub set_index_entries {
my Cache::File $self = shift;
my $key = shift;
my $index_entries = $#_? { @_ } : shift;
$$index_entries{age} and $$index_entries{lastuse}
or croak "failed to supply age and lastuse for index update on $key";
my $index = $self->get_index();
$$index{$key} = Storable::nfreeze($index_entries);
}
sub get_index {
my Cache::File $self = shift;
( run in 0.553 second using v1.01-cache-2.11-cpan-e1769b4cff6 )