| 1 | package CHI::Driver; | ||||||||||||||||||||||||||||||||||
| 2 | 20 | $CHI::Driver::VERSION = '0.59'; | |||||||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use CHI::CacheObject; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use CHI::Constants qw(CHI_Max_Time); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use CHI::Driver::Metacache; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use CHI::Driver::Role::HasSubcaches; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use CHI::Driver::Role::IsSizeAware; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 20 | ▶ | use CHI::Driver::Role::IsSubcache; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 20 | ▶ | use CHI::Driver::Role::Universal; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 20 | ▶ | use CHI::Serializer::Storable; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 20 | ▶ | use CHI::Serializer::JSON; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 20 | ▶ | use CHI::Util qw(parse_duration); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 20 | ▶ | use CHI::Types qw(:all); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 20 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 20 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | 20 | ▶ | use Hash::MoreUtils qw(slice_grep); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | 20 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | 20 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 20 | 20 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | 20 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 22 | 20 | ▶ | use Time::Duration; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 23 | 20 | ▶ | use Time::HiRes qw(gettimeofday); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 24 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 25 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 20 | my $default_serializer = CHI::Serializer::Storable->new(); | |||||||||||||||||||||||||||||||||
| 28 | 20 | my $default_key_serializer = CHI::Serializer::JSON->new(); | |||||||||||||||||||||||||||||||||
| 29 | 20 | my $default_key_digester = Digest::MD5->new(); | |||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 20 | my @common_params; | |||||||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||||||||
| 33 | 20 | my %attr = ( | |||||||||||||||||||||||||||||||||
| 34 | chi_root_class => { | ||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||
| 37 | compress_threshold => { | ||||||||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 39 | isa => Int, | ||||||||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||||||||
| 41 | constructor_params => { | ||||||||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 43 | init_arg => undef, | ||||||||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||||||||
| 45 | driver_class => { | ||||||||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||||
| 48 | expires_at => { | ||||||||||||||||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 50 | 827 | ▶ | default => sub { CHI_Max_Time }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||||
| 52 | expires_in => { | ||||||||||||||||||||||||||||||||||
| 53 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 54 | isa => Duration, | ||||||||||||||||||||||||||||||||||
| 55 | coerce => \&to_Duration, | ||||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||||
| 57 | expires_on_backend => { | ||||||||||||||||||||||||||||||||||
| 58 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 59 | isa => Num, | ||||||||||||||||||||||||||||||||||
| 60 | 998 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||||
| 62 | expires_variance => { | ||||||||||||||||||||||||||||||||||
| 63 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 64 | isa => Num, | ||||||||||||||||||||||||||||||||||
| 65 | 835 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 66 | }, | ||||||||||||||||||||||||||||||||||
| 67 | has_subcaches => { | ||||||||||||||||||||||||||||||||||
| 68 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 69 | isa => Bool, | ||||||||||||||||||||||||||||||||||
| 70 | init_arg => undef, | ||||||||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||||||||
| 72 | is_size_aware => { | ||||||||||||||||||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 74 | isa => Bool, | ||||||||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||||||||
| 76 | is_subcache => { | ||||||||||||||||||||||||||||||||||
| 77 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 78 | isa => Bool, | ||||||||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||||||||
| 80 | key_digester => { | ||||||||||||||||||||||||||||||||||
| 81 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 82 | isa => Digester, | ||||||||||||||||||||||||||||||||||
| 83 | coerce => \&to_Digester, | ||||||||||||||||||||||||||||||||||
| 84 | 1000 | ▶ | default => sub { $default_key_digester }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||||||||||||||||
| 86 | key_serializer => { | ||||||||||||||||||||||||||||||||||
| 87 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 88 | isa => Serializer, | ||||||||||||||||||||||||||||||||||
| 89 | coerce => \&to_Serializer, | ||||||||||||||||||||||||||||||||||
| 90 | 1000 | ▶ | default => sub { $default_key_serializer }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||||||||
| 92 | label => { | ||||||||||||||||||||||||||||||||||
| 93 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 94 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 95 | builder => 1, | ||||||||||||||||||||||||||||||||||
| 96 | clearer => 1, | ||||||||||||||||||||||||||||||||||
| 97 | predicate => 1, | ||||||||||||||||||||||||||||||||||
| 98 | }, | ||||||||||||||||||||||||||||||||||
| 99 | max_build_depth => { | ||||||||||||||||||||||||||||||||||
| 100 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 101 | 1000 | ▶ | default => sub { 8 }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 102 | }, | ||||||||||||||||||||||||||||||||||
| 103 | max_key_length => { | ||||||||||||||||||||||||||||||||||
| 104 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 105 | isa => Int, | ||||||||||||||||||||||||||||||||||
| 106 | 575 | ▶ | default => sub { 1 << 31 }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||||||||
| 108 | metacache => { | ||||||||||||||||||||||||||||||||||
| 109 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 110 | clearer => 1, | ||||||||||||||||||||||||||||||||||
| 111 | predicate => 1, | ||||||||||||||||||||||||||||||||||
| 112 | }, | ||||||||||||||||||||||||||||||||||
| 113 | namespace => { | ||||||||||||||||||||||||||||||||||
| 114 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 115 | isa => Str, | ||||||||||||||||||||||||||||||||||
| 116 | 690 | ▶ | default => sub { 'Default' }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||||||||||||||||
| 118 | on_get_error => { | ||||||||||||||||||||||||||||||||||
| 119 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 120 | isa => OnError, | ||||||||||||||||||||||||||||||||||
| 121 | 59 | ▶ | default => sub { 'log' }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 122 | }, | ||||||||||||||||||||||||||||||||||
| 123 | on_set_error => { | ||||||||||||||||||||||||||||||||||
| 124 | is => 'rw', | ||||||||||||||||||||||||||||||||||
| 125 | isa => OnError, | ||||||||||||||||||||||||||||||||||
| 126 | 58 | ▶ | default => sub { 'log' }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||||||||
| 128 | serializer => { | ||||||||||||||||||||||||||||||||||
| 129 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 130 | isa => Serializer, | ||||||||||||||||||||||||||||||||||
| 131 | coerce => \&to_Serializer, | ||||||||||||||||||||||||||||||||||
| 132 | 748 | ▶ | default => sub { $default_serializer }, | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 133 | }, | ||||||||||||||||||||||||||||||||||
| 134 | 20 | short_driver_name => { | |||||||||||||||||||||||||||||||||
| 135 | is => 'lazy', | ||||||||||||||||||||||||||||||||||
| 136 | clearer => 1, | ||||||||||||||||||||||||||||||||||
| 137 | predicate => 1, | ||||||||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||||||||
| 139 | storage => { | ||||||||||||||||||||||||||||||||||
| 140 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 141 | }, | ||||||||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||||||||
| 143 | 20 | push @common_params, keys %attr; | |||||||||||||||||||||||||||||||||
| 144 | 20 | for my $attr ( keys %attr ) { | |||||||||||||||||||||||||||||||||
| 145 | 460 | has $attr => %{ $attr{$attr} }; | |||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | 24 | ▶ | sub _build_has_subcaches { undef } | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_has_subcaches: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | # These methods must be implemented by subclass | ||||||||||||||||||||||||||||||||||
| 152 | 20 | foreach my $method (qw(fetch store remove get_keys get_namespaces)) { | |||||||||||||||||||||||||||||||||
| 153 | 20 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | *{$method} = sub { die "method '$method' must be implemented by subclass" }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | # Given a hash of params, return the subset that are not in CHI's common parameters. | ||||||||||||||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||||||||||||||
| 159 | 20 | push @common_params, qw( | |||||||||||||||||||||||||||||||||
| 160 | discard_policy | ||||||||||||||||||||||||||||||||||
| 161 | discard_timeout | ||||||||||||||||||||||||||||||||||
| 162 | l1_cache | ||||||||||||||||||||||||||||||||||
| 163 | max_size | ||||||||||||||||||||||||||||||||||
| 164 | max_size_reduction_factor | ||||||||||||||||||||||||||||||||||
| 165 | mirror_cache | ||||||||||||||||||||||||||||||||||
| 166 | parent_cache | ||||||||||||||||||||||||||||||||||
| 167 | subcache_type | ||||||||||||||||||||||||||||||||||
| 168 | subcaches | ||||||||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||||||||
| 170 | 20 | my %common_params = map { ( $_, 1 ) } @common_params; | |||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | sub non_common_constructor_params { | ||||||||||||||||||||||||||||||||||
| 173 | 1 | ▶ | my ( $class, $params ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
non_common_constructor_params: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | return { | ||||||||||||||||||||||||||||||||||
| 176 | 3 | map { ( $_, $params->{$_} ) } | |||||||||||||||||||||||||||||||||
| 177 | 1 | grep { !$common_params{$_} } keys(%$params) | |||||||||||||||||||||||||||||||||
| 178 | }; | ||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||
| 181 | sub declare_unsupported_methods { | ||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | my ( $class, @methods ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
declare_unsupported_methods: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | 1 | foreach my $method (@methods) { | |||||||||||||||||||||||||||||||||
| 185 | 20 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 186 | 1 | *{"$class\::$method"} = | |||||||||||||||||||||||||||||||||
| 187 | 1 | ▶ | sub { croak "method '$method' not supported by '$class'" }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||
| 191 | 16062 | ▶ | sub cache_object_class { 'CHI::CacheObject' } | ||||||||||||||||||||||||||||||||
|
Subroutine
cache_object_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||
| 193 | # To override time() for testing - must be writable in a dynamically scoped way from tests | ||||||||||||||||||||||||||||||||||
| 194 | 20 | our $Test_Time; ## no critic (ProhibitPackageVars) | |||||||||||||||||||||||||||||||||
| 195 | 20 | our $Build_Depth = 0; ## no critic (ProhibitPackageVars) | |||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub valid_get_options { qw(expire_if busy_lock) } | ||||||||||||||||||||||||||||||||
|
Subroutine
valid_get_options: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | sub valid_set_options { qw(expires_at expires_in expires_variance) } | ||||||||||||||||||||||||||||||||
|
Subroutine
valid_set_options: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | sub BUILD { | ||||||||||||||||||||||||||||||||||
| 201 | 998 | ▶ | my ( $self, $params ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||
| 203 | # Ward off infinite build recursion, e.g. from circular subcache configuration. | ||||||||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||||||||
| 205 | 998 | local $Build_Depth = $Build_Depth + 1; | |||||||||||||||||||||||||||||||||
| 206 | 998 | ▶ | die "$Build_Depth levels of CHI cache creation; infinite recursion?" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 207 | if ( $Build_Depth > $self->max_build_depth ); | ||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||
| 209 | # Save off constructor params. Used to create metacache, for | ||||||||||||||||||||||||||||||||||
| 210 | # example. Hopefully this will not cause circular references... | ||||||||||||||||||||||||||||||||||
| 211 | # | ||||||||||||||||||||||||||||||||||
| 212 | 998 | $self->{constructor_params} = {%$params}; | |||||||||||||||||||||||||||||||||
| 213 | 998 | foreach my $param (qw(l1_cache mirror_cache parent_cache)) { | |||||||||||||||||||||||||||||||||
| 214 | 2994 | delete( $self->{constructor_params}->{$param} ); | |||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||
| 217 | # If stats enabled, add ns_stats slot for keeping track of stats | ||||||||||||||||||||||||||||||||||
| 218 | # | ||||||||||||||||||||||||||||||||||
| 219 | 998 | my $stats = $self->chi_root_class->stats; | |||||||||||||||||||||||||||||||||
| 220 | 998 | ▶ | if ( $stats->enabled ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 221 | 6 | $self->{ns_stats} = $stats->stats_for_driver($self); | |||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||
| 224 | # Call BUILD_roles on any of the roles that need initialization. | ||||||||||||||||||||||||||||||||||
| 225 | # | ||||||||||||||||||||||||||||||||||
| 226 | 998 | $self->BUILD_roles($params); | |||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||
| 229 | ▶ | sub BUILD_roles { | |||||||||||||||||||||||||||||||||
|
Subroutine
BUILD_roles: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||
| 231 | # Will be modified by roles that need it | ||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||
| 234 | sub _build_short_driver_name { | ||||||||||||||||||||||||||||||||||
| 235 | 450 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_short_driver_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||
| 237 | 450 | ( my $name = $self->driver_class ) =~ s/^CHI::Driver:://; | |||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | 450 | return $name; | |||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||
| 242 | sub _build_label { | ||||||||||||||||||||||||||||||||||
| 243 | 438 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_label: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||
| 245 | 438 | return $self->short_driver_name; | |||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||
| 248 | sub _build_metacache { | ||||||||||||||||||||||||||||||||||
| 249 | 85 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_metacache: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | 85 | return CHI::Driver::Metacache->new( owner_cache => $self ); | |||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||
| 254 | sub get { | ||||||||||||||||||||||||||||||||||
| 255 | 11932 | ▶ | my ( $self, $key, %params ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=21 SCAR=30.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||
| 257 | 11932 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 258 | 11924 | my $ns_stats = $self->{ns_stats}; | |||||||||||||||||||||||||||||||||
| 259 | 11924 | my $log_is_debug = $log->is_debug; | |||||||||||||||||||||||||||||||||
| 260 | 11924 | ▶ | my $measure_time = defined($ns_stats) || $log_is_debug; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $ns_stats || $log_is_debug
MC/DC0%0/2
defined $ns_stats || $log_is_debug
defined $ns_stats
$log_is_debug
Truth table66%2/3
A || B
A= defined $ns_statsB= $log_is_debug
| |||||||||||||||||||||||||||||||||||
| 261 | 11924 | my ( $start_time, $elapsed_time ); | |||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||
| 263 | # Fetch cache object | ||||||||||||||||||||||||||||||||||
| 264 | # | ||||||||||||||||||||||||||||||||||
| 265 | 11924 | ▶ | $start_time = gettimeofday() if $measure_time; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 266 | 11924 | ▶ | my $obj = eval { $params{obj} || $self->get_object($key) }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 267 | 11924 | ▶ | $elapsed_time = ( gettimeofday() - $start_time ) * 1000 if $measure_time; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 268 | 11924 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 269 | 4 | ▶ | $ns_stats->{'get_errors'}++ if defined($ns_stats); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 270 | 4 | $self->_handle_get_error( $error, $key ); | |||||||||||||||||||||||||||||||||
| 271 | 3 | return undef; | |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | 11920 | ▶ | if ( !defined $obj ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 274 | 1685 | ▶ | $self->_record_get_stats( 'absent_misses', $elapsed_time ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 275 | if defined($ns_stats); | ||||||||||||||||||||||||||||||||||
| 276 | 1685 | ▶ | $self->_log_get_result( $log, "MISS (not in cache)", | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 277 | $key, $elapsed_time ) | ||||||||||||||||||||||||||||||||||
| 278 | if $log_is_debug; | ||||||||||||||||||||||||||||||||||
| 279 | 1685 | return undef; | |||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||
| 281 | 10235 | ▶ | if ( defined( my $obj_ref = $params{obj_ref} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 282 | 629 | $$obj_ref = $obj; | |||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||
| 285 | # Check if expired | ||||||||||||||||||||||||||||||||||
| 286 | # | ||||||||||||||||||||||||||||||||||
| 287 | my $is_expired = $obj->is_expired() | ||||||||||||||||||||||||||||||||||
| 288 | || ( defined( $params{expire_if} ) | ||||||||||||||||||||||||||||||||||
| 289 | 10235 | ▶ | && $params{expire_if}->( $obj, $self ) ); | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
$obj->is_expired || defined $params{'expire_if'} && $params{'expire_if'}->($obj, $self)
defined $params{"expire_if"} and $params{"expire_if"}->($obj, $self)
MC/DC100%3/3
$obj->is_expired || defined $params{'expire_if'} && $params{'expire_if'}->($obj, $self)
$obj->is_expired
defined $params{"expire_if"}
$params{"expire_if"}->($obj, $self)
Truth table100%4/4
A || B and C
A= $obj->is_expiredB= defined $params{"expire_if"}C= $params{"expire_if"}->($obj, $self)
| |||||||||||||||||||||||||||||||||||
| 290 | 10235 | ▶ | if ($is_expired) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 291 | 4004 | ▶ | $self->_record_get_stats( 'expired_misses', $elapsed_time ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 292 | if defined($ns_stats); | ||||||||||||||||||||||||||||||||||
| 293 | 4004 | ▶ | $self->_log_get_result( $log, "MISS (expired)", $key, $elapsed_time ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 294 | if $log_is_debug; | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | # If busy_lock value provided, set a new "temporary" expiration time that many | ||||||||||||||||||||||||||||||||||
| 297 | # seconds forward before returning undef | ||||||||||||||||||||||||||||||||||
| 298 | # | ||||||||||||||||||||||||||||||||||
| 299 | 4004 | ▶ | if ( defined( my $busy_lock = $params{busy_lock} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 300 | 9 | ▶ | my $time = $Test_Time || time(); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$CHI::Driver::Test_Time || time
MC/DC0%0/2
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
time
Truth table33%1/3
A || B
A= $CHI::Driver::Test_TimeB= time
| |||||||||||||||||||||||||||||||||||
| 301 | 9 | my $busy_lock_time = $time + parse_duration($busy_lock); | |||||||||||||||||||||||||||||||||
| 302 | 9 | $obj->set_early_expires_at($busy_lock_time); | |||||||||||||||||||||||||||||||||
| 303 | 9 | $obj->set_expires_at($busy_lock_time); | |||||||||||||||||||||||||||||||||
| 304 | 9 | $self->set_object( $key, $obj ); | |||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||
| 307 | 4004 | return undef; | |||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | 6231 | ▶ | $self->_record_get_stats( 'hits', $elapsed_time ) if defined($ns_stats); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 311 | 6231 | ▶ | $self->_log_get_result( $log, "HIT", $key, $elapsed_time ) if $log_is_debug; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 312 | 6231 | return $obj->value; | |||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||
| 315 | sub _record_get_stats { | ||||||||||||||||||||||||||||||||||
| 316 | 3 | ▶ | my ( $self, $stat, $elapsed_time ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_record_get_stats: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 317 | 3 | $self->{ns_stats}->{$stat}++; | |||||||||||||||||||||||||||||||||
| 318 | 3 | $self->{ns_stats}->{'get_time_ms'} += $elapsed_time; | |||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||
| 321 | sub unpack_from_data { | ||||||||||||||||||||||||||||||||||
| 322 | 11818 | ▶ | my ( $self, $key, $data ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
unpack_from_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||
| 324 | 11818 | return $self->cache_object_class->unpack_from_data( $key, $data, | |||||||||||||||||||||||||||||||||
| 325 | $self->serializer ); | ||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||
| 328 | sub get_object { | ||||||||||||||||||||||||||||||||||
| 329 | 14524 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_object: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||
| 331 | 14524 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 332 | 14516 | $key = $self->transform_key($key); | |||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | 14516 | ▶ | my $data = $self->fetch($key) or return undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 335 | 11472 | my $obj = $self->unpack_from_data( $key, $data ); | |||||||||||||||||||||||||||||||||
| 336 | 11472 | return $obj; | |||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||
| 339 | sub get_expires_at { | ||||||||||||||||||||||||||||||||||
| 340 | 88 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_expires_at: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 341 | 88 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||
| 343 | 80 | ▶ | if ( my $obj = $self->get_object($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 344 | 72 | return $obj->expires_at; | |||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||
| 346 | else { | ||||||||||||||||||||||||||||||||||
| 347 | 8 | return; | |||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | sub exists_and_is_expired { | ||||||||||||||||||||||||||||||||||
| 352 | 24 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
exists_and_is_expired: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 353 | 24 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||
| 355 | 16 | ▶ | if ( my $obj = $self->get_object($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 356 | 16 | return $obj->is_expired; | |||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||
| 358 | else { | ||||||||||||||||||||||||||||||||||
| 359 | 0 | return; | |||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | sub is_valid { | ||||||||||||||||||||||||||||||||||
| 364 | 80 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 365 | 80 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||
| 367 | 72 | ▶ | if ( my $obj = $self->get_object($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 368 | 40 | return !$obj->is_expired; | |||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||
| 370 | else { | ||||||||||||||||||||||||||||||||||
| 371 | 32 | return; | |||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | sub _default_set_options { | ||||||||||||||||||||||||||||||||||
| 376 | 3889 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_default_set_options: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||
| 378 | 3889 | return { map { $_ => $self->$_() } | |||||||||||||||||||||||||||||||||
| 379 | qw( expires_at expires_in expires_variance ) }; | ||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||
| 382 | sub set { | ||||||||||||||||||||||||||||||||||
| 383 | 4252 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 384 | 4252 | my ( $key, $value, $options ) = @_; | |||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||
| 386 | 4252 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 387 | 4244 | $key = $self->transform_key($key); | |||||||||||||||||||||||||||||||||
| 388 | 4244 | ▶ | return unless defined($value); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | # Fill in $options if not passed, copy if passed, and apply defaults. | ||||||||||||||||||||||||||||||||||
| 391 | # | ||||||||||||||||||||||||||||||||||
| 392 | 4244 | ▶ | if ( !defined($options) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 393 | 3846 | $options = $self->_default_set_options; | |||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||
| 395 | else { | ||||||||||||||||||||||||||||||||||
| 396 | 398 | ▶ | if ( !ref($options) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 397 | 78 | ▶ | if ( $options eq 'never' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 398 | 9 | $options = { expires_at => CHI_Max_Time }; | |||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||
| 400 | elsif ( $options eq 'now' ) { | ||||||||||||||||||||||||||||||||||
| 401 | 0 | $options = { expires_in => 0 }; | |||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||
| 403 | else { | ||||||||||||||||||||||||||||||||||
| 404 | 69 | $options = { expires_in => $options }; | |||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||
| 408 | # Disregard default expires_at and expires_in if either are provided | ||||||||||||||||||||||||||||||||||
| 409 | # | ||||||||||||||||||||||||||||||||||
| 410 | 398 | ▶ | if ( exists( $options->{expires_at} ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $options->{"expires_at"} or exists $options->{"expires_in"}
MC/DC100%2/2
exists $options->{"expires_at"} or exists $options->{"expires_in"}
exists $options->{"expires_at"}
exists $options->{"expires_in"}
Truth table100%3/3
A or B
A= exists $options->{"expires_at"}B= exists $options->{"expires_in"}
| |||||||||||||||||||||||||||||||||||
| 411 | || exists( $options->{expires_in} ) ) | ||||||||||||||||||||||||||||||||||
| 412 | { | ||||||||||||||||||||||||||||||||||
| 413 | 355 | $options = | |||||||||||||||||||||||||||||||||
| 414 | { expires_variance => $self->expires_variance, %$options }; | ||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||
| 416 | else { | ||||||||||||||||||||||||||||||||||
| 417 | 43 | $options = { %{ $self->_default_set_options }, %$options }; | |||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||
| 421 | 4244 | $self->set_with_options( $key, $value, $options ); | |||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||
| 424 | sub set_with_options { | ||||||||||||||||||||||||||||||||||
| 425 | 4244 | ▶ | my ( $self, $key, $value, $options ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
set_with_options: called CC=12 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 426 | 4244 | my $ns_stats = $self->{ns_stats}; | |||||||||||||||||||||||||||||||||
| 427 | 4244 | my $log_is_debug = $log->is_debug; | |||||||||||||||||||||||||||||||||
| 428 | 4244 | ▶ | my $measure_time = defined($ns_stats) || $log_is_debug; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $ns_stats || $log_is_debug
MC/DC0%0/2
defined $ns_stats || $log_is_debug
defined $ns_stats
$log_is_debug
Truth table66%2/3
A || B
A= defined $ns_statsB= $log_is_debug
| |||||||||||||||||||||||||||||||||||
| 429 | 4244 | my ( $start_time, $elapsed_time ); | |||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||
| 431 | # Determine early and final expiration times | ||||||||||||||||||||||||||||||||||
| 432 | # | ||||||||||||||||||||||||||||||||||
| 433 | 4244 | ▶ | my $time = $Test_Time || time(); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$CHI::Driver::Test_Time || time
MC/DC0%0/2
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
time
Truth table66%2/3
A || B
A= $CHI::Driver::Test_TimeB= time
| |||||||||||||||||||||||||||||||||||
| 434 | 4244 | my $created_at = $time; | |||||||||||||||||||||||||||||||||
| 435 | my $expires_at = | ||||||||||||||||||||||||||||||||||
| 436 | ( defined( $options->{expires_in} ) ) | ||||||||||||||||||||||||||||||||||
| 437 | ? $time + parse_duration( $options->{expires_in} ) | ||||||||||||||||||||||||||||||||||
| 438 | 4244 | ▶ | : $options->{expires_at}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 439 | my $early_expires_at = | ||||||||||||||||||||||||||||||||||
| 440 | defined( $options->{early_expires_at} ) ? $options->{early_expires_at} | ||||||||||||||||||||||||||||||||||
| 441 | : ( $expires_at == CHI_Max_Time ) ? CHI_Max_Time | ||||||||||||||||||||||||||||||||||
| 442 | : $expires_at - | ||||||||||||||||||||||||||||||||||
| 443 | 4244 | ▶ | ( ( $expires_at - $time ) * $options->{expires_variance} ); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||
| 445 | # Pack into data, and store | ||||||||||||||||||||||||||||||||||
| 446 | # | ||||||||||||||||||||||||||||||||||
| 447 | 4244 | my $obj = | |||||||||||||||||||||||||||||||||
| 448 | $self->cache_object_class->new( $key, $value, $created_at, | ||||||||||||||||||||||||||||||||||
| 449 | $early_expires_at, $expires_at, $self->serializer, | ||||||||||||||||||||||||||||||||||
| 450 | $self->compress_threshold ); | ||||||||||||||||||||||||||||||||||
| 451 | 4244 | ▶ | if ( defined( my $obj_ref = $options->{obj_ref} ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 452 | 16 | $$obj_ref = $obj; | |||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||
| 454 | 4244 | ▶ | $start_time = gettimeofday() if $measure_time; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 455 | 4244 | ▶ | if ( $self->set_object( $key, $obj ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 456 | 4238 | ▶ | $elapsed_time = ( gettimeofday() - $start_time ) * 1000 | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 457 | if $measure_time; | ||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||
| 459 | # Log the set | ||||||||||||||||||||||||||||||||||
| 460 | # | ||||||||||||||||||||||||||||||||||
| 461 | 4238 | ▶ | if ( defined($ns_stats) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 462 | 2 | $ns_stats->{'sets'}++; | |||||||||||||||||||||||||||||||||
| 463 | 2 | $ns_stats->{'set_key_size'} += length( $obj->key ); | |||||||||||||||||||||||||||||||||
| 464 | 2 | $ns_stats->{'set_value_size'} += $obj->size; | |||||||||||||||||||||||||||||||||
| 465 | 2 | $ns_stats->{'set_time_ms'} += $elapsed_time; | |||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||
| 467 | 4238 | ▶ | if ($log_is_debug) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 468 | 4238 | $self->_log_set_result( $log, $obj, $elapsed_time ); | |||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||
| 472 | 4241 | return $value; | |||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||
| 475 | sub set_object { | ||||||||||||||||||||||||||||||||||
| 476 | 4454 | ▶ | my ( $self, $key, $obj ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
set_object: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||
| 478 | 4454 | my $data = $obj->pack_to_data(); | |||||||||||||||||||||||||||||||||
| 479 | 4454 | my $expires_on_backend = $self->expires_on_backend; | |||||||||||||||||||||||||||||||||
| 480 | 4454 | ▶ | my @expires_in = ( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$expires_on_backend and $obj->expires_at < 4294967295
MC/DC50%1/2
$expires_on_backend and $obj->expires_at < 4294967295
$expires_on_backend
$obj->expires_at < 4294967295
Truth table66%2/3
A and B
A= $expires_on_backendB= $obj->expires_at < 4294967295
| |||||||||||||||||||||||||||||||||||
| 481 | $expires_on_backend && $obj->expires_at < CHI_Max_Time | ||||||||||||||||||||||||||||||||||
| 482 | ? ( ( $obj->expires_at - time ) * $expires_on_backend ) | ||||||||||||||||||||||||||||||||||
| 483 | : () | ||||||||||||||||||||||||||||||||||
| 484 | ); | ||||||||||||||||||||||||||||||||||
| 485 | 4454 | eval { $self->store( $key, $data, @expires_in ) }; | |||||||||||||||||||||||||||||||||
| 486 | 4454 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 487 | 6 | ▶ | $self->{ns_stats}->{'set_errors'}++ if defined( $self->{ns_stats} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 488 | 6 | $self->_handle_set_error( $error, $obj ); | |||||||||||||||||||||||||||||||||
| 489 | 3 | return 0; | |||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||
| 491 | 4448 | return 1; | |||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||
| 494 | sub get_keys_iterator { | ||||||||||||||||||||||||||||||||||
| 495 | 62 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_keys_iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | 62 | my @keys = $self->get_keys(); | |||||||||||||||||||||||||||||||||
| 498 | 62 | ▶ | return sub { shift(@keys) }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||
| 501 | sub clear { | ||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | die "clear takes no arguments" if @_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||
| 505 | 0 | $self->remove_multi( [ $self->get_keys() ] ); | |||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||
| 508 | sub expire { | ||||||||||||||||||||||||||||||||||
| 509 | 209 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
expire: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 510 | 209 | ▶ | croak "must specify key" unless defined($key); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||
| 512 | 201 | ▶ | my $time = $Test_Time || time(); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$CHI::Driver::Test_Time || time
MC/DC0%0/2
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
time
Truth table33%1/3
A || B
A= $CHI::Driver::Test_TimeB= time
| |||||||||||||||||||||||||||||||||||
| 513 | 201 | ▶ | if ( defined( my $obj = $self->get_object($key) ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 514 | 201 | my $expires_at = $time - 1; | |||||||||||||||||||||||||||||||||
| 515 | 201 | $obj->set_early_expires_at($expires_at); | |||||||||||||||||||||||||||||||||
| 516 | 201 | $obj->set_expires_at($expires_at); | |||||||||||||||||||||||||||||||||
| 517 | 201 | $self->set_object( $key, $obj ); | |||||||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||
| 521 | sub compute { | ||||||||||||||||||||||||||||||||||
| 522 | 56 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
compute: called CC=12 SCAR=25.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 523 | 56 | my $key = shift; | |||||||||||||||||||||||||||||||||
| 524 | 56 | my $wantarray = wantarray(); | |||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||
| 526 | # Allow these in either order for backward compatibility | ||||||||||||||||||||||||||||||||||
| 527 | 56 | ▶ | my ( $code, $options ) = | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 528 | ( ref( $_[0] ) eq 'CODE' ) ? ( $_[0], $_[1] ) : ( $_[1], $_[0] ); | ||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||
| 530 | 56 | ▶ | croak "must specify key and code" unless defined($key) && defined($code); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and defined $code
MC/DC50%1/2
defined $key and defined $code
defined $key
defined $code
Truth table66%2/3
A and B
A= defined $keyB= defined $code
| |||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||
| 532 | my %get_options = | ||||||||||||||||||||||||||||||||||
| 533 | ( ref($options) eq 'HASH' ) | ||||||||||||||||||||||||||||||||||
| 534 | 48 | ▶ | ? slice_grep { /(?:expire_if|busy_lock)/ } $options | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 535 | : (); | ||||||||||||||||||||||||||||||||||
| 536 | my $set_options = | ||||||||||||||||||||||||||||||||||
| 537 | ( ref($options) eq 'HASH' ) | ||||||||||||||||||||||||||||||||||
| 538 | 48 | ▶ | ? { slice_grep { !/(?:expire_if|busy_lock)/ } $options } | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 539 | : $options; | ||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||
| 541 | 48 | my $value = $self->get( $key, %get_options ); | |||||||||||||||||||||||||||||||||
| 542 | 48 | ▶ | if ( !defined $value ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 543 | 40 | my ( $start_time, $elapsed_time ); | |||||||||||||||||||||||||||||||||
| 544 | 40 | my $ns_stats = $self->{ns_stats}; | |||||||||||||||||||||||||||||||||
| 545 | 40 | ▶ | $start_time = gettimeofday if defined($ns_stats); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 546 | 40 | ▶ | $value = $wantarray ? [ $code->() ] : $code->(); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 547 | 40 | ▶ | $elapsed_time = ( gettimeofday() - $start_time ) * 1000 | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 548 | if defined($ns_stats); | ||||||||||||||||||||||||||||||||||
| 549 | 40 | $self->set( $key, $value, $set_options ); | |||||||||||||||||||||||||||||||||
| 550 | 40 | ▶ | if ( defined($ns_stats) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 551 | 0 | $ns_stats->{'computes'}++; | |||||||||||||||||||||||||||||||||
| 552 | 0 | $ns_stats->{'compute_time_ms'} += $elapsed_time; | |||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||
| 555 | 48 | ▶ | return $wantarray ? @$value : $value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||
| 558 | sub purge { | ||||||||||||||||||||||||||||||||||
| 559 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
purge: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||
| 561 | 0 | foreach my $key ( $self->get_keys() ) { | |||||||||||||||||||||||||||||||||
| 562 | 0 | ▶ | if ( my $obj = $self->get_object($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 563 | 0 | ▶ | if ( $obj->is_expired() ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 564 | 0 | $self->remove($key); | |||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||
| 570 | sub dump_as_hash { | ||||||||||||||||||||||||||||||||||
| 571 | 32 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
dump_as_hash: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||
| 573 | 32 | my %hash; | |||||||||||||||||||||||||||||||||
| 574 | 32 | foreach my $key ( $self->get_keys() ) { | |||||||||||||||||||||||||||||||||
| 575 | 416 | ▶ | if ( defined( my $value = $self->get($key) ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 576 | 416 | $hash{$key} = $value; | |||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||
| 579 | 32 | return \%hash; | |||||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||
| 582 | sub is_empty { | ||||||||||||||||||||||||||||||||||
| 583 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||
| 585 | 0 | return !$self->get_keys(); | |||||||||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||
| 588 | # | ||||||||||||||||||||||||||||||||||
| 589 | # (SEMI-) ATOMIC OPERATIONS | ||||||||||||||||||||||||||||||||||
| 590 | # | ||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||
| 592 | sub add { | ||||||||||||||||||||||||||||||||||
| 593 | 24 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 594 | 24 | my $key = shift; | |||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||
| 596 | 24 | ▶ | if ( !$self->is_valid($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 597 | 16 | $self->set( $key, @_ ); | |||||||||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||
| 601 | sub append { | ||||||||||||||||||||||||||||||||||
| 602 | 18 | ▶ | my ( $self, $key, $new ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
append: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||
| 604 | 18 | ▶ | my $current = $self->fetch($key) or return undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 605 | 12 | $self->store( $key, $current . $new ); | |||||||||||||||||||||||||||||||||
| 606 | 12 | return 1; | |||||||||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||
| 609 | sub replace { | ||||||||||||||||||||||||||||||||||
| 610 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 611 | 16 | my $key = shift; | |||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||
| 613 | 16 | ▶ | if ( $self->is_valid($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 614 | 8 | $self->set( $key, @_ ); | |||||||||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||
| 618 | # | ||||||||||||||||||||||||||||||||||
| 619 | # MULTI KEY OPERATIONS | ||||||||||||||||||||||||||||||||||
| 620 | # | ||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||
| 622 | sub fetch_multi_hashref { | ||||||||||||||||||||||||||||||||||
| 623 | 112 | ▶ | my ( $self, $keys ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
fetch_multi_hashref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||
| 625 | 112 | return { map { ( $_, $self->fetch($_) ) } @$keys }; | |||||||||||||||||||||||||||||||||
| 626 | } | ||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||
| 628 | sub get_multi_hashref_objects { | ||||||||||||||||||||||||||||||||||
| 629 | 112 | ▶ | my ( $self, $keys ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_multi_hashref_objects: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 630 | 112 | my $key_data = $self->fetch_multi_hashref($keys); | |||||||||||||||||||||||||||||||||
| 631 | return { | ||||||||||||||||||||||||||||||||||
| 632 | map { | ||||||||||||||||||||||||||||||||||
| 633 | 112 | my $data = $key_data->{$_}; | |||||||||||||||||||||||||||||||||
| 634 | 544 | ▶ | defined($data) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 635 | ? ( $_, $self->unpack_from_data( $_, $data ) ) | ||||||||||||||||||||||||||||||||||
| 636 | : ( $_, undef ) | ||||||||||||||||||||||||||||||||||
| 637 | } keys(%$key_data) | ||||||||||||||||||||||||||||||||||
| 638 | }; | ||||||||||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||
| 641 | sub get_multi_arrayref { | ||||||||||||||||||||||||||||||||||
| 642 | 120 | ▶ | my ( $self, $keys ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_multi_arrayref: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 643 | 120 | ▶ | croak "must specify keys" unless defined($keys); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 644 | 112 | my $transformed_keys = [ map { $self->transform_key($_) } @$keys ]; | |||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||
| 646 | 112 | my $key_count = scalar(@$keys); | |||||||||||||||||||||||||||||||||
| 647 | 112 | my $keyobjs = $self->get_multi_hashref_objects($transformed_keys); | |||||||||||||||||||||||||||||||||
| 648 | return [ | ||||||||||||||||||||||||||||||||||
| 649 | map { | ||||||||||||||||||||||||||||||||||
| 650 | 112 | my $key = $transformed_keys->[$_]; | |||||||||||||||||||||||||||||||||
| 651 | 544 | my $obj = $keyobjs->{$key}; | |||||||||||||||||||||||||||||||||
| 652 | 544 | ▶ | $obj ? $self->get( $key, obj => $obj ) : undef | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 653 | } ( 0 .. $key_count - 1 ) | ||||||||||||||||||||||||||||||||||
| 654 | ]; | ||||||||||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||
| 657 | sub get_multi_hashref { | ||||||||||||||||||||||||||||||||||
| 658 | 36 | ▶ | my ( $self, $keys ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
get_multi_hashref: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 659 | 36 | ▶ | croak "must specify keys" unless defined($keys); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||
| 661 | 28 | my $key_count = scalar(@$keys); | |||||||||||||||||||||||||||||||||
| 662 | 28 | my $values = $self->get_multi_arrayref($keys); | |||||||||||||||||||||||||||||||||
| 663 | 28 | return { map { ( $keys->[$_], $values->[$_] ) } ( 0 .. $key_count - 1 ) }; | |||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||
| 666 | sub set_multi { | ||||||||||||||||||||||||||||||||||
| 667 | 96 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
set_multi: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 668 | 96 | $self->store_multi(@_); | |||||||||||||||||||||||||||||||||
| 669 | } | ||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||
| 671 | sub store_multi { | ||||||||||||||||||||||||||||||||||
| 672 | 96 | ▶ | my ( $self, $key_values, $set_options ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
store_multi: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 673 | 96 | ▶ | croak "must specify key_values" unless defined($key_values); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||
| 675 | 88 | ▶ | while ( my ( $key, $value ) = each(%$key_values) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 676 | 232 | $self->set( $key, $value, $set_options ); | |||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||
| 680 | sub remove_multi { | ||||||||||||||||||||||||||||||||||
| 681 | 24 | ▶ | my ( $self, $keys ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
remove_multi: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 682 | 24 | ▶ | croak "must specify keys" unless defined($keys); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||
| 684 | 16 | foreach my $key (@$keys) { | |||||||||||||||||||||||||||||||||
| 685 | 104 | $self->remove($key); | |||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||
| 689 | # | ||||||||||||||||||||||||||||||||||
| 690 | # KEY TRANSFORMATION | ||||||||||||||||||||||||||||||||||
| 691 | # | ||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||
| 693 | 20 | my %escapes; | |||||||||||||||||||||||||||||||||
| 694 | 20 | for ( 0 .. 255 ) { | |||||||||||||||||||||||||||||||||
| 695 | 5120 | $escapes{ chr($_) } = sprintf( "+%02x", $_ ); | |||||||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||||||||
| 697 | my $_fail_hi = sub { | ||||||||||||||||||||||||||||||||||
| 698 | 0 | ▶ | croak( sprintf "Can't escape multibyte character \\x{%04X}", ord( $_[0] ) ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 699 | 20 | }; | |||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||
| 701 | sub transform_key { | ||||||||||||||||||||||||||||||||||
| 702 | 22107 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
transform_key: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||
| 704 | 22107 | ▶ | if ( ref($key) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 705 | 768 | $key = $self->key_serializer->serialize($key); | |||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||
| 707 | ▶ | elsif ( Encode::is_utf8($key) && $key =~ /[^\x00-\xFF]/ ) { | |||||||||||||||||||||||||||||||||
|
Condition66%2/3
&Encode::is_utf8($key) and $key =~ /[^\x00-\xFF]/
MC/DC50%1/2
&Encode::is_utf8($key) and $key =~ /[^\x00-\xFF]/
&Encode::is_utf8($key)
$key =~ /[^\x00-\xFF]/
Truth table66%2/3
A and B
A= &Encode::is_utf8($key)B= $key =~ /[^\x00-\xFF]/
| |||||||||||||||||||||||||||||||||||
| 708 | 267 | $key = $self->encode_key($key); | |||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||||
| 710 | 22107 | ▶ | if ( length($key) > $self->max_key_length ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 711 | 78 | $key = $self->digest_key($key); | |||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||
| 714 | 22107 | return $key; | |||||||||||||||||||||||||||||||||
| 715 | } | ||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||
| 717 | sub digest_key { | ||||||||||||||||||||||||||||||||||
| 718 | 174 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
digest_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||
| 720 | 174 | return $self->key_digester->add($key)->hexdigest; | |||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||
| 723 | sub encode_key { | ||||||||||||||||||||||||||||||||||
| 724 | 267 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
encode_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||
| 726 | 267 | return Encode::encode( utf8 => $key ); | |||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||
| 729 | # These will be called by drivers if necessary, and in testing. By default | ||||||||||||||||||||||||||||||||||
| 730 | # no escaping/unescaping is necessary. | ||||||||||||||||||||||||||||||||||
| 731 | # | ||||||||||||||||||||||||||||||||||
| 732 | 404 | ▶ | sub escape_key { $_[1] } | ||||||||||||||||||||||||||||||||
|
Subroutine
escape_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 733 | 404 | ▶ | sub unescape_key { $_[1] } | ||||||||||||||||||||||||||||||||
|
Subroutine
unescape_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||
| 735 | # May be used by drivers to implement escape_key/unescape_key. | ||||||||||||||||||||||||||||||||||
| 736 | # | ||||||||||||||||||||||||||||||||||
| 737 | sub escape_for_filename { | ||||||||||||||||||||||||||||||||||
| 738 | 10246 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
escape_for_filename: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||
| 740 | 10246 | ▶ | $key =~ s/([^A-Za-z0-9_\=\-\~])/$escapes{$1} || $_fail_hi->($1)/ge; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 741 | 10246 | return $key; | |||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||||||||
| 744 | sub unescape_for_filename { | ||||||||||||||||||||||||||||||||||
| 745 | 3233 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
unescape_for_filename: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||
| 747 | 3233 | ▶ | $key =~ s/\+([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $key; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 748 | 3233 | return $key; | |||||||||||||||||||||||||||||||||
| 749 | } | ||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||
| 751 | sub is_escaped_for_filename { | ||||||||||||||||||||||||||||||||||
| 752 | 186 | ▶ | my ( $self, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_escaped_for_filename: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||
| 754 | 186 | return $self->escape_for_filename( $self->unescape_for_filename($key) ) eq | |||||||||||||||||||||||||||||||||
| 755 | $key; | ||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||
| 758 | # | ||||||||||||||||||||||||||||||||||
| 759 | # LOGGING AND ERROR HANDLING | ||||||||||||||||||||||||||||||||||
| 760 | # | ||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||
| 762 | sub _log_get_result { | ||||||||||||||||||||||||||||||||||
| 763 | 11920 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_log_get_result: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 764 | 11920 | my $log = shift; | |||||||||||||||||||||||||||||||||
| 765 | 11920 | my $msg = shift; | |||||||||||||||||||||||||||||||||
| 766 | 11920 | $log->debug( sprintf( "%s: %s", $self->_describe_cache_get(@_), $msg ) ); | |||||||||||||||||||||||||||||||||
| 767 | } | ||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||
| 769 | sub _log_set_result { | ||||||||||||||||||||||||||||||||||
| 770 | 4238 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_log_set_result: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 771 | 4238 | my $log = shift; | |||||||||||||||||||||||||||||||||
| 772 | 4238 | $log->debug( $self->_describe_cache_set(@_) ); | |||||||||||||||||||||||||||||||||
| 773 | } | ||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||
| 775 | sub _handle_get_error { | ||||||||||||||||||||||||||||||||||
| 776 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_handle_get_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 777 | 4 | my $error = shift; | |||||||||||||||||||||||||||||||||
| 778 | 4 | my $key = $_[0]; | |||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||
| 780 | 4 | my $msg = | |||||||||||||||||||||||||||||||||
| 781 | sprintf( "error during %s: %s", $self->_describe_cache_get(@_), $error ); | ||||||||||||||||||||||||||||||||||
| 782 | 4 | $self->_dispatch_error_msg( $msg, $error, $self->on_get_error(), $key ); | |||||||||||||||||||||||||||||||||
| 783 | } | ||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||
| 785 | sub _handle_set_error { | ||||||||||||||||||||||||||||||||||
| 786 | 6 | ▶ | my ( $self, $error, $obj ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_handle_set_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||
| 788 | 6 | my $msg = | |||||||||||||||||||||||||||||||||
| 789 | sprintf( "error during %s: %s", $self->_describe_cache_set($obj), | ||||||||||||||||||||||||||||||||||
| 790 | $error ); | ||||||||||||||||||||||||||||||||||
| 791 | 6 | $self->_dispatch_error_msg( $msg, $error, $self->on_set_error(), | |||||||||||||||||||||||||||||||||
| 792 | $obj->key ); | ||||||||||||||||||||||||||||||||||
| 793 | } | ||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||
| 795 | sub _dispatch_error_msg { | ||||||||||||||||||||||||||||||||||
| 796 | 10 | ▶ | my ( $self, $msg, $error, $on_error, $key ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_dispatch_error_msg: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||
| 798 | 10 | for ($on_error) { | |||||||||||||||||||||||||||||||||
| 799 | 10 | ▶ | ( ref($_) eq 'CODE' ) && do { $_->( $msg, $key, $error ) }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 800 | /^log$/ | ||||||||||||||||||||||||||||||||||
| 801 | 10 | ▶ | && do { $log->error($msg) }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 802 | 10 | ▶ | /^ignore$/ && do { }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 803 | 10 | ▶ | /^warn$/ && do { carp $msg }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 804 | 10 | ▶ | /^die$/ && do { croak $msg }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||
| 808 | sub _describe_cache_get { | ||||||||||||||||||||||||||||||||||
| 809 | 11924 | ▶ | my ( $self, $key, $elapsed_time ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_describe_cache_get: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||
| 811 | return | ||||||||||||||||||||||||||||||||||
| 812 | 11924 | ▶ | sprintf( "cache get for namespace='%s', key='%s', cache='%s'" | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 813 | . ( defined($elapsed_time) ? ", time='%dms'" : "" ), | ||||||||||||||||||||||||||||||||||
| 814 | $self->namespace, $key, $self->label, | ||||||||||||||||||||||||||||||||||
| 815 | defined($elapsed_time) ? int($elapsed_time) : () ); | ||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||
| 818 | sub _describe_cache_set { | ||||||||||||||||||||||||||||||||||
| 819 | 4244 | ▶ | my ( $self, $obj, $elapsed_time ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
_describe_cache_set: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||
| 821 | 4244 | ▶ | my $expires_str = ( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 822 | ( $obj->expires_at == CHI_Max_Time ) | ||||||||||||||||||||||||||||||||||
| 823 | ? 'never' | ||||||||||||||||||||||||||||||||||
| 824 | : Time::Duration::concise( | ||||||||||||||||||||||||||||||||||
| 825 | Time::Duration::duration_exact( | ||||||||||||||||||||||||||||||||||
| 826 | $obj->expires_at - $obj->created_at | ||||||||||||||||||||||||||||||||||
| 827 | ) | ||||||||||||||||||||||||||||||||||
| 828 | ) | ||||||||||||||||||||||||||||||||||
| 829 | ); | ||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||
| 831 | return | ||||||||||||||||||||||||||||||||||
| 832 | 4244 | ▶ | sprintf( | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 833 | "cache set for namespace='%s', key='%s', size=%d, expires='%s', cache='%s'" | ||||||||||||||||||||||||||||||||||
| 834 | . ( defined($elapsed_time) ? ", time='%dms'" : "" ), | ||||||||||||||||||||||||||||||||||
| 835 | $self->namespace, $obj->key, $obj->size, $expires_str, $self->label, | ||||||||||||||||||||||||||||||||||
| 836 | defined($elapsed_time) ? int($elapsed_time) : () ); | ||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||
| 838 | } | ||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||
| 840 | 20 | 1; | |||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||
| 842 | __END__ | ||||||||||||||||||||||||||||||||||