CHI/Driver/Role/HasSubcaches.pm

filter: Statementstmt 98.8% 88 / 89 Branchbran 94.4% 17 / 18 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 98.4% 128 / 130 SCAR 25.7
CC
13
Cov
98%
__ANON__
6.0
__ANON__
3.0
_non_overridable
3.0
CRAP 13.0
« CHI/Test/Util.pm ↑ Summary CHI/Driver/FastMmap.pm »
1 package CHI::Driver::Role::HasSubcaches;
2 20 $CHI::Driver::Role::HasSubcaches::VERSION = '0.59';
3 20 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use CHI::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Hash::MoreUtils qw(slice_exists);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 my @subcache_nonoverride_params =
13 qw(expires_at expires_in expires_variance serializer);
14  
15 sub _non_overridable {
16 150 ▶ my $params = shift;
Subroutine _non_overridable: called CC=3 SCAR=11.0
17 150 ▶ if ( is_HashRef($params) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_HashRef($params)) 149 1 150
18 149 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my(@nonoverride) = grep({exists $params->{$_};} @subcache_nonoverride_params)) 7 142 149
19 my @nonoverride =
20 596 grep { exists $params->{$_} } @subcache_nonoverride_params
21 )
22 {
23 7 warn sprintf( "cannot override these keys in a subcache: %s",
24 join( ", ", @nonoverride ) );
25 7 delete( @$params{@nonoverride} );
26 }
27 }
28 150 return $params;
29 }
30  
31 20 my @subcache_inherited_params = (
32 qw(expires_at expires_in expires_variance namespace on_get_error on_set_error serializer)
33 );
34 20 for my $type (qw(l1_cache mirror_cache)) {
35 40 my $config_acc = "_${type}_config";
36 40 has $config_acc => (
37 is => 'ro',
38 init_arg => $type,
39 isa => HashRef,
40 coerce => \&_non_overridable,
41 );
42  
43 my $default = sub {
44 276 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
45 276 ▶ my $config = $self->$config_acc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->$config_acc or return undef 133 143 276
46  
47 143 ▶ my %inherit = map { ( defined $self->$_ ) ? ( $_ => $self->$_ ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->$_ ? : 855 146 1001
48 @subcache_inherited_params;
49 143 my $build_config = {
50 %inherit,
51 label => $self->label . ":$type",
52 %$config,
53 is_subcache => 1,
54 parent_cache => $self,
55 subcache_type => $type,
56 };
57  
58 143 return $self->chi_root_class->new(%$build_config);
59 40 };
60  
61 40 has $type => (
62 is => 'ro',
63 lazy => 1,
64 init_arg => undef,
65 default => $default,
66 isa => Maybe [ InstanceOf ['CHI::Driver'] ],
67 );
68 }
69  
70 20 has subcaches => (
71 is => 'lazy',
72 init_arg => undef,
73 );
74  
75 sub _build_subcaches {
76 133 ▶ [ grep { defined $_ } $_[0]->l1_cache, $_[0]->mirror_cache ];
Subroutine _build_subcaches: called CC=1 SCAR=0.0
77 }
78  
79 24 ▶sub _build_has_subcaches { 1 }
Subroutine _build_has_subcaches: called CC=1 SCAR=0.0
80  
81 # Call these methods first on the main cache, then on any subcaches.
82 #
83 20 foreach my $method (qw(clear expire purge remove set)) {
84 after $method => sub {
85 1842 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
86 1842 my $subcaches = $self->subcaches;
87 1842 foreach my $subcache (@$subcaches) {
88 1842 $subcache->$method(@_);
89 }
90 100 };
91 }
92  
93 around 'get' => sub {
94 2153 ▶ my $orig = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
95 2153 my $self = shift;
96 2153 my ( $key, %params ) = @_;
97 2153 my $l1_cache = $self->l1_cache;
98  
99 2153 ▶ if ( !defined($l1_cache) || $params{obj} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $l1_cache or $params{'obj'}) { } 468 1685 2153
Condition100%3/3
not defined $l1_cache or $params{"obj"}
l!l&&r!l&&!r
444241685
MC/DC100%2/2
not defined $l1_cache or $params{"obj"}
not defined $l1_cache $params{"obj"}
Truth table100%3/3
not(A) or B
A= not defined $l1_cacheB= $params{"obj"}
ABresult
1X1
011
000
100 468 return $self->$orig(@_);
101 }
102 else {
103  
104 # Consult l1 cache first
105 #
106 1685 ▶ if ( defined( my $value = $l1_cache->get(@_) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = $l1_cache->get(@_))) { } 815 869 1684
107 815 return $value;
108 }
109 else {
110 869 my ( $key, %params ) = @_;
111 869 ▶ $params{obj_ref} ||= \my $obj_store;
Condition100%2/2
$params{"obj_ref"} ||= \my $obj_store
l!l
8861
MC/DC100%1/1
$params{"obj_ref"} ||= \my $obj_store
$params{"obj_ref"}
Truth table100%2/2
A
A= $params{"obj_ref"}
Aresult
11
00
112 869 my $value = $self->$orig( $key, %params );
113 869 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 194 675 869
114  
115 # If found in primary cache, write back to l1 cache.
116 #
117 194 my $obj = ${ $params{obj_ref} };
118 194 $l1_cache->set(
119 $key,
120 $obj->value,
121 {
122 expires_at => $obj->expires_at,
123 early_expires_at => $obj->early_expires_at
124 }
125 );
126 }
127 869 return $value;
128 }
129 }
130 20 };
131  
132 around 'get_multi_arrayref' => sub {
133 32 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
134 32 my $self = shift;
135 32 my ($keys) = @_;
136  
137 32 my $l1_cache = $self->l1_cache;
138 32 ▶ if ( !defined($l1_cache) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $l1_cache) { } 0 32 32
139 0 return $self->$orig(@_);
140 }
141 else {
142  
143 # Consult l1 cache first, then call on primary cache with remainder of keys,
144 # and combine the arrays.
145 #
146 32 my $l1_values = $l1_cache->get_multi_arrayref($keys);
147 31 my @indices = ( 0 .. scalar(@$keys) - 1 );
148 my @primary_keys =
149 31 map { $keys->[$_] } grep { !defined( $l1_values->[$_] ) } @indices;
150 31 my $primary_values = $self->$orig( \@primary_keys );
151 my $values = [
152 map {
153 31 ▶ defined( $l1_values->[$_] )
Branch: 1 branch
BranchTrueFalseTotal
defined $l1_values->[$_] ? : 61 62 123
154 ? $l1_values->[$_]
155 : shift(@$primary_values)
156 } @indices
157 ];
158 31 return $values;
159 }
160 20 };
161  
162 20 1;
« CHI/Test/Util.pm ↑ Summary CHI/Driver/FastMmap.pm »