Cache-Sliding
view release on metacpan or search on metacpan
$cache->set($key, $value);
$value = $cache->get($key);
$cache->del($key);
DESCRIPTION
Implement caching object using sliding time-based expiration strategy
(data in the cache is invalidated by specifying the amount of time the
item is allowed to be idle in the cache after last access time).
Use EV::timer, so this module only useful in EV-based applications,
because cache expiration will work only while you inside EV::loop.
INTERFACE
new
$cache = Cache::Sliding->new( $expire_after );
Create and return new cache object. Elements in this cache will expire
between $expire_after seconds and 2*$expire_after seconds.
lib/Cache/Sliding.pm view on Meta::CPAN
sub new {
my ($class, $expire_after) = @_;
my $self = {
L1 => {},
L2 => {},
t => undef,
};
weaken(my $this = $self);
$self->{t} = EV::timer $expire_after, $expire_after, sub { if ($this) {
$this->{L2} = $this->{L1};
$this->{L1} = {};
} };
return bless $self, $class;
}
sub get {
my ($self, $key) = @_;
if (exists $self->{L1}{$key}) {
return $self->{L1}{$key};
lib/Cache/Sliding.pm view on Meta::CPAN
$value = $cache->get($key);
$cache->del($key);
=head1 DESCRIPTION
Implement caching object using sliding time-based expiration strategy
(data in the cache is invalidated by specifying the amount of time the
item is allowed to be idle in the cache after last access time).
Use EV::timer, so this module only useful in EV-based applications,
because cache expiration will work only while you inside EV::loop.
=head1 INTERFACE
=head2 new
$cache = Cache::Sliding->new( $expire_after );
Create and return new cache object. Elements in this cache will expire
my $cache1 = Cache::Sliding->new(0.1);
my $cache2 = Cache::Sliding->new(0.2);
my $cache5 = Cache::Sliding->new(0.5);
$cache1->set('test', 1);
$cache2->set('test', 2);
$cache5->set('test', 5);
my @t = (
EV::timer(0.10, 0, sub { is($cache1->get('test'), 1) }),
EV::timer(0.10, 0, sub { is($cache2->get('test'), 2) }),
EV::timer(0.10, 0, sub { is($cache5->get('test'), 5) }),
EV::timer(0.19, 0, sub { is($cache1->get('test'), 1) }),
EV::timer(0.20, 0, sub { $cache2->del('test') }),
EV::timer(0.28, 0, sub { is($cache1->get('test'), 1) }),
EV::timer(0.30, 0, sub { is($cache2->get('test'), undef) }),
EV::timer(0.30, 0, sub { is($cache5->get('test'), 5) }),
EV::timer(0.37, 0, sub { is($cache1->get('test'), 1) }),
EV::timer(0.58, 0, sub { is($cache1->get('test'), undef) }),
EV::timer(1.40, 0, sub { is($cache5->get('test'), undef) }),
EV::timer(2.00, 0, sub { EV::break }),
);
EV::run;
( run in 0.706 second using v1.01-cache-2.11-cpan-49f99fa48dc )