| 1 | package CHI::Driver::Role::HasSubcaches; | ||||||||||||||||||||||||||||
| 2 | 19 | $CHI::Driver::Role::HasSubcaches::VERSION = '0.60'; | |||||||||||||||||||||||||||
| 3 | 19 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use CHI::Types qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use Hash::MoreUtils qw(slice_exists); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 19 | 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
| |||||||||||||||||||||||||||||
| 18 | 149 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 19 | my @subcache_inherited_params = ( | |||||||||||||||||||||||||||
| 32 | qw(expires_at expires_in expires_variance namespace on_get_error on_set_error serializer) | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | 19 | for my $type (qw(l1_cache mirror_cache)) { | |||||||||||||||||||||||||||
| 35 | 38 | my $config_acc = "_${type}_config"; | |||||||||||||||||||||||||||
| 36 | 38 | 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
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 143 | ▶ | my %inherit = map { ( defined $self->$_ ) ? ( $_ => $self->$_ ) : () } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 38 | }; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 38 | 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 | 19 | 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 | 19 | foreach my $method (qw(clear expire purge remove set)) { | |||||||||||||||||||||||||||
| 84 | after $method => sub { | ||||||||||||||||||||||||||||
| 85 | 1840 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 1840 | my $subcaches = $self->subcaches; | |||||||||||||||||||||||||||
| 87 | 1840 | foreach my $subcache (@$subcaches) { | |||||||||||||||||||||||||||
| 88 | 1840 | $subcache->$method(@_); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 95 | }; | |||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $l1_cache or $params{"obj"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | 810 | return $value; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||
| 110 | 874 | my ( $key, %params ) = @_; | |||||||||||||||||||||||||||
| 111 | 874 | ▶ | $params{obj_ref} ||= \my $obj_store; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params{"obj_ref"} ||= \my $obj_store
MC/DC100%1/1
$params{"obj_ref"} ||= \my $obj_store
$params{"obj_ref"}
Truth table100%2/2
A
A= $params{"obj_ref"}
| |||||||||||||||||||||||||||||
| 112 | 874 | my $value = $self->$orig( $key, %params ); | |||||||||||||||||||||||||||
| 113 | 874 | ▶ | if ( defined($value) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # If found in primary cache, write back to l1 cache. | ||||||||||||||||||||||||||||
| 116 | # | ||||||||||||||||||||||||||||
| 117 | 213 | my $obj = ${ $params{obj_ref} }; | |||||||||||||||||||||||||||
| 118 | 213 | $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 | 874 | return $value; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 19 | }; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 154 | ? $l1_values->[$_] | ||||||||||||||||||||||||||||
| 155 | : shift(@$primary_values) | ||||||||||||||||||||||||||||
| 156 | } @indices | ||||||||||||||||||||||||||||
| 157 | ]; | ||||||||||||||||||||||||||||
| 158 | 31 | return $values; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 19 | }; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 19 | 1; | |||||||||||||||||||||||||||