Module-Generic
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Module/Generic/File/Mmap.pm view on Meta::CPAN
# We use the root as a reliable and stable path.
# I initially though about using __FILE__, but during testing this would be in ./blib/lib and beside one user might use a version of this module somewhere while the one used under Apache/mod_perl2 could be somewhere else and this would render...
my $val = $self->ftok( $id );
return( $val );
}
}
sub FREEZE
{
my $self = CORE::shift( @_ );
my $serialiser = CORE::shift( @_ ) // '';
my $class = CORE::ref( $self );
my %hash = %$self;
CORE::delete( $hash{cache_file} );
if( my $cache = $self->{_cache} )
{
my $file = $self->cache_file;
my $cache_data = $cache->get( $self->key );
$hash{__cache_file} = "$file";
$hash{__cache_data} = $cache_data;
}
CORE::delete( $hash{_cache} );
# Return an array reference rather than a list so this works with Sereal and CBOR
# On or before Sereal version 4.023, Sereal did not support multiple values returned
CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
# But Storable want a list with the first element being the serialised element
CORE::return( $class, \%hash );
}
sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
# NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
# NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
sub THAW
{
my( $self, undef, @args ) = @_;
my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
my $new;
my( $cache_data, $cache_file );
if( CORE::exists( $hash->{__cache_data} ) &&
CORE::exists( $hash->{__cache_file} ) )
{
( $cache_data, $cache_file ) = CORE::delete( @$hash{qw( __cache_data __cache_file )} );
}
# Storable pattern requires to modify the object it created rather than returning a new one
if( CORE::ref( $self ) )
{
foreach( CORE::keys( %$hash ) )
{
$self->{ $_ } = CORE::delete( $hash->{ $_ } );
}
$new = $self;
}
else
{
$new = CORE::bless( $hash => $class );
}
if( CORE::defined( $cache_data ) &&
CORE::defined( $cache_file ) )
{
my $f = Module::Generic::File->new( $cache_file ) || do
{
warn( "Unable to get a file object for \"$cache_file\": ", Module::Generic::File->error, "\n" ) if( $self->_warnings_is_enabled() );
return( $new );
};
$new->{cache_file} = $f;
$new->{_cache} = Cache::FastMmap->new(
( ( defined( $new->{destroy} ) && length( $new->{destroy} ) ) ? ( unlink_on_exit => $new->{destroy} ) : () ),
share_file => "$f",
( $new->{size} ? ( cache_size => $new->{size} ) : () ),
( $new->{mode} ? ( permissions => $new->{mode} ) : () ),
serializer => '',
) || do
{
warn( "Unable to instantiate a Cache::FastMmap object.\n" ) if( $self->_warnings_is_enabled() );
return( $new );
};
$new->{_cache}->set( $new->key => $cache_data );
}
$new->{owner} = $$;
CORE::return( $new );
}
1;
# NOTE: POD
__END__
=encoding utf-8
=head1 NAME
Module::Generic::File::Mmap - MMap File Class
=head1 SYNOPSIS
use Module::Generic::File::Mmap;
my $cache = Module::Generic::File::Mmap->new(
create => 1,
destroy => 1,
key => 'my key',
mode => 0666,
# Other possibilities are: cbor, sereal, storable and json
serialiser => 'sereal',
# 256k
size => 262144,
base64 => 1,
) || die( Module::Generic::File::Mmap->error, "\n" );
=head1 VERSION
v0.1.2
L<Module::Generic::File::Mmap> implements a Mmap cache mechanism using L<Cache::FastMmap>, which is an XS module. The api is very similar to its counterpart with L<Module::Generic::File::Cache> and L<Module::Generic::SharedMem>, but has the advantage...
You must have installed separately L<Cache::FastMmap> for this module to work. If it is not installed, you could still instantiate an object, but you would not be able to get a new object after with L</open>.
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.382 second using v1.00-cache-2.02-grep-82fe00e-cpan-9e6bc14194b )