NAME
Cache::Isolator - transaction and concurrency manager of cache systems.
SYNOPSIS
use Cache::Isolator;
use Cache::Memcached::Fast;
my $isolator = Cache::Isolator->new(
cache => Cache::Memcached::Fast->new(...),
concurrency => 4,
);
my $key = 'query:XXXXXX';
$isolator->get_or_set(
$key,
sub { # This callback invoked when miss cache
get_from_db($key);
},
3600
);
DESCRIPTION
Cache::Isolator is transaction and concurrency manager of cache systems. Many cache systems have Thundering Herd problem. If a cache has expired, concentration of access to the database may happen. This will cause a system failure. Cache::Isolator can control the concentration of load.
METHODS
new( %args )
Following parameters are recognized.
- cache
-
Required. Cache::Memcached::Fast object or similar interface object.
- concurrency
-
Optional. Number of get_or_set callback executed in parallel. If many process need to run callback, they wait until lock becomes released or able to get values. Defaults to 1. It means no concurrency.
- interval
-
Optional. The seconds for busy loop interval. Defaults to 0.01 seconds.
- trial
-
Optional. When the value is being set zero, get_or_set will be waiting until lock becomes released. When the value is being set positive integer value, get_or_set will die on reached trial count. Defaults to 0.
- timeout
-
Optional. The seconds until lock becomes released. Defaults to 30 seconds.
- early_expires_ratio
-
Optional. if early_expires_ratio was set to greater than zero, Cache::Isolator stores to duplicate the cache. One of them is set to expire earlier than normal. Cache::Isolator gets the cache has been set to expire early in the specified percentage. This feature can prevent the cache from disappearing all together. Defaults is "0".
my $cache = Cache::Isolator->new( early_expires_ratio => 10, # This means 1/10 ratio );
- expires_before
-
Optional. Seconds earlier expiration. Defaults is 10.
get_or_set( $key, $callback, $expires )
$callback is subroutine reference. That invoked when cache miss occurred.
get($key)
set($key, $value, $expires)
delete($key)
AUTHOR
Masahiro Nagano <kazeburo {at} gmail.com>
SEE ALSO
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.