| 1 | package File::DataClass::Cache; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.01; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use File::DataClass::Constants qw( FALSE NUL SPC TRUE ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::DataClass::Functions qw( merge_attributes throw ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use File::DataClass::Types qw( Bool Cache ClassName HashRef | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | LoadableClass Object Str ); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Storable qw( freeze ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | # Public attributes | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | has 'cache' => is => 'lazy', isa => Object, builder => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_cache: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | $_[ 0 ]->cache_class->new( %{ $_[ 0 ]->cache_attributes } ) }; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | has 'cache_attributes' => is => 'ro', isa => HashRef, required => TRUE; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | has 'cache_class' => is => 'lazy', isa => LoadableClass, | |||||||||||||||||||||||||||||||||||||||||
| 21 | default => 'Cache::FastMmap'; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | has 'log' => is => 'ro', isa => Object, required => TRUE; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Private attributes | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | has '_mtimes_key' => is => 'ro', isa => Str, default => '_mtimes'; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # Construction | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | ▶ | around 'BUILDARGS' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | my ($orig, $class, @args) = @_; my $attr = $orig->( $class, @args ); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | ▶ | $attr->{cache_attributes} //= {}; my $cache_class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$attr->{"cache_attributes"} //= {}
MC/DC0%0/1
$attr->{"cache_attributes"} //= {}
$attr->{"cache_attributes"}
Truth table50%1/2
A
A= $attr->{"cache_attributes"}
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | $cache_class = delete $attr->{cache_attributes}->{cache_class} | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | and $attr->{cache_class} = $cache_class; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | my $builder = delete $attr->{builder} or return $attr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | merge_attributes $attr, $builder, [ 'log' ]; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | return $attr; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # Private methods | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | my $_get_key_and_newest = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | my ($self, $paths) = @_; my $newest = 0; my $is_valid = TRUE; my $key; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | for my $path (grep { defined && length "${_}" } @{ $paths }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && length "$_"
MC/DC0%0/2
defined $_ && length "$_"
defined $_
length "$_"
Truth table33%1/3
A && B
A= defined $_B= length "$_"
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | ▶ | my $mtime = $self->get_mtime( "${path}" ) or $is_valid = FALSE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | ($mtime and $path->exists and $mtime == $path->stat->{mtime}) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 8 | ▶ | or $is_valid = FALSE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$mtime and $path->exists and $mtime == $path->stat->{"mtime"}
$mtime and $path->exists
MC/DC33%1/3
$mtime and $path->exists and $mtime == $path->stat->{"mtime"}
$mtime
$path->exists
$mtime == $path->stat->{"mtime"}
Truth table50%2/4
A and B and C
A= $mtimeB= $path->existsC= $mtime == $path->stat->{"mtime"}
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | $mtime and $mtime > $newest and $newest = $mtime; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mtime and $mtime > $newest
MC/DC100%2/2
$mtime and $mtime > $newest
$mtime
$mtime > $newest
Truth table100%3/3
A and B
A= $mtimeB= $mtime > $newest
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 8 | ▶ | $key .= $key ? "~${path}" : "${path}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | return ($key, $is_valid ? $newest : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # Public methods | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 75 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 75 | my ($self, $key) = @_; $key .= NUL; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 75 | ▶ | my $cached = $key ? $self->cache->get( $key ) : FALSE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 75 | ▶ | $cached and return ($cached->{data}, $cached->{meta}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 29 | return (undef, { mtime => undef }); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub get_by_paths { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_by_paths: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | my ($self, $paths) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | my ($key, $newest) = $self->$_get_key_and_newest( $paths ); | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | return ($self->get( $key ), $newest); | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 10 | ▶ | sub get_mtime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_mtime: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | ▶ | my ($self, $k) = @_; $k or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 9 | ▶ | my $mtimes = $self->cache->get( $self->_mtimes_key ) or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 8 | return $mtimes->{ $k }; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 24 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 24 | ▶ | my ($self, $key) = @_; defined $key or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 23 | $self->cache->remove( $key ); $self->set_mtime( $key, undef ); | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 23 | return; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 28 | ▶ | my ($self, $key, $data, $meta) = @_; $meta //= { mtime => undef }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$meta //= {"mtime", undef}
MC/DC100%1/1
$meta //= {"mtime", undef}
$meta
Truth table100%2/2
A
A= $meta
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 28 | my $val = { data => $data, meta => $meta }; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 28 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 28 | ▶ | $key eq $self->_mtimes_key and throw 'key not allowed'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 27 | ▶ | $self->cache->set( $key, $val ) or throw 'set operation returned false'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 27 | $self->set_mtime( $key, $meta->{mtime} ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | my $len = length( $key ) + length( freeze $val ); | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | $self->log->error( "Cache key ${key}(${len}) set failed: ${_}" ); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 28 | }; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 28 | return ($data, $meta); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | sub set_by_paths { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_by_paths: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | my ($self, $paths, $data, $meta) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | my ($key, $newest) = $self->$_get_key_and_newest( $paths ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | $meta->{mtime} = $newest; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | return $self->set( $key, $data, $meta ); | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 50 | ▶ | sub set_mtime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_mtime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 50 | my ($self, $k, $v) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 50 | ▶ | return $self->cache->get_and_set( $self->_mtimes_key, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 50 | my (undef, $mtimes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 50 | ▶ | if (defined $v) { $mtimes->{ $k } = $v } else { delete $mtimes->{ $k } } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 50 | return $mtimes; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 50 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||||||||||||||||