lib/File/DataClass/Cache.pm

filter: Statementstmt 100% 93 / 93 Branchbran 90.0% 27 / 30 Conditioncond 68.7% 11 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 22 / 22 Podpod 100% 7 / 7 totaltotal 92.6% 164 / 177 CC 23 SCAR 14.8
CC
23
Cov
94%
__ANON__
11.2
__ANON__
4.2
__ANON__
3.0
CRAP 4.4
« lib/File/DataClass/IO.pm ↑ Summary lib/File/DataClass/ResultSet.pm »
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"} //= {}
l!l
30
MC/DC0%0/1
$attr->{"cache_attributes"} //= {}
$attr->{"cache_attributes"}
Truth table50%1/2
A
A= $attr->{"cache_attributes"}
Aresult
11
00
33  
34 $cache_class = delete $attr->{cache_attributes}->{cache_class}
35 3 ▶ and $attr->{cache_class} = $cache_class;
Branch: 1 branch
BranchTrueFalseTotal
$cache_class = delete $attr->{"cache_attributes"}{"cache_class"} and $attr->{"cache_class"} = $cache_class 0 3 3
36  
37 3 ▶ my $builder = delete $attr->{builder} or return $attr;
Branch: 1 branch
BranchTrueFalseTotal
my $builder = delete $attr->{"builder"} or return $attr 3 0 3
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 "$_"
!ll&&!rl&&r
008
MC/DC0%0/2
defined $_ && length "$_"
defined $_ length "$_"
Truth table33%1/3
A && B
A= defined $_B= length "$_"
ABresult
0X0
100
111
49 8 ▶ my $mtime = $self->get_mtime( "${path}" ) or $is_valid = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
my $mtime = $self->get_mtime("$path") or $is_valid = 0 5 3 8
50  
51 ($mtime and $path->exists and $mtime == $path->stat->{mtime})
52 8 ▶ or $is_valid = FALSE;
Branch: 1 branch
BranchTrueFalseTotal
$mtime and $path->exists and $mtime == $path->stat->{"mtime"} or $is_valid = 0 5 3 8
Condition66%4/6
$mtime and $path->exists and $mtime == $path->stat->{"mtime"}
!ll&&!rl&&r
305
$mtime and $path->exists
!ll&&!rl&&r
305
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"}
ABCresult
0XX0
10X0
1100
1111
53 8 ▶ $mtime and $mtime > $newest and $newest = $mtime;
Branch: 1 branch
BranchTrueFalseTotal
$mtime and $mtime > $newest and $newest = $mtime 3 5 8
Condition100%3/3
$mtime and $mtime > $newest
!ll&&!rl&&r
323
MC/DC100%2/2
$mtime and $mtime > $newest
$mtime $mtime > $newest
Truth table100%3/3
A and B
A= $mtimeB= $mtime > $newest
ABresult
0X0
100
111
54 8 ▶ $key .= $key ? "~${path}" : "${path}";
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 4 4 8
55 }
56  
57 4 ▶ return ($key, $is_valid ? $newest : undef);
Branch: 1 branch
BranchTrueFalseTotal
$is_valid ? : 2 2 4
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
BranchTrueFalseTotal
$key ? : 74 1 75
65  
66 75 ▶ $cached and return ($cached->{data}, $cached->{meta});
Branch: 1 branch
BranchTrueFalseTotal
$cached and return $cached->{"data"}, $cached->{"meta"} 46 29 75
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
BranchTrueFalseTotal
$k or return 9 1 10
80  
81 9 ▶ my $mtimes = $self->cache->get( $self->_mtimes_key ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mtimes = $self->cache->get($self->_mtimes_key) or return 8 1 9
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
BranchTrueFalseTotal
defined $key or return 23 1 24
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}
l!l
253
MC/DC100%1/1
$meta //= {"mtime", undef}
$meta
Truth table100%2/2
A
A= $meta
Aresult
11
00
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
BranchTrueFalseTotal
$key eq $self->_mtimes_key and &throw("key not allowed") 1 27 28
101 27 ▶ $self->cache->set( $key, $val ) or throw 'set operation returned false';
Branch: 1 branch
BranchTrueFalseTotal
$self->cache->set($key, $val) or &throw("set operation returned false") 27 0 27
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
BranchTrueFalseTotal
if (defined $v) { } 25 25 50
130  
131 50 return $mtimes;
132 50 } );
133 }
134  
135 3 1;
136  
137 __END__
« lib/File/DataClass/IO.pm ↑ Summary lib/File/DataClass/ResultSet.pm »