Koha::Cache - Handling caching of html and Objects for Koha
use Koha::Cache; my $cache = Koha::Cache->new({cache_type => $cache_type, %params});
Koha caching routines. This class provides two interfaces for cache access. The first, traditional 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 caching system has been selected. This is not an instance method.
$cache->set_in_cache($key, $value, [$expiry]);
Save a value to the specified key in the default cache, optionally with a particular expiry.
my $value = $cache->get_from_cache($key);
Retrieve the value stored under the specified key in the default cache.
$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>