AnyEvent-InMemoryCache
view release on metacpan or search on metacpan
my $cache = AnyEvent::InMemoryCache->new;
$cache->set(immortal => "Don't expire!"); # It lasts forever by default
say $cache->get("immortal"); # "Don't expire!"
$cache->set(a_second => "Expire soon", "1s"); # Expires in one-second.
say $cache->get('a_second'); # "Expires soon"
AE::timer 2, 0, sub{ # 2 seconds later
$cache->exists('a_second'); # false
};
# You can overwrite key, and it's mortal now.
$cache->set(immortal => 'will die...', "10min");
# If you want a key not to be expired, pass negative integer for the third parameter.
$cache->set(immortal => 'Immortal again!', -1);
lib/AnyEvent/InMemoryCache.pm view on Meta::CPAN
sub set {
my ($self, $key, $val, $expires_in) = @_;
if ( @_ < 4) {
$expires_in = $self->{expires_in};
} else {
$expires_in = parse_duration($expires_in);
}
$self->{_datastore}{$key} = [
$val,
($expires_in < 0 ? undef : AE::timer $expires_in, 0, sub{ delete $self->{_datastore}{$key} })
];
$val;
}
sub get {
my ($self, $key) = @_;
($self->{_datastore}{$key} || [])->[0];
}
sub exists {
lib/AnyEvent/InMemoryCache.pm view on Meta::CPAN
use AnyEvent;
use AnyEvent::InMemoryCache;
my $cache = AnyEvent::InMemoryCache->new;
$cache->set(immortal => "Don't expire!"); # It lasts forever by default
say $cache->get("immortal"); # "Don't expire!"
$cache->set(a_second => "Expire soon", "1s"); # Expires in one-second.
say $cache->get('a_second'); # "Expires soon"
AE::timer 2, 0, sub{ # 2 seconds later
$cache->exists('a_second'); # false
};
# You can overwrite key, and it's mortal now.
$cache->set(immortal => 'will die...', "10min");
# If you want a key not to be expired, pass negative integer for the third parameter.
$cache->set(immortal => 'Immortal again!', -1);
# You can specify default lifetime of keys.
t/01-expires.t view on Meta::CPAN
ok !$cache->exists("bar");
is $cache->set(bar => "fuga", "2s"), "fuga"; # lives for 2 seconds
ok $cache->exists("bar");
is $cache->get("bar"), "fuga";
ok !$cache->exists("baz");
is $cache->set(baz => "piyo", "4 seconds"), "piyo"; # lives for 4 seconds
ok $cache->exists("baz");
is $cache->get("baz"), "piyo";
my $w1; $w1 = AE::timer 3, 0, sub{ # 3 sendos later
ok $cache->exists("foo");
is $cache->get("foo"), "hoge";
ok !$cache->exists("bar");
ok $cache->exists("baz");
is $cache->get("baz"), "piyo";
};
my $w2; $w2 = AE::timer 5, 0, sub{ # 5 sendos later
ok $cache->exists("foo");
is $cache->get("foo"), "hoge";
ok !$cache->exists("bar");
ok !$cache->exists("baz");
is $cache->delete("foo"), "hoge";
ok !$cache->exists("foo");
$end_cv->send;
t/02-default_expires.t view on Meta::CPAN
ok !$cache->exists("bar");
is $cache->set(bar => "fuga"), "fuga"; # lives for 2 second, but overwritten later
ok $cache->exists("bar");
is $cache->get("bar"), "fuga";
ok !$cache->exists("baz");
is $cache->set(baz => "piyo", "3 seconds"), "piyo"; # lives for 3 seconds
ok $cache->exists("baz");
is $cache->get("baz"), "piyo";
my $w0; $w0 = AE::timer 1, 0, sub{ # 1 seconds later
ok $cache->exists("foo");
is $cache->get("foo"), "hoge";
ok $cache->exists("bar");
is $cache->get("bar"), "fuga";
is $cache->set(bar => "fuga", "3 s"), "fuga", "Extend lifetime!";
ok $cache->exists("baz");
is $cache->get("baz"), "piyo";
};
my $w1; $w1 = AE::timer 2, 0, sub{ # 2 seconds later
ok !$cache->exists("foo");
ok $cache->exists("bar");
is $cache->get("bar"), "fuga";
ok $cache->exists("baz");
is $cache->get("baz"), "piyo";
};
my $w2; $w2 = AE::timer 4, 0, sub{ # 4 seconds later
ok !$cache->exists("foo");
ok $cache->exists("bar");
is $cache->get("bar"), "fuga";
ok !$cache->exists("baz");
};
my $w3; $w3 = AE::timer 5, 0, sub{ # 5 seconds later
ok !$cache->exists("foo");
ok !$cache->exists("bar");
ok !$cache->exists("baz");
$end_cv->send;
};
$end_cv->recv;
t/03-tiehash.t view on Meta::CPAN
ok !exists $cache{"baz"};
is scalar keys %cache, 2;
is_deeply [sort keys %cache], [qw(bar foo)];
ok scalar %cache;
# Overwrite bar
(tied %cache)->set(bar => "updated!", "3s");
is $cache{"bar"}, "updated!";
my $w1; $w1 = AE::timer 2, 0, sub{ # 2 seconds later
is scalar keys %cache, 1;
is_deeply [sort keys %cache], [qw(bar)];
ok scalar %cache;
ok !exists $cache{"foo"};
ok exists $cache{"bar"};
is $cache{"bar"}, "updated!";
ok !exists $cache{"baz"};
};
my $w2; $w2 = AE::timer 4, 0, sub{ # 4 seconds later
is scalar keys %cache, 0;
is_deeply [sort keys %cache], [];
is "" . %cache, "0";
ok !%cache;
ok !exists $cache{"foo"};
ok !exists $cache{"bar"};
ok !exists $cache{"baz"};
$end_cv->send;
( run in 1.076 second using v1.01-cache-2.11-cpan-49f99fa48dc )