CGI::Application::Plugin::CHI - CGI-App plugin for CHI caching interface
Version 0.03
package My::WebApp; use strict; use warnings; use base 'CGI::Application'; use CGI::Application::Plugin::CHI; __PACKAGE__->cache_config( { driver => 'File', root_dir => '/path/to/nowhere' } ); ... # a runmode sub a_runmode { my $self = shift; $self->cache->set( foo => 42 ); return 'the value of foo is ' . $self->cache->get( 'foo' ); }
This module exports the following methods into your CGI::Application base class: cache_config, cache_default, cache, rmcache and __get_cache.
cache_config
cache_default
cache
rmcache
__get_cache
This method sets up all your caches and stores their configurations for later retrieval. You can call cache_config in two ways. The simple way sets up a default cache, and takes a single hashref which is passed directly to CHI:
__PACKAGE__->cache_config( { driver => 'File', root_dir => '/path/to/nowhere' } )
Once it's set up, this default cache can be accessed from anywhere that can call the cache or rmcache methods. (e.g. your web application class and any of its subclasses.)
Alternatively, you can pass in a list of name => hashref pairs to set up several caches with different names.
__PACKAGE__->cache_config( ondisk => { driver => 'File', root_dir => '/path/to/nowhere' }, inram => { driver => 'Memory', datastore => \%hash }, distributed => { driver => 'Memcached', ... } );
You can call cache_config multiple times to add or overwrite additional cache configurations.
These caches can be accessed with the one-argument form of cache and rmcache described below.
This method designates a named cache as the default cache.
__PACKAGE__->cache_default( 'foobar' ); # $self->cache() now returns the same as $self->cache( 'foobar' )
This method instantiates and returns a cache which you have previously configured. With no arguments, it returns the default cache, if there is one.
my $cache = $self->cache; # default cache
If there is no default cache, a fatal error occurs.
You can pass the name of a cache as an argument.
my $cache = $self->cache( 'foobar' ); # the foobar cache
If there is no cache with that name, a fatal error occurs.
This does the same thing as cache above, except it performs the extra step of setting the cache's namespace to a concatenation of the current class's name and the current runmode. You can use this to store per-runmode data that you don't want crashing into other runmodes.
sub runmode_foo { my $self = shift; my $cache = $self->rmcache( 'foobar' ); # items stored here will be in # their own namespace }
Just like cache, you can call rmcache with zero arguments to get the default cache with a namespace set.
Note that if you set a namespace when you called cache_config, using rmcache will override it.
This method is used internally by cache and rmcache to fetch and instantiate the proper cache object. It will be exported to your application, but you should not call it directly.
Mike Friedman, <friedo at friedo.com>
<friedo at friedo.com>
Thanks to 黄叶 for pointing out some documentation bugs, and Jonathan Swartz, Perrin Harkins and the rest of the CHI team.
Please report any bugs or feature requests to bug-cgi-application-plugin-chi at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-CHI. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-cgi-application-plugin-chi at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc CGI::Application::Plugin::CHI
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=CGI-Application-Plugin-CHI
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/CGI-Application-Plugin-CHI
CPAN Ratings
http://cpanratings.perl.org/d/CGI-Application-Plugin-CHI
Search CPAN
http://search.cpan.org/dist/CGI-Application-Plugin-CHI
Copyright 2008 Mike Friedman, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install CGI::Application::Plugin::CHI, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Application::Plugin::CHI
CPAN shell
perl -MCPAN -e shell install CGI::Application::Plugin::CHI
For more information on module installation, please visit the detailed CPAN module installation guide.