NAME

Data::Tubes::Util::Cache

DESCRIPTION

Simple class for handling caching. The interface is compatible with that of CHI for methods used by "cache" in Data::Tubes::Plugin::Plumbing, so it is provided as a simple replacement in case you don't have, or don't want to use, CHI.

The class implements both an in-memory and a on-filesystem caches, depending on what you set as parameter repository. A hash reference means caching in memory; a directory name means caching on file (the directory will be created if not existent).

The class is realized via Mo. You can extend it, to a certain degree.

CLASS METHODS

new

my cache = Data::Tubes::Util::Cache->new(%args);

constructor for the class. See "ACCESSOR METHODS" for the accepted arguments.

ACCESSOR METHODS

repository

$cache->repository($href);
$href = $cache->repository();

set/retrieve the repository for the cache. It can be either of:

  • an hash reference, in which case the cache is kept in memory;

  • a directory name, in which case the cache is kept in the filesystem.

Defaults to an empty hash.

max_items

$cache->max_items($n);
$n = $cache->max_items();

set/retrieve the maximum numbers of elements you want to store in the cache. When set to a false value, the limitation is disabled.

Used by "purge".

BUSINESS LOGIC METHODS

get

$item = $cache->get($key, @args);

get the item related to $key, taking it from the cache. Any additional argument in @arg is ignored. Returns undef if the $key is missing.

purge

$cache->purge();

cleanup the cache. If the value of "max_items" is not true, this method does nothing. Otherwise, elements are removed from the cache until the number of items is less than, or equal to, the maximum number set.

set

$cache->set($key, $value, @args);

set the $value corresponding to the $key in the cache, overriding any previous value or creating a new item. Any additional argument in @args is ignored. Returns $value.

SEE ALSO

Data::Tubes is a valid entry point of all of this.

CHI for a comprehensive caching library.

AUTHOR

Flavio Poletti <polettix@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2016 by Flavio Poletti <polettix@cpan.org>

This module is free software. You can redistribute it and/or modify it under the terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.