Catalyst::Plugin::Cache(3pm) | User Contributed Perl Documentation | Catalyst::Plugin::Cache(3pm) |
Catalyst::Plugin::Cache - Flexible caching support for Catalyst.
use Catalyst qw/ Cache /; # configure a backend or use a store plugin __PACKAGE__->config->{'Plugin::Cache'}{backend} = { class => "Cache::Bounded", # ... params for Cache::Bounded... }; # typical example for Cache::Memcached::libmemcached __PACKAGE__->config->{'Plugin::Cache'}{backend} = { class => "Cache::Memcached::libmemcached", servers => ['127.0.0.1:11211'], debug => 2, }; # In a controller: sub foo : Local { my ( $self, $c, $id ) = @_; my $cache = $c->cache; my $result; unless ( $result = $cache->get( $id ) ) { # ... calculate result ... $c->cache->set( $id, $result ); } };
This plugin gives you access to a variety of systems for caching data. It allows you to use a very simple configuration API, while maintaining the possibility of flexibility when you need it later.
Among its features are support for multiple backends, segmentation based on component or controller, keyspace partitioning, and so more, in various subsidiary plugins.
If a profile by the name $profile_name doesn't exist, but a backend object by that name does exist, the backend will be returned instead, since the interface for curried caches and backends is almost identical.
This method can also be called without arguments, in which case is treated as though the %meta hash was empty.
See "METADATA" for details.
See "METADATA" for details.
If the backend object does not support "compute" then we emulate it by calling cache_get, and if the returned value is undefined we call the passed code reference, stores the returned value with cache_set, and then returns the value. Inspired by CHI.
This method is typically used by plugins.
Whenever you set or retrieve a key you may specify additional metadata that will be used to select a specific backend.
This metadata is very freeform, and the only key that has any meaning by default is the "backend" key which can be used to explicitly choose a backend by name.
The "choose_cache_backend" method can be overridden in order to facilitate more intelligent backend selection. For example, Catalyst::Plugin::Cache::Choose::KeyRegexes overrides that method to select a backend based on key regexes.
Another example is a Catalyst::Plugin::Cache::ControllerNamespacing, which wraps backends in objects that perform key mangling, in order to keep caches namespaced per controller.
However, this is generally left as a hook for larger, more complex applications. Most configurations should make due XXXX
The simplest way to dynamically select a backend is based on the "Cache Profiles" configuration.
"choose_cache_backend" is called with some default keys.
In order to avoid specifying %meta over and over again you may call "cache" or "curry_cache" with %meta once, and get back a curried cache object. This object responds to the methods "get", "set", and "remove", by appending its captured metadata and delegating them to "cache_get", "cache_set", and "cache_remove".
This is simpler than it sounds.
Here is an example using currying:
my $cache = $c->cache( %meta ); # cache is curried $cache->set( $key, $value ); $cache->get( $key );
And here is an example without using currying:
$c->cache_set( $key, $value, %meta ); $c->cache_get( $key, %meta );
See Catalyst::Plugin::Cache::Curried for details.
$c->config->{'Plugin::Cache'} = { ... };
All configuration parameters should be provided in a hash reference under the "Plugin::Cache" key in the "config" hash.
Configuring backend objects is done by adding hash entries under the "backends" key in the main config.
A special case is that the hash key under the "backend" (singular) key of the main config is assumed to be the backend named "default".
$c->config->{'Plugin::Cache'}{backends}{small_things} = { class => "Cache::Bounded", interval => 1000, size => 10000, }; $c->config->{'Plugin::Cache'}{backends}{large_things} = { class => "Cache::Memcached", data => '1.2.3.4:1234', };
The options in the hash are passed to the class's "new" method.
The class will be "required" as necessary during setup time.
$c->config->{'Plugin::Cache'}{backend} = { store => "FastMmap", };
Store plugins typically require less configuration because they are specialized for Catalyst applications. For example Catalyst::Plugin::Cache::Store::FastMmap will specify a default "share_file", and additionally use a subclass of Cache::FastMmap that can also store non reference data.
The store plugin must be loaded.
For example when you specify
$c->config->{'Plugin::Cache'}{profiles}{thumbnails} = { backend => "large_things", };
And then get a cache object like this:
$c->cache("thumbnails");
It is the same as if you had done:
$c->cache( backend => "large_things" );
my $cache = $c->cache(type => 'thumbnails'); $cache->set('pic01', $thumbnaildata);
A cache which has been pre-configured with a particular set of namespacing data. In the example the cache returned could be one specifically tuned for storing thumbnails.
An object that responds to "get", "set", and "remove", and will automatically add metadata to calls to "$c->cache_get", etc.
Cache - the generic cache API on CPAN.
Catalyst::Plugin::Cache::Store - how to write a store plugin.
Catalyst::Plugin::Cache::Curried - the interface for curried caches.
Catalyst::Plugin::Cache::Choose::KeyRegexes - choose a backend based on regex matching on the keys. Can be used to partition the keyspace.
Catalyst::Plugin::Cache::ControllerNamespacing - wrap backend objects in a name mangler so that every controller gets its own keyspace.
Yuval Kogman, "nothingmuch@woobling.org"
Jos Boumans, "kane@cpan.org"
Copyright (c) Yuval Kogman, 2006. All rights reserved.
This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself, as well as under the terms of the MIT license.
2022-06-09 | perl v5.34.0 |