CHI/Driver.pm

filter: Statementstmt 95.3% 386 / 405 Branchbran 82.1% 138 / 168 Conditioncond 75.0% 27 / 36 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 92.7% 89 / 96 Podpod 0.0% 0 / 41 totaltotal 84.7% 651 / 768 CC 104 SCAR 18.5
CC
104
Cov
90%
get
21.7
purge
20.0
compute
12.9
CRAP 6.3
« CHI/Test/Util.pm ↑ Summary CHI/t/Driver/FastMmap.pm »
1 package CHI::Driver;
2 19 $CHI::Driver::VERSION = '0.61';
3 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use CHI::CacheObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use CHI::Constants qw(CHI_Max_Time);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use CHI::Driver::Metacache;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use CHI::Driver::Role::HasSubcaches;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use CHI::Driver::Role::IsSizeAware;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use CHI::Driver::Role::IsSubcache;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use CHI::Driver::Role::Universal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use CHI::Serializer::Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use CHI::Serializer::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use CHI::Util qw(parse_duration);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use CHI::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use Hash::MoreUtils qw(slice_grep);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 19 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 19 ▶use Time::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 19 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 19 my $default_serializer = CHI::Serializer::Storable->new();
28 19 my $default_key_serializer = CHI::Serializer::JSON->new();
29 19 my $default_key_digester = Digest::MD5->new();
30  
31 19 my @common_params;
32 {
33 19 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 811 ▶ 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 982 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 },
62 expires_variance => {
63 is => 'rw',
64 isa => Num,
65 819 ▶ 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 984 ▶ 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 984 ▶ 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 984 ▶ 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 43 ▶ default => sub { 'log' },
Subroutine __ANON__: called CC=1 SCAR=0.0
122 },
123 on_set_error => {
124 is => 'rw',
125 isa => OnError,
126 42 ▶ 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 732 ▶ default => sub { $default_serializer },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 },
134 19 short_driver_name => {
135 is => 'lazy',
136 clearer => 1,
137 predicate => 1,
138 },
139 storage => {
140 is => 'ro',
141 },
142 );
143 19 push @common_params, keys %attr;
144 19 for my $attr ( keys %attr ) {
145 437 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 19 foreach my $method (qw(fetch store remove get_keys get_namespaces)) {
153 19 ▶ 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 19 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 19 my %common_params = map { ( $_, 1 ) } @common_params;
171  
172 1 ▶sub non_common_constructor_params {
Subroutine non_common_constructor_params: called CC=1 SCAR=0.0
Pod undocumented
173 1 my ( $class, $params ) = @_;
174  
175 return {
176 3 map { ( $_, $params->{$_} ) }
177 1 grep { !$common_params{$_} } keys(%$params)
178 };
179 }
180  
181 1 ▶sub declare_unsupported_methods {
Subroutine declare_unsupported_methods: called CC=2 SCAR=6.9
Pod undocumented
182 1 my ( $class, @methods ) = @_;
183  
184 1 foreach my $method (@methods) {
185 19 ▶ 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 16093 ▶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 19 our $Test_Time; ## no critic (ProhibitPackageVars)
195 19 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 982 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
201 982 my ( $self, $params ) = @_;
202  
203 # Ward off infinite build recursion, e.g. from circular subcache configuration.
204 #
205 982 local $Build_Depth = $Build_Depth + 1;
206 982 ▶ die "$Build_Depth levels of CHI cache creation; infinite recursion?"
Branch: 1 branch
BranchTrueFalseTotal
if $CHI::Driver::Build_Depth > $self->max_build_depth 0 982 982
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 982 $self->{constructor_params} = {%$params};
213 982 foreach my $param (qw(l1_cache mirror_cache parent_cache)) {
214 2946 delete( $self->{constructor_params}->{$param} );
215 }
216  
217 # If stats enabled, add ns_stats slot for keeping track of stats
218 #
219 982 my $stats = $self->chi_root_class->stats;
220 982 ▶ if ( $stats->enabled ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stats->enabled) 6 976 982
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 982 $self->BUILD_roles($params);
227 }
228  
229 982 ▶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 440 ▶sub _build_short_driver_name {
Subroutine _build_short_driver_name: called CC=1 SCAR=0.0
235 440 my ($self) = @_;
236  
237 440 ( my $name = $self->driver_class ) =~ s/^CHI::Driver:://;
238  
239 440 return $name;
240 }
241  
242 438 ▶sub _build_label {
Subroutine _build_label: called CC=1 SCAR=0.0
243 438 my ($self) = @_;
244  
245 438 return $self->short_driver_name;
246 }
247  
248 85 ▶sub _build_metacache {
Subroutine _build_metacache: called CC=1 SCAR=0.0
249 85 my $self = shift;
250  
251 85 return CHI::Driver::Metacache->new( owner_cache => $self );
252 }
253  
254 11939 ▶sub get {
Subroutine get: called CC=21 SCAR=30.8
Pod undocumented
255 11939 my ( $self, $key, %params ) = @_;
256  
257 11939 ▶ croak "must specify key" unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 8 11931 11939
258 11931 my $ns_stats = $self->{ns_stats};
259 11931 my $log_is_debug = $log->is_debug;
260 11931 ▶ my $measure_time = defined($ns_stats) || $log_is_debug;
Condition66%2/3
defined $ns_stats || $log_is_debug
l!l&&r!l&&!r
3119280
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 11931 my ( $start_time, $elapsed_time );
262  
263 # Fetch cache object
264 #
265 11931 ▶ $start_time = gettimeofday() if $measure_time;
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 11931 0 11931
266 11931 ▶ my $obj = eval { $params{obj} || $self->get_object($key) };
Condition100%3/3
$params{'obj'} || $self->get_object($key)
l!l&&r!l&&!r
34698931692
MC/DC100%2/2
$params{'obj'} || $self->get_object($key)
$params{'obj'} $self->get_object($key)
Truth table100%3/3
A || B
A= $params{'obj'}B= $self->get_object($key)
ABresult
1X1
011
000
267 11931 ▶ $elapsed_time = ( gettimeofday() - $start_time ) * 1000 if $measure_time;
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 11931 0 11931
268 11931 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 4 11927 11931
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 11927 ▶ if ( !defined $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 1688 10239 11927
274 1688 ▶ $self->_record_get_stats( 'absent_misses', $elapsed_time )
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 2 1686 1688
275 if defined($ns_stats);
276 1688 ▶ $self->_log_get_result( $log, "MISS (not in cache)",
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 1688 0 1688
277 $key, $elapsed_time )
278 if $log_is_debug;
279 1688 return undef;
280 }
281 10239 ▶ if ( defined( my $obj_ref = $params{obj_ref} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj_ref = $params{"obj_ref"})) 636 9603 10239
282 636 $$obj_ref = $obj;
283 }
284  
285 # Check if expired
286 #
287 my $is_expired = $obj->is_expired()
288 || ( defined( $params{expire_if} )
289 10239 ▶ && $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
3970366233
defined $params{"expire_if"} and $params{"expire_if"}->($obj, $self)
!ll&&!rl&&r
62171636
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 10239 ▶ if ($is_expired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_expired) 4006 6233 10239
291 4006 ▶ $self->_record_get_stats( 'expired_misses', $elapsed_time )
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 0 4006 4006
292 if defined($ns_stats);
293 4006 ▶ $self->_log_get_result( $log, "MISS (expired)", $key, $elapsed_time )
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 4006 0 4006
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 4006 ▶ if ( defined( my $busy_lock = $params{busy_lock} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $busy_lock = $params{"busy_lock"})) 9 3997 4006
300 9 ▶ my $time = $Test_Time || time();
Condition50%1/2
$CHI::Driver::Test_Time || time
l!l
90
MC/DC0%0/1
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
Truth table50%1/2
A
A= $CHI::Driver::Test_Time
Aresult
11
00
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 4006 return undef;
308 }
309  
310 6233 ▶ $self->_record_get_stats( 'hits', $elapsed_time ) if defined($ns_stats);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns_stats 1 6232 6233
311 6233 ▶ $self->_log_get_result( $log, "HIT", $key, $elapsed_time ) if $log_is_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 6233 0 6233
312 6233 return $obj->value;
313 }
314  
315 3 ▶sub _record_get_stats {
Subroutine _record_get_stats: called CC=1 SCAR=0.0
316 3 my ( $self, $stat, $elapsed_time ) = @_;
317 3 $self->{ns_stats}->{$stat}++;
318 3 $self->{ns_stats}->{'get_time_ms'} += $elapsed_time;
319 }
320  
321 11822 ▶sub unpack_from_data {
Subroutine unpack_from_data: called CC=1 SCAR=0.0
Pod undocumented
322 11822 my ( $self, $key, $data ) = @_;
323  
324 11822 return $self->cache_object_class->unpack_from_data( $key, $data,
325 $self->serializer );
326 }
327  
328 14524 ▶sub get_object {
Subroutine get_object: called CC=3 SCAR=11.0
Pod undocumented
329 14524 my ( $self, $key ) = @_;
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 11476 3036 14512
335 11476 my $obj = $self->unpack_from_data( $key, $data );
336 11476 return $obj;
337 }
338  
339 88 ▶sub get_expires_at {
Subroutine get_expires_at: called CC=3 SCAR=11.0
Pod undocumented
340 88 my ( $self, $key ) = @_;
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 24 ▶sub exists_and_is_expired {
Subroutine exists_and_is_expired: called CC=3 SCAR=11.3
Pod undocumented
352 24 my ( $self, $key ) = @_;
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 80 ▶sub is_valid {
Subroutine is_valid: called CC=3 SCAR=11.0
Pod undocumented
364 80 my ( $self, $key ) = @_;
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 3889 ▶sub _default_set_options {
Subroutine _default_set_options: called CC=1 SCAR=0.0
376 3889 my $self = shift;
377  
378 3889 return { map { $_ => $self->$_() }
379 qw( expires_at expires_in expires_variance ) };
380 }
381  
382 4279 ▶sub set {
Subroutine set: called CC=9 SCAR=22.0
Pod undocumented
383 4279 my $self = shift;
384 4279 my ( $key, $value, $options ) = @_;
385  
386 4279 ▶ croak "must specify key" unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 8 4271 4279
387 4271 $key = $self->transform_key($key);
388 4271 ▶ return unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4271 4271
389  
390 # Fill in $options if not passed, copy if passed, and apply defaults.
391 #
392 4271 ▶ if ( !defined($options) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $options) { } 3846 425 4271
393 3846 $options = $self->_default_set_options;
394 }
395 else {
396 425 ▶ if ( !ref($options) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $options) 78 347 425
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 425 ▶ if ( exists( $options->{expires_at} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{'expires_at'} or exists $options->{'expires_in'}) { } 382 43 425
Condition100%3/3
exists $options->{"expires_at"} or exists $options->{"expires_in"}
l!l&&r!l&&!r
3136943
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 382 $options =
414 { expires_variance => $self->expires_variance, %$options };
415 }
416 else {
417 43 $options = { %{ $self->_default_set_options }, %$options };
418 }
419 }
420  
421 4271 $self->set_with_options( $key, $value, $options );
422 }
423  
424 4271 ▶sub set_with_options {
Subroutine set_with_options: called CC=12 SCAR=24.9
Pod undocumented
425 4271 my ( $self, $key, $value, $options ) = @_;
426 4271 my $ns_stats = $self->{ns_stats};
427 4271 my $log_is_debug = $log->is_debug;
428 4271 ▶ my $measure_time = defined($ns_stats) || $log_is_debug;
Condition66%2/3
defined $ns_stats || $log_is_debug
l!l&&r!l&&!r
242690
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 4271 my ( $start_time, $elapsed_time );
430  
431 # Determine early and final expiration times
432 #
433 4271 ▶ my $time = $Test_Time || time();
Condition100%2/2
$CHI::Driver::Test_Time || time
l!l
1424129
MC/DC100%1/1
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
Truth table100%2/2
A
A= $CHI::Driver::Test_Time
Aresult
11
00
434 4271 my $created_at = $time;
435 my $expires_at =
436 ( defined( $options->{expires_in} ) )
437 ? $time + parse_duration( $options->{expires_in} )
438 4271 ▶ : $options->{expires_at};
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'expires_in'} ? : 87 4184 4271
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 4271 ▶ ( ( $expires_at - $time ) * $options->{expires_variance} );
Branch: 2 branches
BranchTrueFalseTotal
defined $options->{'early_expires_at'} ? : 221 4050 4271
$expires_at == 4294967295 ? : 3862 188 4050
444  
445 # Pack into data, and store
446 #
447 4271 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 4271 ▶ if ( defined( my $obj_ref = $options->{obj_ref} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj_ref = $options->{"obj_ref"})) 16 4255 4271
452 16 $$obj_ref = $obj;
453 }
454 4271 ▶ $start_time = gettimeofday() if $measure_time;
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 4271 0 4271
455 4271 ▶ if ( $self->set_object( $key, $obj ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->set_object($key, $obj)) 4265 3 4268
456 4265 ▶ $elapsed_time = ( gettimeofday() - $start_time ) * 1000
Branch: 1 branch
BranchTrueFalseTotal
if $measure_time 4265 0 4265
457 if $measure_time;
458  
459 # Log the set
460 #
461 4265 ▶ if ( defined($ns_stats) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns_stats) 2 4263 4265
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 4265 ▶ if ($log_is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_is_debug) 4265 0 4265
468 4265 $self->_log_set_result( $log, $obj, $elapsed_time );
469 }
470 }
471  
472 4268 return $value;
473 }
474  
475 4481 ▶sub set_object {
Subroutine set_object: called CC=5 SCAR=16.1
Pod undocumented
476 4481 my ( $self, $key, $obj ) = @_;
477  
478 4481 my $data = $obj->pack_to_data();
479 4481 my $expires_on_backend = $self->expires_on_backend;
480 4481 ▶ my @expires_in = (
Branch: 1 branch
BranchTrueFalseTotal
$expires_on_backend && $obj->expires_at < 4294967295 ? : 2 4479 4481
Condition66%2/3
$expires_on_backend and $obj->expires_at < 4294967295
!ll&&!rl&&r
447902
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 4481 eval { $self->store( $key, $data, @expires_in ) };
486 4481 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 6 4475 4481
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 4475 return 1;
492 }
493  
494 58 ▶sub get_keys_iterator {
Subroutine get_keys_iterator: called CC=1 SCAR=0.0
Pod undocumented
495 58 my ($self) = @_;
496  
497 58 my @keys = $self->get_keys();
498 58 ▶ return sub { shift(@keys) };
Subroutine __ANON__: called CC=1 SCAR=0.0
499 }
500  
501 0 ▶sub clear {
Subroutine clear: not called CC=2 SCAR=17.9
Pod undocumented
502 0 my $self = shift;
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 209 ▶sub expire {
Subroutine expire: called CC=4 SCAR=14.0
Pod undocumented
509 209 my ( $self, $key ) = @_;
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();
Condition50%1/2
$CHI::Driver::Test_Time || time
l!l
0201
MC/DC0%0/1
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
Truth table50%1/2
A
A= $CHI::Driver::Test_Time
Aresult
11
00
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 56 ▶sub compute {
Subroutine compute: called CC=12 SCAR=25.5
Pod undocumented
522 56 my $self = shift;
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 0 ▶sub purge {
Subroutine purge: not called CC=4 SCAR=30.0
Pod undocumented
559 0 my ($self) = @_;
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 32 ▶sub dump_as_hash {
Subroutine dump_as_hash: called CC=3 SCAR=11.0
Pod undocumented
571 32 my ($self) = @_;
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 0 ▶sub is_empty {
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod undocumented
583 0 my ($self) = @_;
584  
585 0 return !$self->get_keys();
586 }
587  
588 #
589 # (SEMI-) ATOMIC OPERATIONS
590 #
591  
592 24 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod undocumented
593 24 my $self = shift;
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 18 ▶sub append {
Subroutine append: called CC=2 SCAR=6.9
Pod undocumented
602 18 my ( $self, $key, $new ) = @_;
603  
604 18 ▶ my $current = $self->fetch($key) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $current = $self->fetch($key) or return undef 12 6 18
605 12 $self->store( $key, $current . $new );
606 12 return 1;
607 }
608  
609 16 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod undocumented
610 16 my $self = shift;
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 112 ▶sub fetch_multi_hashref {
Subroutine fetch_multi_hashref: called CC=1 SCAR=0.0
Pod undocumented
623 112 my ( $self, $keys ) = @_;
624  
625 112 return { map { ( $_, $self->fetch($_) ) } @$keys };
626 }
627  
628 112 ▶sub get_multi_hashref_objects {
Subroutine get_multi_hashref_objects: called CC=2 SCAR=6.9
Pod undocumented
629 112 my ( $self, $keys ) = @_;
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 120 ▶sub get_multi_arrayref {
Subroutine get_multi_arrayref: called CC=3 SCAR=11.0
Pod undocumented
642 120 my ( $self, $keys ) = @_;
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 36 ▶sub get_multi_hashref {
Subroutine get_multi_hashref: called CC=2 SCAR=6.9
Pod undocumented
658 36 my ( $self, $keys ) = @_;
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 96 ▶sub set_multi {
Subroutine set_multi: called CC=1 SCAR=0.0
Pod undocumented
667 96 my $self = shift;
668 96 $self->store_multi(@_);
669 }
670  
671 96 ▶sub store_multi {
Subroutine store_multi: called CC=3 SCAR=11.0
Pod undocumented
672 96 my ( $self, $key_values, $set_options ) = @_;
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 24 ▶sub remove_multi {
Subroutine remove_multi: called CC=3 SCAR=11.0
Pod undocumented
681 24 my ( $self, $keys ) = @_;
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 19 my %escapes;
694 19 for ( 0 .. 255 ) {
695 4864 $escapes{ chr($_) } = sprintf( "+%02x", $_ );
696 }
697 0 ▶my $_fail_hi = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
698 0 croak( sprintf "Can't escape multibyte character \\x{%04X}", ord( $_[0] ) );
699 19 };
700  
701 22120 ▶sub transform_key {
Subroutine transform_key: called CC=5 SCAR=16.1
Pod undocumented
702 22120 my ( $self, $key ) = @_;
703  
704 22120 ▶ if ( ref($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $key) { } 767 21353 22120
elsif (&Encode::is_utf8($key) and $key =~ /[^\x00-\xFF]/) { } 267 42172 42439
705 767 $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
210860267
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 22120 ▶ if ( length($key) > $self->max_key_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > $self->max_key_length) 78 22042 22120
711 78 $key = $self->digest_key($key);
712 }
713  
714 22120 return $key;
715 }
716  
717 174 ▶sub digest_key {
Subroutine digest_key: called CC=1 SCAR=0.0
Pod undocumented
718 174 my ( $self, $key ) = @_;
719  
720 174 return $self->key_digester->add($key)->hexdigest;
721 }
722  
723 267 ▶sub encode_key {
Subroutine encode_key: called CC=1 SCAR=0.0
Pod undocumented
724 267 my ( $self, $key ) = @_;
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 10216 ▶sub escape_for_filename {
Subroutine escape_for_filename: called CC=2 SCAR=7.4
Pod undocumented
738 10216 my ( $self, $key ) = @_;
739  
740 10216 ▶ $key =~ s/([^A-Za-z0-9_\=\-\~])/$escapes{$1} || $_fail_hi->($1)/ge;
Condition33%1/3
$escapes{$1} || &$_fail_hi($1)
l!l&&r!l&&!r
2006600
MC/DC0%0/2
$escapes{$1} || &$_fail_hi($1)
$escapes{$1} &$_fail_hi($1)
Truth table33%1/3
A || B
A= $escapes{$1}B= &$_fail_hi($1)
ABresult
1X1
011
000
741 10216 return $key;
742 }
743  
744 3207 ▶sub unescape_for_filename {
Subroutine unescape_for_filename: called CC=2 SCAR=7.0
Pod undocumented
745 3207 my ( $self, $key ) = @_;
746  
747 3207 ▶ $key =~ s/\+([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 3207 0 3207
748 3207 return $key;
749 }
750  
751 186 ▶sub is_escaped_for_filename {
Subroutine is_escaped_for_filename: called CC=1 SCAR=0.0
Pod undocumented
752 186 my ( $self, $key ) = @_;
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 11927 ▶sub _log_get_result {
Subroutine _log_get_result: called CC=1 SCAR=0.0
763 11927 my $self = shift;
764 11927 my $log = shift;
765 11927 my $msg = shift;
766 11927 $log->debug( sprintf( "%s: %s", $self->_describe_cache_get(@_), $msg ) );
767 }
768  
769 4265 ▶sub _log_set_result {
Subroutine _log_set_result: called CC=1 SCAR=0.0
770 4265 my $self = shift;
771 4265 my $log = shift;
772 4265 $log->debug( $self->_describe_cache_set(@_) );
773 }
774  
775 4 ▶sub _handle_get_error {
Subroutine _handle_get_error: called CC=1 SCAR=0.0
776 4 my $self = shift;
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 6 ▶sub _handle_set_error {
Subroutine _handle_set_error: called CC=1 SCAR=0.0
786 6 my ( $self, $error, $obj ) = @_;
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 10 ▶sub _dispatch_error_msg {
Subroutine _dispatch_error_msg: called CC=7 SCAR=19.5
796 10 my ( $self, $msg, $error, $on_error, $key ) = @_;
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 0 ▶ /^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 11931 ▶sub _describe_cache_get {
Subroutine _describe_cache_get: called CC=3 SCAR=11.0
809 11931 my ( $self, $key, $elapsed_time ) = @_;
810  
811 return
812 11931 ▶ sprintf( "cache get for namespace='%s', key='%s', cache='%s'"
Branch: 2 branches
BranchTrueFalseTotal
defined $elapsed_time ? : 11927 4 11931
defined $elapsed_time ? : 11927 4 11931
813 . ( defined($elapsed_time) ? ", time='%dms'" : "" ),
814 $self->namespace, $key, $self->label,
815 defined($elapsed_time) ? int($elapsed_time) : () );
816 }
817  
818 4271 ▶sub _describe_cache_set {
Subroutine _describe_cache_set: called CC=4 SCAR=13.9
819 4271 my ( $self, $obj, $elapsed_time ) = @_;
820  
821 4271 ▶ my $expires_str = (
Branch: 1 branch
BranchTrueFalseTotal
$obj->expires_at == 4294967295 ? : 3995 276 4271
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 4271 ▶ sprintf(
Branch: 2 branches
BranchTrueFalseTotal
defined $elapsed_time ? : 4265 6 4271
defined $elapsed_time ? : 4265 6 4271
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 19 1;
841  
842 __END__
« CHI/Test/Util.pm ↑ Summary CHI/t/Driver/FastMmap.pm »