|
NAMECatalyst::Plugin::Cache - Flexible caching support for Catalyst. SYNOPSIS 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 );
}
};
DESCRIPTIONThis 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. METHODS
METADATAIntroductionWhenever 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. Meta Data Keys"choose_cache_backend" is called with some default keys.
Metadata CurryingIn 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. CONFIGURATION $c->config->{'Plugin::Cache'} = {
...
};
All configuration parameters should be provided in a hash reference under the "Plugin::Cache" key in the "config" hash. Backend ConfigurationConfiguring 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".
Cache Profiles
Miscellaneous Configuration
TERMINOLOGY
SEE ALSOCache - 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. AUTHORYuval Kogman, "nothingmuch@woobling.org" Jos Boumans, "kane@cpan.org" COPYRIGHT & LICENSECopyright (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. POD ERRORSHey! The above document had some coding errors, which are explained below:
|