The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Module::Generic::File::Mmap - MMap File Class

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" );

VERSION

    v0.1.2

Module::Generic::File::Mmap implements a Mmap cache mechanism using Cache::FastMmap, which is an XS module. The api is very similar to its counterpart with Module::Generic::File::Cache and Module::Generic::SharedMem, but has the advantage of sharing data over a file like Module::Generic::File::Cache, but using Mmap and being very fast.

You must have installed separately 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 "open".

This is particularly useful for system that lack support for shared memory cache. See perlport for that.

METHODS

new

This instantiates a shared mmap cache object. It takes the following parameters:

debug

A debug value will enable debugging output (equal or above 3 actually)

cbor

Provided with a value (true or false does not matter), and this will set CBOR::XS as the data serialisation mechanism when storing data to mmap file.

create

A boolean value to indicate whether the shared mmap cache file should be created if it does not exist. Default to false.

destroy

A boolean value to indicate if the shared mmap cache file should be removed when the object is destroyed upon end of the script process.

See perlmod for more about object destruction.

json

Provided with a value (true or false does not matter), and this will set JSON as the data serialisation mechanism when storing data to mmap file

Please note that if you want to store objects, you need to use storable instead, because JSON is not suitable to serialise objects.

key

The shared mmap cache key identifier to use. It defaults to a random one created with "rand"

If you provide an empty value, it will revert to one created with "rand".

If you provide a number, it will be used to call "ftok".

Otherwise, if you provide a key as string, the characters in the string will be converted to their numeric value and added up. The resulting id will be used to call "ftok" and will produce a unique and repeatable value.

Either way, the resulting value is used to create a shared mmap cache file by "open".

mode

The octal mode value to use when opening the shared mmap cache file.

Shared cache files are owned by system users and access to shared mmap cache files is ruled by the initial permissions set to it.

If you do not want to share it with any other user than yourself, setting mode to 0600 is fine.

sereal

Provided with a value (true or false does not matter), and this will set Sereal as the data serialisation mechanism when storing data to mmap file

size

The size in byte of the shared mmap cache.

This is set once it is created. You can create again the shared mmap cache file with a smaller size. No need to remove it first.

storable

Provided with a value (true or false does not matter), and this will set Storable::Improved as the data serialisation mechanism when storing data to mmap file

An object will be returned if it successfully initiated, or undef() upon error, which can then be retrieved with "error" in Module::Generic inherited by Module::Generic::SharedMem. You should always check the return value of the methods used here for their definedness.

    my $shmem = Module::Generic::SharedMem->new(
        create => 1,
        destroy => 0,
        key => 'my_memory',
        # 64K
        size => 65536,
        storable => 1,
    ) || die( Module::Generic::SharedMem->error );

cache_file

Sets or gets the underlying cache file to use.

cbor

When called, this will set CBOR::XS as the data serialisation mechanism when storing data to mmap cache or reading data from mmap cache.

create

Boolean value. If set, this will have "open" create the cache file if it does not already exists.

delete

Removes the cache file ad returns true upon success, or sets an error and return undef upon error.

destroy

Boolean value. If true, the cache file will be removed when this objects is destroyed by perl upon clean-up.

exclusive

Boolean value. Sets whether there should be an exclusive access to the cache file. This is currently not used.

exists

Returns true if the cache file exists, or false otherwise.

flags

Provided with an optional hash or hash reference and this return a bitwise value of flags used by "open".

    my $flags = $cache->flags({
        create => 1,
        exclusive => 0,
        mode => 0600,
    }) || die( $cache->error );

ftok

This attempts to be a polyfil for "ftok" in POSIX and provided with some digits, this returns a steady and reproducible serial that serves as a base file name for the cache file.

has_xs

Read-only. Returns true if the XS module Cache::FastMmap is installed on your system and false otherwise.

id

Returns the id or serial of the cache file set after having opened it with "open"

json

When called, this will set JSON as the data serialisation mechanism when storing data to cache mmap.

key

The key to use to identify the cache file.

This must be unique enough to be different from other cache file and to be shared among other processes.

It returns the value currently set, if any.

lock

This locks the cache file, if any and returns the result from the lock.

If the cache has not been opened first, then this will set an error and return undef.

locked

Returns the boolean value representing the lock state of the cache file.

mode

