Cache
view release on metacpan or search on metacpan
lib/Cache/File/Entry.pm view on Meta::CPAN
=head1 NAME
Cache::File::Entry - An entry in the file based implementation of Cache
=head1 SYNOPSIS
See 'Cache::Entry' for a synopsis.
=head1 DESCRIPTION
This module implements a version of Cache::Entry for the Cache::File variant
of Cache. It should not be created or used directly, please see
'Cache::File' or 'Cache::Entry' instead.
=cut
package Cache::File::Entry;
require 5.006;
use strict;
use warnings;
use Cache::File;
use File::Spec;
use File::Path;
use File::Temp qw(tempfile);
use Fcntl qw(LOCK_EX LOCK_SH LOCK_NB);
use File::NFSLock;
use Symbol ();
use Carp;
use base qw(Cache::Entry);
use fields qw(dir path lockdetails);
our $VERSION = '2.11';
# hash of locks held my the process, keyed on path. This is useful for
# catching potential deadlocks and warning the user, and for implementing
# LOCK_NONE (which still needs to do some synchronization). Each entry will
# be an hash of { lock, type, count, lock, lockfh, linkcount }. The
# filehandle and link count is for checking when the lock has been released by
# another process.
my %PROCESS_LOCKS;
sub new {
my Cache::File::Entry $self = shift;
$self = fields::new($self) unless ref $self;
$self->SUPER::new(@_);
# get file path and store full path and containing directory
my ($dir, $file) = $self->{cache}->cache_file_path($self->{key});
$self->{dir} = $dir;
$self->{path} = File::Spec->catfile($dir, $file);
return $self;
}
sub exists {
my Cache::File::Entry $self = shift;
# ensure pending expiries are removed
$self->{cache}->purge();
return -e $self->{path};
}
sub _set {
my Cache::File::Entry $self = shift;
my ($data, $expiry) = @_;
$self->_make_path() or return;
my ($fh, $filename) = tempfile('.XXXXXXXX', DIR => $self->{dir});
binmode $fh;
print $fh $data;
close($fh);
my $time = time();
my $cache = $self->{cache};
my $key = $self->{key};
# lock indexes
$cache->lock();
my $exists = -e $self->{path};
my $orig_size;
unless ($exists) {
# we're creating the entry
$cache->create_entry($key, $time);
$cache->change_count(1);
$orig_size = 0;
}
# only remove current size if there is no active write handle
elsif ($self->_trylock(LOCK_SH)) {
$orig_size = $self->size();
( run in 0.506 second using v1.01-cache-2.11-cpan-e1769b4cff6 )