Koha::Cache - Handling caching of html and Objects for Koha
use Koha::Cache; my $cache = Koha::Cache->new({cache_type => $cache_type, %params}); # see also Koha::Caches->get_instance;
Koha caching routines. This class provides two interfaces for cache access. The first, traditional OO interface provides the following functions:
Create a new Koha::Cache object. This is required for all cache-related functionality.
Routine that checks whether or not a default caching method is active on this object.
$cache->set_in_cache($key, $value, [$options]);
Save a value to the specified key in the cache. A hashref of options may be specified.
The possible options are:
Expiry time of this cached entry in seconds.
The cache object to use if you want to provide your own. It should be an instance of Cache::*
and follow the same interface as Cache::Memcache.
my $value = $cache->get_from_cache($key, [ $options ]);
Retrieve the value stored under the specified key in the cache.
The possible options are:
If set, this will avoid performing a deep copy of the item. This means that it won't be safe if something later modifies the result of the function. It should be used with caution, and could save processing time in some situations where is safe to use it. Make sure you know what you are doing!
The cache object to use if you want to provide your own. It should be an instance of Cache::*
and follow the same interface as Cache::Memcache.
$cache->clear_from_cache($key);
Remove the value identified by the specified key from the default cache.
$cache->flush_all();
Clear the entire default cache.
Koha::Cache also provides a tied interface which enables users to provide a constructor closure and (after creation) treat cached data like normal reference variables and rely on the cache Just Working and getting updated when it expires, etc.
my $cache = Koha::Cache->new(); my $data = 'whatever'; my $scalar = Koha::Cache->create_scalar( { 'key' => 'whatever', 'timeout' => 2, 'constructor' => sub { return $data; }, } ); print "$$scalar\n"; # Prints "whatever" $data = 'somethingelse'; print "$$scalar\n"; # Prints "whatever" because it is cached sleep 2; # Wait until the cache entry has expired print "$$scalar\n"; # Prints "somethingelse" my $hash = Koha::Cache->create_hash( { 'key' => 'whatever', 'timeout' => 2, 'constructor' => sub { return $data; }, } ); print "$$variable\n"; # Prints "whatever"
The gotcha with this interface, of course, is that the variable returned by create_scalar and create_hash is a reference to a tied variable and not a tied variable itself.
The tied variable is configured by means of a hashref passed in to the create_scalar and create_hash methods. The following parameters are supported:
Required. The key to use for identifying the variable in the cache.
Required. A closure (or reference to a function) that will return the value that needs to be stored in the cache.
Optional. A closure (or reference to a function) that gets run to initialize the cache when creating the tied variable.
Optional. Array reference with the arguments that should be passed to the constructor function.
Optional. The cache timeout in seconds for the variable. Defaults to 600 (ten minutes).
Optional. Which type of cache to use for the variable. Defaults to whatever is set in the environment variable CACHING_SYSTEM. If set to 'null', disables caching for the tied variable.
Optional. Boolean flag to allow the variable to be updated directly. When this is set and the variable is used as an l-value, the cache will be updated immediately with the new value. Using this is probably a bad idea on a multi-threaded system. When allowupdate is not set to true, using the tied variable as an l-value will have no effect.
Optional. A closure (or reference to a function) that should be called when the tied variable is destroyed.
Optional. Boolean flag to tell the object to remove the variable from the cache when it is destroyed or goes out of scope.
Optional. Boolean flag to tell the object not to refresh the variable from the cache every time the value is desired, but rather only when the local copy of the variable is older than the timeout.
my $scalar = Koha::Cache->create_scalar(\%params);
Create scalar tied to the cache.
None by default.
Koha::Cache::Object
Chris Cormack, <chris@bigballofwax.co.nz> Paul Poulain, <paul.poulain@biblibre.com> Jared Camins-Esakov, <jcamins@cpbibliography.com>