Cache-Cascade
view release on metacpan or search on metacpan
lib/Cache/Cascade.pm view on Meta::CPAN
package Cache::Cascade; # git description: v0.06-3-g9cbe01e
# ABSTRACT: Get/set values to/from a group of caches, with some advanced semantics.
use strictures 2;
use Moo;
use Carp qw/croak/;
use Types::Standard qw(ArrayRef Bool);
sub _eval {
my ( $code, %args ) = @_;
$code =~ s/\[%\s*(\w+)\s*%\]/$args{$1} || die "$1 is not in eval" /ge;
eval $code;
}
use namespace::autoclean;
our $VERSION = '0.07';
has caches => (
isa => ArrayRef,
is => "rw",
);
has float_hits => (
isa => Bool,
is => "rw",
default => 0,
);
has set_deep => (
isa => Bool,
is => "rw",
default => 1,
);
sub get {
my ( $self, $key ) = @_;
if ( $self->float_hits ) {
$self->get_and_float_result( $key, @{ $self->caches } );
} else {
foreach my $cache ( @{ $self->caches } ) {
if ( defined( my $res = $cache->get($key) ) ) {
return $res;
}
}
return;
}
}
sub get_and_float_result {
my ( $self, $key, $head, @tail ) = @_;
$head || return;
if ( defined( my $res = $head->get($key) ) ) {
return $res;
} elsif ( @tail ) {
if ( defined( my $res = $self->get_and_float_result( $key, @tail ) ) ) {
$head->set( $key, $res );
return $res;
}
}
return;
}
sub set {
my ( $self, $key, $value, @extra ) = @_;
if ( $self->set_deep ) {
$_->set($key, $value, @extra) for @{ $self->caches };
} else {
( $self->caches->[0] || return )->set($key, $value, @extra);
( run in 0.656 second using v1.01-cache-2.11-cpan-39bf76dae61 )