CHI/Driver.pm

filter: Statementstmt 95.3% 386 / 405 Branchbran 81.9% 141 / 172 Conditioncond 69.7% 23 / 33 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 92.7% 89 / 96 Podpod 0.0% 0 / 41 totaltotal 84.2% 647 / 768 SCAR 47.4
CC
104
Cov
90%
get
21.9
purge
20.0
compute
12.9
CRAP 114.3
« CHI/t/Driver.pm ↑ Summary CHI/Driver/File.pm »
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
BranchTrueFalseTotal
if $CHI::Driver::Build_Depth > $self->max_build_depth 0 998 998
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
BranchTrueFalseTotal
if ($stats->enabled) 6 992 998
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
BranchTrueFalseTotal
unless defined $key 8 11924 11932
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
l!l&&r!l&&!r
3119210
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
ABresult
1X1
011
000
261 11924 my ( $start_time, $elapsed_time );
262  
263 # Fetch cache object
264 #
265 11924 ▶ $start_time = gettimeofday() if $measure_time;
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 11924 0 11924
266 11924 ▶ my $obj = eval { $params{obj} || $self->get_object($key) };
Branch: 1 branch
BranchTrueFalseTotal
$params{"obj"} or $self->get_object($key) 11578 346 11924
267 11924 ▶ $elapsed_time = ( gettimeofday() - $start_time ) * 1000 if $measure_time;
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 11924 0 11924
268 11924 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 4 11920 11924
269 4 ▶ $ns_stats->{'get_errors'}++ if defined($ns_stats);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 0 4 4
270 4 $self->_handle_get_error( $error, $key );
271 3 return undef;
272 }
273 11920 ▶ if ( !defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 1685 10235 11920
274 1685 ▶ $self->_record_get_stats( 'absent_misses', $elapsed_time )
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 2 1683 1685
275 if defined($ns_stats);
276 1685 ▶ $self->_log_get_result( $log, "MISS (not in cache)",
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 1685 0 1685
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
BranchTrueFalseTotal
if (defined(my $obj_ref = $params{"obj_ref"})) 629 9606 10235
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)
l!l&&r!l&&!r
3968366231
defined $params{"expire_if"} and $params{"expire_if"}->($obj, $self)
!ll&&!rl&&r
62151636
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)
ABCresult
1XX1
0111
00X0
0100
290 10235 ▶ if ($is_expired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_expired) 4004 6231 10235
291 4004 ▶ $self->_record_get_stats( 'expired_misses', $elapsed_time )
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 0 4004 4004
292 if defined($ns_stats);
293 4004 ▶ $self->_log_get_result( $log, "MISS (expired)", $key, $elapsed_time )
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 4004 0 4004
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
BranchTrueFalseTotal
if (defined(my $busy_lock = $params{"busy_lock"})) 9 3995 4004
300 9 ▶ my $time = $Test_Time || time();
Condition33%1/3
$CHI::Driver::Test_Time || time
l!l&&r!l&&!r
900
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if defined $ns_stats 1 6230 6231
311 6231 ▶ $self->_log_get_result( $log, "HIT", $key, $elapsed_time ) if $log_is_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 6231 0 6231
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
BranchTrueFalseTotal
unless defined $key 8 14516 14524
332 14516 $key = $self->transform_key($key);
333  
334 14516 ▶ my $data = $self->fetch($key) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->fetch($key) or return undef 3040 11472 14512
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
BranchTrueFalseTotal
unless defined $key 8 80 88
342  
343 80 ▶ if ( my $obj = $self->get_object($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_object($key)) { } 72 8 80
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
BranchTrueFalseTotal
unless defined $key 8 16 24
354  
355 16 ▶ if ( my $obj = $self->get_object($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_object($key)) { } 16 0 16
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
BranchTrueFalseTotal
unless defined $key 8 72 80
366  
367 72 ▶ if ( my $obj = $self->get_object($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $self->get_object($key)) { } 40 32 72
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
BranchTrueFalseTotal
unless defined $key 8 4244 4252
387 4244 $key = $self->transform_key($key);
388 4244 ▶ return unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4244 4244
389  
390 # Fill in $options if not passed, copy if passed, and apply defaults.
391 #
392 4244 ▶ if ( !defined($options) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $options) { } 3846 398 4244
393 3846 $options = $self->_default_set_options;
394 }
395 else {
396 398 ▶ if ( !ref($options) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $options) 78 320 398
397 78 ▶ if ( $options eq 'never' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options eq 'never') { } 9 69 78
elsif ($options eq 'now') { } 0 69 69
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
BranchTrueFalseTotal
if (exists $options->{'expires_at'} or exists $options->{'expires_in'}) { } 355 43 398
Condition100%3/3
exists $options->{"expires_at"} or exists $options->{"expires_in"}
l!l&&r!l&&!r
2866943
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"}
ABresult
1X1
011
000
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
l!l&&r!l&&!r
242420
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
ABresult
1X1
011
000
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
l!l&&r!l&&!r
11841260
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
defined $options->{'expires_in'} ? : 87 4157 4244
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
BranchTrueFalseTotal
defined $options->{'early_expires_at'} ? : 194 4050 4244
$expires_at == 4294967295 ? : 3862 188 4050
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
BranchTrueFalseTotal
if (defined(my $obj_ref = $options->{"obj_ref"})) 16 4228 4244
452 16 $$obj_ref = $obj;
453 }
454 4244 ▶ $start_time = gettimeofday() if $measure_time;
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 4244 0 4244
455 4244 ▶ if ( $self->set_object( $key, $obj ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->set_object($key, $obj)) 4238 3 4241
456 4238 ▶ $elapsed_time = ( gettimeofday() - $start_time ) * 1000
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 4238 0 4238
457 if $measure_time;
458  
459 # Log the set
460 #
461 4238 ▶ if ( defined($ns_stats) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns_stats) 2 4236 4238
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
BranchTrueFalseTotal
if ($log_is_debug) 4238 0 4238
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
BranchTrueFalseTotal
$expires_on_backend && $obj->expires_at < 4294967295 ? : 2 4452 4454
Condition66%2/3
$expires_on_backend and $obj->expires_at < 4294967295
!ll&&!rl&&r
445202
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (my $error = $@) 6 4448 4454
487 6 ▶ $self->{ns_stats}->{'set_errors'}++ if defined( $self->{ns_stats} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ns_stats"} 0 6 6
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
BranchTrueFalseTotal
if @_ 0 0 0
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
BranchTrueFalseTotal
unless defined $key 8 201 209
511  
512 201 ▶ my $time = $Test_Time || time();
Condition33%1/3
$CHI::Driver::Test_Time || time
l!l&&r!l&&!r
02010
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
ABresult
1X1
011
000
513 201 ▶ if ( defined( my $obj = $self->get_object($key) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $self->get_object($key))) 201 0 201
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
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 16 40 56
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
BranchTrueFalseTotal
unless defined $key and defined $code 8 48 56
Condition66%2/3
defined $key and defined $code
!ll&&!rl&&r
8048
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 48 0 48
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
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 48 0 48
539 : $options;
540  
541 48 my $value = $self->get( $key, %get_options );
542 48 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 40 8 48
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
BranchTrueFalseTotal
if defined $ns_stats 0 40 40
546 40 ▶ $value = $wantarray ? [ $code->() ] : $code->();
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 8 32 40
547 40 ▶ $elapsed_time = ( gettimeofday() - $start_time ) * 1000
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 0 40 40
548 if defined($ns_stats);
549 40 $self->set( $key, $value, $set_options );
550 40 ▶ if ( defined($ns_stats) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns_stats) 0 40 40
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
BranchTrueFalseTotal
$wantarray ? : 16 32 48
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
BranchTrueFalseTotal
if (my $obj = $self->get_object($key)) 0 0 0
563 0 ▶ if ( $obj->is_expired() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->is_expired) 0 0 0
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
BranchTrueFalseTotal
if (defined(my $value = $self->get($key))) 416 0 416
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
BranchTrueFalseTotal
if (not $self->is_valid($key)) 16 8 24
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
BranchTrueFalseTotal
my $current = $self->fetch($key) or return undef 6 12 18
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
BranchTrueFalseTotal
if ($self->is_valid($key)) 8 8 16
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
BranchTrueFalseTotal
defined $data ? : 346 198 544
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
BranchTrueFalseTotal
unless defined $keys 8 112 120
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
BranchTrueFalseTotal
$obj ? : 346 198 544
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
BranchTrueFalseTotal
unless defined $keys 8 28 36
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
BranchTrueFalseTotal
unless defined $key_values 8 88 96
674  
675 88 ▶ while ( my ( $key, $value ) = each(%$key_values) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$key_values) 232 88 320
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
BranchTrueFalseTotal
unless defined $keys 8 16 24
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
BranchTrueFalseTotal
if (ref $key) { } 768 21339 22107
elsif (&Encode::is_utf8($key) and $key =~ /[^\x00-\xFF]/) { } 267 42144 42411
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]/
!ll&&!rl&&r
210720267
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]/
ABresult
0X0
100
111
708 267 $key = $self->encode_key($key);
709 }
710 22107 ▶ if ( length($key) > $self->max_key_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > $self->max_key_length) 78 22029 22107
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
BranchTrueFalseTotal
$escapes{$1} or &$_fail_hi($1) 0 20110 20110
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
BranchTrueFalseTotal
if defined $key 3233 0 3233
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
BranchTrueFalseTotal
ref $_ eq "CODE" and do { &$_($msg, $key, $error) } 2 8 10
800 /^log$/
801 10 ▶ && do { $log->error($msg) };
Branch: 1 branch
BranchTrueFalseTotal
/^log$/ and do { $CHI::Driver::log->error($msg) } 2 8 10
802 10 ▶ /^ignore$/ && do { };
Branch: 1 branch
BranchTrueFalseTotal
/^ignore$/ and do { () } 2 8 10
803 10 ▶ /^warn$/ && do { carp $msg };
Branch: 1 branch
BranchTrueFalseTotal
/^warn$/ and do { carp($msg) } 0 10 10
804 10 ▶ /^die$/ && do { croak $msg };
Branch: 1 branch
BranchTrueFalseTotal
/^die$/ and do { croak($msg) } 4 6 10
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
BranchTrueFalseTotal
defined $elapsed_time ? : 11920 4 11924
defined $elapsed_time ? : 11920 4 11924
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
BranchTrueFalseTotal
$obj->expires_at == 4294967295 ? : 3992 252 4244
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
BranchTrueFalseTotal
defined $elapsed_time ? : 4238 6 4244
defined $elapsed_time ? : 4238 6 4244
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__
« CHI/t/Driver.pm ↑ Summary CHI/Driver/File.pm »