
CHI::Driver::Memory -- In-process memory based cache.

use CHI;
my $cache = CHI->new(driver => 'Memory');
my $hash = {};
my $cache = CHI->new(driver => 'Memory', datastore => $hash);

This cache driver stores data on a per-process basis. This is the fastest of the cache implementations, but data can not be shared between processes. Data will remain in the cache until cleared, expired, or the process dies.
To maintain the same semantics as other caches, data structures are deep-copied on set and get. Thus, modifications to the original data structure will not affect the data structure stored in the cache, and vica versa.

When using this driver, the following options can be passed to CHI->new() in addition to the CHI.
A hash to be used for storage. Within the hash, each namespace is used as a key to a second-level hash. This hash may be passed to multiple CHI::Driver::Memory constructors. By default a single global hash will be used.
For example, it can be useful to create a memory cache that lasts for a single web request. If you using a web framework with a request object which goes out of scope at the end of the page request, you can hang a datastore off of that request object. e.g.
$r->notes('memory_cache_datastore', {});
...
my $cache = CHI->new(driver => 'Memory', datastore => $r->notes('memory_cache_datastore'));
This eliminates the danger of "forgetting" to clear the cache at the end of the request.

CHI

Jonathan Swartz

Copyright (C) 2007 Jonathan Swartz.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.