A synchronized data structure (map) for ipc
use Arch::SharedIndex; my $map = Arch::SharedIndex->new(file => "/tmp/logintimes.idx"); my $time = time; $map->store(migo => $time - 75, bob => $time - 5, enno => $time); printf "All users: %s, %s, %s\n", $map->keys; printf "New users: %s\n", $map->grep(sub { $_[1] == $time }); printf "Login time of migo: %s\n", $map->fetch('migo'); $map->update(sub { $_[1] + 10 }, sub { $_[1] == $time }); $map->store(migo => $time); $map->delete('bob'); printf "Logged users with times: (%s)\n", join(", ", $map->hash);
Arch::SharedIndex provides a key-value map that can be shared and accessed safely by multiple processes.
The following methods are available:
new, encode_value, decode_value, store_value, fetch_value, delete_value, store, fetch, delete, fetch_store, keys, values, hash, list, grep, filter, update, query_index_list.
Create a new index object. option is a hash of parameters.
The path of the index file, used to store data. Must not be omitted.
Whether the index file is automatically created. Defaults to 1.
Maximum number of entries in the index. Defaults to 0 (no limit).
Timeout in seconds after which unused entries are removed. Defaults to 0 (don't expire entries)
Whether fetching values resets the entry expiration timeout. Defaults to 1 if max_size is set, 0 otherwise.
Whether non-scalar perl data can be stored. If true, values are encoded using Data::Dumper.
Indent value for Data::Dumper when perl_data is set. Defaults to 0.
Pair value for Data::Dumper when perl_data is set. Defaults to \*(C`=>\*(C'.
Encode the value referenced by ref in a string representation. The encoding is done in place.
Decode a value encoded with encode_value from its string representation. The decoding is done in place.
Store a value for the given key and token. Create a new token if none is given. Returns the (new) token. Sub-classes should implement this method.
Fetch the value stored for the given key and token. Sub-classes should implement this method.
Delete a value stored for the given key and value. Sub-classes should implement this method.
Store a set of key-value pairs. kvp may either be a reference to a hash or array, or list of keys and values.
Fetch values stored for a list of keys. keys may either be an array reference, or a list of keys.
Delete values stored for a list of keys. keys may either be an array reference, or a list of keys.
This is an optimized (fetch or store) in a single step. Fetch values stored for keys, just like fetch, but store values for the missing keys in the process. keys may be an array reference or a list of keys. mapfunc will be called once for every key in keys that has no associated value, with the key as its only argument. Its return value will be stored for that key.
Returns a list of all valid keys. In scalar context, returns an array reference. Keys are returned in no particular order, but values will return values in matching order if the index has not been changed between calls.
Returns a list of all stored values. In scalar context, returns an array reference. Values are returned in no particular order, but keys will return values in matching order if the index has not been changed between calls.
Returns the stored keys and values as a perl hash. In scalar context, returns hash reference.
Returns the stored keys and values as a list of pairs (array references with two elements each). In scalar context, returns an array reference.
Returns a list of keys for which predicate returns a true value. predicate is called once for every key, with the key and the stored value as its first and second argument.
Deletes every entry for which predicate returns a true value. predicate is called once for every key, with the key and the stored value asi its first and second argument.
Updates the value for every key for which predicate returns a true value with the return value from mapfunc. Both predicate and mapfunc are called with the key and the stored values as their first and second argument.
Synchronize access and call code with a reference to a list of pairs, each containing the key and token, for every stored value. Used internally by store, fetch, delete, fetch_store, keys, values, hash, list, grep, filter and update.
Awaiting for your reports.
Mikhael Goikhman ([email protected]\*(--Perl-GPL/arch-perl\*(--devel).
Enno Cramer ([email protected]/arch-perl\*(--devel).
For more information, see Arch::SharedCache.