Module:...::Cache(3)
okay so this is the manual page for Module::Generic::File::Cache(3), from User Contributed Perl Documentation, and bestieβ¦ itβs giving file-based cache realness πΎβ¨
π NAME π
Module::Generic::File::Cache - File-based Cache. Thatβs it, thatβs the name, no cap π
β¨ SYNOPSIS β¨
okay so you load it and make a cache like this, literally:
use Module::Generic::File::Cache;
my $cache = Module::Generic::File::Cache->new(
key => 'something',
create => 1,
mode => 0666,
base64 => 1,
# Could also be CBOR, Storable::Improved
serialiser => 'Sereal',
) || die( Module::Generic::File::Cache->error, "\n" );
obsessed with a constructor that also tells you the error if it flops π
β VERSION β
v0.3.0 β the version, periodt π«Ά
π¦ DESCRIPTION π¦
so this module gives you a file-based shared space that can be shared across processes. lowkey iconic.
it behaves like Module::Generic::SharedMem, but instead of using a shared memory block that requires IPC::SysV, it uses a file. yes bestie, a whole file, and it ate π₯
this is particularly useful for systems that lack support for shared cache. see perlport for that β iykyk π
π METHODS π
π§ new
Okay so new is the instantiate-a-shared-cache-object moment. It takes these parameters:
base64β when set, this tells the serialiser used (see theserialiseroption) to base64 encode and decode the data before writing and after reading. The value can be a simple true value, like1, or a base64 encoder/decoder. The only supported ones rn are:Crypt::MiscandMIME::Base64, or it can also be an array reference of 2 code references β one for encoding and one for decoding.cborβ give it a value (true or false does not matter), and this setsCBOR::XSas the data serialisation mechanism when storing data to cache file.debugβ a debug value enables debugging output (equal or above 3 actually).createβ a boolean value to indicate whether the shared cache file should be created if it does not exist. Defaults to false.destroyβ a boolean value to indicate if the shared cache file should be removed when the object is destroyed upon end of the script process. Peepperlmodfor more about object destruction.jsonβ give it a value (true or false does not matter), and this sets JSON as the data serialisation mechanism when storing data to cache file.
Heads up: if you want to store objects, you need to usecbor,serealorstorableinstead, because JSON is not suitable to serialise objects.
keyβ the shared cache key identifier to use. It defaults to a random one created withrand.- 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 a string, the characters in the string will be converted to their numeric value and added up. The resulting id will be used to call
ftokand will produce a unique and repeatable value. - Either way, the resulting value is used to create a shared cache file by
open.
- If you provide an empty value, it will revert to one created with
modeβ the octal mode value to use when opening the shared cache file. Shared cache files are owned by system users and access to shared 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 to0600is fine.serealβ give it a value (true or false does not matter), and this sets Sereal as the data serialisation mechanism when storing data to cache file.serialiserβ the class name of the serialiser to use. Currently supported ones are:CBOR,Sereal,Storable::Improved(or the legacyStorable).sizeβ the size in byte of the shared cache. This is set once it is created. You can create again the shared cache file with a smaller size. No need to remove it first.storableβ give it a value (true or false does not matter), and this setsStorable::Improvedas the data serialisation mechanism when storing data to cache file.tmpdirβ the temporary directory to use to store the cache files. By default this is the system standard temporary directory.
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 );
π base64
When set, this tells the serialiser used (see serialiser) to base64 encode and decode the data before writing and after reading.
The value can be a simple true value, like 1, or a base64 encoder/decoder. The only supported ones rn are: Crypt::Misc and MIME::Base64, or it can also be an array reference of 2 code references β one for encoding and one for decoding.
πͺ binmode
No description in the original, bestie. It's just a method name standing there with main character energy. π
π cbor
When called, this sets CBOR::XS as the data serialisation mechanism when storing data to cache file or reading data from cache file.
π create
Boolean value. If set, this has open create the cache file if it does not already exist.
π delete
Removes the cache file and returns true upon success, or sets an error and returns undef upon error.
π destroy
Boolean value. If true, the cache file will be removed when this object is destroyed by perl upon clean-up.
π¦ exclusive
Boolean value. Sets whether there should be exclusive access to the cache file. This is currently not used.
β¨ exists
Returns true if the cache file exists, false otherwise.
π₯ flags
Provided with an optional hash or hash reference, this returns 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, returns a steady and reproducible serial that serves as a base file name for the cache file.
π id
Returns the id or serial of the cache file set after having opened it with open.
πΈ json
Sets the data serialising method to JSON.
π«Ά key
The key to use to identify the cache file.
This must be unique enough to be different from other cache files 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 sets an error and returns 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 file and return a new Module::Generic::File::Cache object.
my $cache = Module::Generic::File::Cache->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::Cache->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 ($$).
π pid
This is an alias for owner.
π rand
Get a random key to be used as identifier to create a shared cache.
π read
Read the content of the shared cache and decode the data read using JSON, CBOR, Sereal or thaw in Storable depending on your choice of serialiser upon either object instantiation or upon using the methods json, cbor, sereal or storable or even more simply serialiser.
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 cache content decoded in that buffer, if it were provided.
It then returns 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 cache decoded content:
my $buffer;
if( !defined( $buffer = $cache->read ) )
{
die( $cache->error );
}
The content is stored in shared cache after being encoded with the serialiser of choice.
π₯ remove
Remove entire shared cache identified with key.
β¨ removed
Returns true if the shared cache was removed, false otherwise.
π§ reset
Reset the shared cache value. If a value is provided, it will be used as the new reset value; otherwise an empty string will be used.
π serial
Returns the serial number used to create or access the shared 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 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 retrieves value with stat in Module::Generic::File for the underlying cache file.
It returns a Module::Generic::SemStat object.
β storable
When called, this sets Storable as the data packing mechanism when storing data to memory.
π supported
Returns always true as cache file relies solely on file.
π tmpdir
The temporary directory to use to save the cache file. By default, this will be the system standard temporary directory.
π¦ unlock
Remove the lock, if any. The shared cache must first be opened.
$cache->unlock || die( $cache->error );
π write
Write the data provided to the shared 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 cache any kind of data except 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 β¨
Okay bestie, so this package supports serialisation via CBOR, Sereal, and Storable::Improved β or the legacy Storable, if thatβs your vibe. To make that happen, it implements these subroutines:
FREEZETHAWSTORABLE_freezeSTORABLE_thaw
Thatβs the whole menu β no cap, no extra hidden methods. π
π AUTHOR π
Jacques Deguest <jack@deguest.jp>
π¦ SEE ALSO π¦
Module::Generic::SharedMemCache::FileFile::CacheCache::FileCache
Also: JSON and Storable. Slay. β¨
π COPYRIGHT & LICENSE π
Copyright(c) 2022-2024 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.
And thatβs the whole page, bestie β the footer says itβs from perl v5.42.2, dated 2026-02-15, and it belongs to Module:...::Cache(3). Iconic. Periodt. π
π¦ where this page comes from
- alpine 3.24: perl-module-generic-doc 1.1.3-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms