NAME
Memoize::Memcached::Attribute - auto-memoize function results using memcached
VERSION
version 0.10
SYNOPSIS
To set up your memcache client for this package, you can pass the params in during import:
use Memoize::Memcached::Attribute (servers => [ '127.0.0.1:11211' ]);
Alternatively, you can pass in your memcache client object entirely (we use this because we subclass Cache::Memcached::Fast to add some additional methods and default parameters):
use Memoize::Memcached::Attribute (-client => Cache::Memcached::Fast->new({ servers => [ '127.0.0.1:11211' ] }));
Or you can specify it at runtime, the only caveat being that you must do this prior to calling any memoized function:
use Memoize::Memcache::Attribute;
Memoize::Memcache::Attribute::client(Cache::Memcached::Fast->new({ servers => [ '127.0.0.1:11211' ] }));
To use the memoization, you use the :CacheMemoize subroutine attribute, which takes a cache duration as a parameter:
# cache the results in memcache for 5 minutes
sub myfunc :CacheMemoize(300) {
my @params = @_;
my $total;
$total += $_ for @params;
return $total;
}
Sometimes you have a method that is not dependent on object state, and you want to memoize those results, independent of the object used to generate them:
# cache the results in memcache for 30 seconds
# but don't look at the object as part of the input data
sub mymethod :CacheMemoize(method => 30) {
my $self = shift;
my @params = @_;
return join('.', @params);
}
While not generally recommended as good design, we do support the ability to invalidate caches. If you find yourself using the invalidation often, this module is probably not really how you want to go about achieving your caching strategy. Here's how you do it:
Memoize::Memcached::Attribute::invalidate('Some::Package::myfunc', @params);
If you're invalidating the cache from inside the same package as the cached function (which is probably the only place you should be), you can omit the package name:
Memoize::Memcached::Attribute::invalidate('mymethod', @params);
DESCRIPTION
Memoization is a process whereby you cache the results of a function, based on its input, in memory. This module expands that concept to use memcache to provide a shared memory cache rather than a per-process cache like a lot of other memoization modules. You can also specify a timeout, in case your results might change, just not that frequently.
OPTIONS
When you import the package, you can pass a few options in:
- -noattrimport - Precludes importing the necessary methods to the calling namespace
- -client - Allows you to specify your own memcache client object. Useful if you subclass Cache::Memcached in your codebase.
Any remaining options will be used to connect to the Cache::Memcached client object, if passed.
THREADS/FORKING
Because this module internally stores the memcached client as a package global, and the memcached clients have issues with threads and forking, it would be wise to reset the package global after forking or creating a new thread. This can be done like this:
if (my $pid = fork) {
# parent
}
else {
Memoize::Memcached::Attribute::client(%client_constructor_params);
# or $Memoize::Memcached::Attribute::MEMCACHE = $memcached_client_object;
}
ACKNOWLEDGEMENTS
Thanks to Chris Reinhardt and David Dierauer for finding and fixing some issues. And to LiquidWeb for allowing me to contribute this to CPAN.
BUGS
None known. This has been in use in LiquidWeb production code for a few years without any known issues.
If you find one, or have a feature request, submit them here: https://github.com/jimbobhickville/perl-Memoize-Memcached-Attribute/issues/new
LICENCE
Copyright 2010-2012, Greg Hill (jimbobhickville -AT- gmail -DOT- com)
This software is free. It is licensed under the same terms as Perl itself.
The latest version of this software should be available from my github repo: https://github.com/jimbobhickville/perl-Memoize-Memcached-Attribute
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 205:
Unknown directive: =over4
- Around line 207:
'=item' outside of any '=over'