Set or get the cache file mode to be used by "open"

open

Create an access to the cache mmap file and return a new Module::Generic::File::Mmap object.

    my $cache = Module::Generic::File::Mmap->new(
        create => 1,
        destroy => 0,
        # If not provided, will use the one provided during object instantiation
        key => 'my_cache',
        # 64K
        size => 65536,
    ) || die( Module::Generic::File::Mmap->error );
    # Overriding some default value set during previous object instantiation
    my $c = $cache->open({
        mode => 0600,
        size => 1024,
    }) || die( $cache->error );

If the "create" option is set to true, but the cache file already exists, "open" will detect it and attempt to open access to the cache file without the "create" bit on.

owner

Sets or gets the cache file owner, which is by default actually the process id ($$)

rand

Get a random key to be used as identifier to create a shared mmap cache.

read

Read the content of the shared mmap cached and decode the data read using JSON, CBOR, Sereal or "thaw" in Storable depending on your serialiser of choice upon either object instantiation or upon using the methods "json" or "storable"

By default, if no serialiser is specified, it will default to storable.

You can optionally provide a buffer, and a maximum length and it will read that much length and put the shared mmap cache content decoded in that buffer, if it were provided.

It then return the length read, or 0E0 if no data was retrieved. 0E0 still is treated as 0, but as a positive value, so you can do:

    my $len = $cache->read( $buffer ) || die( $cache->error );

But you really should more thoroughly do instead:

    my( $len, $buffer );
    if( !defined( $len = $cache->read( $buffer ) ) )
    {
        die( $cache->error );
    }

If you do not provide any buffer, you can call "read" like this and it will return you the shared mmap cache decoded content:

    my $buffer;
    if( !defined( $buffer = $cache->read ) )
    {
        die( $cache->error );
    }

The content is stored in shared mmap cache after being encoded with "freeze" in Storable.

remove

Remove entire the shared mmap cache identified with "key"

removed

Returns true if the shared mmap cache was removed, false otherwise.

reset

Reset the shared mmap cache value. If a value is provided, it will be used as the new reset value, othewise an empty string will be used.

sereal

When called, this will set Sereal as the data serialisation mechanism when storing data to cache mmap.

serial

Returns the serial number used to create or access the shared mmap cache.

This serial is created based on the key parameter provided either upon object instantiation or upon using the "open" method.

The serial is created by calling "ftok" to provide a reliable and repeatable numeric identifier. "ftok" is a simili polyfill of "ftok" in IPC::SysV

serialiser

Sets or gets the serialiser. Possible values are: cbor, json, sereal, storable

size

Sets or gets the shared mmap cache size.

This should be an integer representing bytes, so typically a multiple of 1024.

This has not much effect, except ensuring there is enough space on the filesystem for the cache and that whatever data is provided does not exceed that threshold.

stat

Sets or retrieve value with "stat" in Module::Generic::File for the underlying cache file.

It returns a Module::Generic::SemStat object.

storable

When called, this will set Storable::Improved as the data serialisation mechanism when storing data to cache mmap.

supported

Returns always true as cache file relies solely on file.

unlock

Remove the lock, if any. The shared mmap cache must first be opened.

    $cache->unlock || die( $cache->error );

write

Write the data provided to the shared mmap cache, after having encoded it using JSON, CBOR, Sereal or "freeze" in Storable depending on your serialiser of choice. See "json", "cbor", "sereal" and "storable" and more simply "serialiser"

By default, if no serialiser is specified, it will default to storable.

You can store in shared mmap cache any kind of data excepted glob, such as scalar reference, array or hash reference. You could also store module objects, but JSON only supports encoding objects that are based on array or hash. As the JSON documentation states "other blessed references will be converted into null"

It returns the current object for chaining, or undef if there was an error, which can then be retrieved with "error" in Module::Generic

SERIALISATION

Serialisation by CBOR, Sereal and Storable::Improved (or the legacy Storable) is supported by this package. To that effect, the following subroutines are implemented: FREEZE, THAW, STORABLE_freeze and STORABLE_thaw

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Module::Generic::SharedMem, Cache::File, File::Cache, Cache::FileCache

JSON, Storable

COPYRIGHT & LICENSE

Copyright(c) 2022 DEGUEST Pte. Ltd.

All rights reserved This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.