CHI::Memoize(3pm) | User Contributed Perl Documentation | CHI::Memoize(3pm) |
CHI::Memoize - Make functions faster with memoization, via CHI
version 0.07
use CHI::Memoize qw(:all); # Straight memoization in memory memoize('func'); memoize('Some::Package::func'); # Memoize to a file or to memcached memoize( 'func', driver => 'File', root_dir => '/path/to/cache' ); memoize( 'func', driver => 'Memcached', servers => ["127.0.0.1:11211"] ); # Expire after one hour memoize('func', expires_in => '1h'); # Memoize based on the second and third argument to func memoize('func', key => sub { $_[1], $_[2] });
"`Memoizing' a function makes it faster by trading space for time. It does this by caching the return values of the function in a table. If you call the function again with the same arguments, "memoize" jumps in and gives you the value out of the table, instead of letting the function compute the value all over again." -- quoted from the original Memoize
For a bit of history and motivation, see
http://www.openswartz.com/2012/05/06/memoize-revisiting-a-twelve-year-old-api/
"CHI::Memoize" provides the same facility as Memoize, but backed by CHI. This means, among other things, that you can
All of these are importable; only "memoize" is imported by default. "use Memoize qw(:all)" will import them all as well as the "NO_MEMOIZE" constant.
$func can be a function name (with or without a package prefix) or an anonymous function. In the former case, the name is rebound to the new function. In either case a code ref to the new wrapper function is returned.
# Memoize a named function memoize('func'); memoize('Some::Package::func'); # Memoize an anonymous function $anon = memoize($anon);
By default, the cache key is formed from combining the full function name, the calling context ("L" or "S"), and all the function arguments with canonical JSON (sorted hash keys). e.g. these calls will be memoized together:
memoized_function({a => 5, b => 6, c => { d => 7, e => 8 }}); memoized_function({b => 6, c => { e => 8, d => 7 }, a => 5});
because the two hashes being passed are canonically the same. But these will be memoized separately because of context:
my $scalar = memoized_function(5); my @list = memoized_function(5);
By default, the cache namespace is formed from the full function name or the stringified code reference. This allows you to introspect and clear the memoized results for a particular function.
"memoize" throws an error if $func is already memoized.
See OPTIONS below for what can go in the options hash.
# The CHI cache where memoize results are stored # my $cache = memoized($func)->cache; $cache->clear; # Code references to the original function and to the new wrapped function # my $orig = memoized($func)->orig; my $wrapped = memoized($func)->wrapped;
memoize('Some::Package::func'); ... unmemoize('Some::Package::func');
The following options can be passed to "memoize".
For example, this uses the second and third argument to the function as a key:
memoize('func', key => sub { @_[1..2] });
and this is useful for functions that accept a list of key/value pairs:
# Ignore order of key/value pairs memoize('func', key => sub { %@_ });
Regardless of what key you specify, it will automatically be prefixed with the full function name and the calling context ("L" or "S").
If the coderef returns "CHI::Memoize::NO_MEMOIZE" (or "NO_MEMOIZE" if you import it), this call won't be memoized. This is useful if you have a cache of limited size or if you know certain arguments will yield nondeterministic results. e.g.
memoize('func', key => sub { $is_worth_caching ? @_ : NO_MEMOIZE });
# Expire after one hour memoize('func', expires_in => '1h'); # Expire when a particular condition occurs memoize('func', expire_if => sub { ... });
# Store in file instead of memory memoize( 'func', driver => 'File', root_dir => '/path/to/cache' ); # Store in memcached instead of memory memoize('func', driver => 'Memcached', servers => ["127.0.0.1:11211"]);
Unless specified, the namespace is generated from the full name of the function being memoized.
You can also specify an existing cache object:
# Store in memcached instead of memory my $cache = CHI->new(driver => 'Memcached', servers => ["127.0.0.1:11211"]); memoize('func', cache => $cache);
By default "CHI", and thus "CHI::Memoize", returns a deep clone of the stored value even when caching in memory. e.g. in this code
# func returns a list reference memoize('func'); my $ref1 = func(); my $ref2 = func();
$ref1 and $ref2 will be references to two completely different lists which have the same contained values. More specifically, the value is serialized by Storable on "set" and deserialized (hence cloned) on "get".
The advantage here is that it is safe to modify a reference returned from a memoized function; your modifications won't affect the cached value.
my $ref1 = func(); push(@$ref1, 3, 4, 5); my $ref2 = func(); # $ref2 does not have 3, 4, 5
The disadvantage is that it takes extra time to serialize and deserialize the value, and that some values like code references may be more difficult to store. And cloning may not be what you want at all, e.g. if you are returning objects.
Alternatively you can use CHI::Driver::RawMemory, which will store raw references the way "Memoize" does. Now, however, any modifications to the contents of a returned reference will affect the cached value.
memoize('func', driver => 'RawMemory'); my $ref1 = func(); push(@$ref1, 3, 4, 5); my $ref2 = func(); # $ref1 eq $ref2 # $ref2 has 3, 4, 5
The caveats of Memoize apply here as well. To summarize:
A number of modules address a subset of the problems addressed by this module, including:
Questions and feedback are welcome, and should be directed to the perl-cache mailing list:
http://groups.google.com/group/perl-cache-discuss
Bugs and feature requests will be tracked at RT:
http://rt.cpan.org/NoAuth/Bugs.html?Dist=CHI-Memoize bug-chi-memoize@rt.cpan.org
The latest source code can be browsed and fetched at:
http://github.com/jonswar/perl-chi-memoize git clone git://github.com/jonswar/perl-chi-memoize.git
CHI, Memoize
Jonathan Swartz <swartz@pobox.com>
This software is copyright (c) 2011 by Jonathan Swartz.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
2018-10-03 | perl v5.26.2 |