Cache-Ref
view release on metacpan or search on metacpan
lib/Cache/Ref.pm view on Meta::CPAN
This is the only algorithm for which C<get> (and C<hit>) has no side effects.
=head2 LRU
This implementation uses an LRU list of entries (two implementations are
provided for trading off memory for speed).
Long term utility of cache entries is not considered at all, so scans will
poison the cache.
=head3 Cache::Ref::Util::LRU::List
Uses a doubly linked list to perform MRU propagation.
Faster than Array.
Cache hits and LRU removal is O(1).
=head3 Cache::Ref::Util::LRU::Array
Generally slower for a cache size bigger than about 10 elements, but uses less memory due to the compact layout.
Cache hits are O(cache size). LRU removal is O(1).
=head2 CLOCK
This is an implementation of second chance FIFO, using a circular buffer.
Second chance FIFO is a very simple approximation of LRU. The CLOCK algorithm
lib/Cache/Ref/LRU.pm view on Meta::CPAN
BEGIN {
$Cache::Ref::LRU::AUTHORITY = 'cpan:NUFFIN';
}
BEGIN {
$Cache::Ref::LRU::VERSION = '0.04';
}
# ABSTRACT: Least recently used expiry policy
use Moose;
use Cache::Ref::Util::LRU::List;
use namespace::autoclean;
extends qw(Cache::Ref);
with qw(
Cache::Ref::Role::API
Cache::Ref::Role::Index
);
has size => (
isa => "Int",
is => "ro",
required => 1,
);
has lru_class => (
isa => "ClassName",
is => "ro",
default => "Cache::Ref::Util::LRU::List",
);
has _lru => (
does => "Cache::Ref::Util::LRU::API",
is => "ro",
lazy_build => 1,
);
sub _build__lru { shift->lru_class->new }
sub get {
my ( $self, @keys ) = @_;
my @e = $self->_index_get(@keys);
lib/Cache/Ref/Util/LRU/API.pm view on Meta::CPAN
package Cache::Ref::Util::LRU::API;
BEGIN {
$Cache::Ref::Util::LRU::API::AUTHORITY = 'cpan:NUFFIN';
}
BEGIN {
$Cache::Ref::Util::LRU::API::VERSION = '0.04';
}
use Moose::Role;
use namespace::autoclean;
requires qw(
insert
hit
remove
lib/Cache/Ref/Util/LRU/API.pm view on Meta::CPAN
# ex: set sw=4 et:
__END__
=pod
=encoding utf-8
=head1 NAME
Cache::Ref::Util::LRU::API
=head1 AUTHOR
Yuval Kogman
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Yuval Kogman.
This is free software; you can redistribute it and/or modify it under
lib/Cache/Ref/Util/LRU/Array.pm view on Meta::CPAN
package Cache::Ref::Util::LRU::Array;
BEGIN {
$Cache::Ref::Util::LRU::Array::AUTHORITY = 'cpan:NUFFIN';
}
BEGIN {
$Cache::Ref::Util::LRU::Array::VERSION = '0.04';
}
use Moose;
use Scalar::Util qw(refaddr);
use Hash::Util::FieldHash::Compat qw(id);
use namespace::autoclean;
has _list => (
traits => [qw(Array)],
lib/Cache/Ref/Util/LRU/Array.pm view on Meta::CPAN
handles => {
#size => "length",
mru => [ get => 0 ],
lru => [ get => -1 ],
remove_mru => "shift",
remove_lru => "pop",
clear => "clear",
},
);
with qw(Cache::Ref::Util::LRU::API);
# since there's no need for metadata, insert is just like hit
sub insert {
my ( $self, @elements ) = @_;
$self->hit(@elements);
return ( @elements == 1 ? $elements[0] : @elements );
}
lib/Cache/Ref/Util/LRU/Array.pm view on Meta::CPAN
# ex: set sw=4 et:
__END__
=pod
=encoding utf-8
=head1 NAME
Cache::Ref::Util::LRU::Array
=head1 AUTHOR
Yuval Kogman
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Yuval Kogman.
This is free software; you can redistribute it and/or modify it under
lib/Cache/Ref/Util/LRU/List.pm view on Meta::CPAN
package Cache::Ref::Util::LRU::List;
BEGIN {
$Cache::Ref::Util::LRU::List::AUTHORITY = 'cpan:NUFFIN';
}
BEGIN {
$Cache::Ref::Util::LRU::List::VERSION = '0.04';
}
use Moose;
use namespace::autoclean;
with (
'Cache::Ref::Util::LRU::API',
'Cache::Ref::Role::WithDoublyLinkedList' => {
name => "",
value_offset => 0,
next_offset => 1,
prev_offset => 2,
head_method => "mru",
tail_method => "lru",
shift_method => "remove_mru",
pop_method => "remove_lru",
lib/Cache/Ref/Util/LRU/List.pm view on Meta::CPAN
# ex: set sw=4 et:
__END__
=pod
=encoding utf-8
=head1 NAME
Cache::Ref::Util::LRU::List
=head1 AUTHOR
Yuval Kogman
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Yuval Kogman.
This is free software; you can redistribute it and/or modify it under
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use ok 'Cache::Ref::LRU';
foreach my $lru ( map { "Cache::Ref::Util::LRU::$_" } qw(Array List) ) {
use_ok($lru);
{
my $c = Cache::Ref::LRU->new( size => 3, lru_class => $lru );
isa_ok( $c, "Cache::Ref" );
$c->set( foo => "blah" );
is( $c->get("foo"), "blah", "foo in cache" );
t/util_lru.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Moose;
#foreach my $impl qw(Cache::Ref::Util::LRU::Array Cache::Ref::Util::LRU::List) {
foreach my $impl (qw(Cache::Ref::Util::LRU::List)) {
use_ok($impl);
isa_ok( my $l = $impl->new, $impl );
does_ok( $l, "Cache::Ref::Util::LRU::API" );
is( $l->lru, undef, "no mru" );
is( $l->mru, undef, "no lru" );
my ( $foo, $bar ) = $l->insert(qw(foo bar));
is( $l->mru, "foo", "get mru" );
is( $l->lru, "bar", "get lru" );
$l->hit($bar);
( run in 0.451 second using v1.01-cache-2.11-cpan-4d50c553e7e )