Cache::Adaptive - A Cache Engine with Adaptive Lifetime Control
use Cache::Adaptive; use Cache::FileCache; my $cache = Cache::Adaptive->new({ backend => Cache::FileCache->new({ namespace => 'html_cache', max_size => 10 * 1024 * 1024, }), expires_min => 3, expires_max => 60, check_load => sub { my $entry = shift; int($entry->{process_time} * 2) - 1; }, }); ... print "Content-Type: text/html\n\n"; print $cache->access({ key => $uri, builder => sub { # your HTML generation logic here $html; }, });
Cache::Adaptive is a cache engine with adaptive lifetime control. Cache lifetimes can be increased or decreased by any factor, e.g. load average, process time for building the cache entry, etc., through the definition of the check_load callback.
Cache::Adaptive
check_load
Cache::Adaptive recognizes following properties. The properties can be set though the constructor, or by calling the accessors.
Backend storage to be used. Should be a Cache::Cache object. Note: do not use Cache::SizeAwareFileCache, since its get method might overwrite data saved by other processes. The update algorithm of Cache::Adaptive needs a reliable set method.
Interval between calls to the check_load callback for each cache entry. Default is 0, meaning that check_load will be called every time the cache entry is being built.
User supplied callback for deciding the cache policy. If a positive number is returned, cache lifetime for the entry will be increased. If a negative number is returned, the lifetime will be decreased. If 0 is returned, the lifetime will not be modified. For detail, see the "DEFINING THE CACHE STRATEGY" section.
Cache lifetime will be increased or decreased by applying either factor to current lifetime.
Minimal and maximal expiration times, in seconds.
An optional callback for logging.
Seconds until per-entry information used for deciding caching algorithm will be purged. Defaults to expires_max * 2.
expires_max
See above.
Returns the cached entry if possible, or builds the entry by calling the builder function, and optionally stores the build entry to cache.
A variety of cache strategies can be implemented by defining the check_load callback. Below are some examples.
my $cache = Cache::Adaptive->new({ ... check_load => sub { my ($entry, $params) = @_; int($entry->{process_time} * 2) - 1; }, });
Assume that the process time of each operation increases as the system becomes heavily loaded. Above code will start caching or increase cache lifetime if the process time for each operation takes more than a second. As more entries become cached, the system load will become lighter, leading to faster process times, and cache lifetimes will no more be increased. When the process time becomes smaller than 0.5 seconds, the cache lifetime will be decreased.
my $cache = Cache::Adaptive->new({ ... check_interval => 60, check_load => sub { my ($entry, $params) = @_; int($params->{load} * 4) - 1; }, });
$params-{load}> contains $entry-{process_time}> divided by build frequency. The above code increases cache lifetime if the system is building the entry during more than 50% of its operation recently. Note that the system may be running multiple processes simultaneously. This value represents the real time, not CPU cycles that were actually spent for handling the operation.
$params-
$entry-
real
use BSD::Sysctl qw(sysctl); my $cache = Cache::Adaptive->new({ ... check_interval => 60, check_load => sub { my $load_avg = sysctl('vm.loadavg'); int($load_avg->[0] * 2) - 1; }, });
The example updates the cache lifetime by referring to the load average. The example should only work on BSD systems.
my $cache = Cache::Adaptive->new({ ... check_interval => 60, check_load => sub { my ($entry, $params) = @_; my $load_avg = sysctl('vm.loadavg'); int($params{load} * 4 * $load_avg->[0] ** 2) - 1; }, });
The example utilizes the cache for heavily accessed entries under heavy load.
For updates, see
http://labs.cybozu.co.jp/blog/kazuho/ http://labs.cybozu.co.jp/blog/kazuhoatwork/
Copyright (c) 2007 Cybozu Labs, Inc. All rights reserved.
written by Kazuho Oku <kazuhooku@gmail.com>
Toru Yamaguchi <zigorou@cpan.org>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install Cache::Adaptive, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Cache::Adaptive
CPAN shell
perl -MCPAN -e shell install Cache::Adaptive
For more information on module installation, please visit the detailed CPAN module installation guide.