CHI/CacheObject.pm

filter: Statementstmt 100% 122 / 122 Branchbran 95.8% 23 / 24 Conditioncond 91.6% 11 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 32 / 32 Podpod 38.4% 5 / 13 totaltotal 94.2% 198 / 210 SCAR 28.3
CC
17
Cov
99%
new
6.0
is_expired
5.0
value
5.0
CRAP 17.0
« CHI/Driver/Role/IsSizeAware.pm ↑ Summary CHI/Types.pm »
1 package CHI::CacheObject;
2 20 $CHI::CacheObject::VERSION = '0.59';
3 20 ▶use CHI::Constants qw(CHI_Max_Time);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use constant f_key => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use constant f_raw_value => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use constant f_serializer => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use constant f_created_at => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use constant f_early_expires_at => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use constant f_expires_at => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use constant f_is_transformed => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use constant f_cache_version => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use constant f_value => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use constant f_packed_data => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use constant f_size => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 20 ▶use constant T_SERIALIZED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 ▶use constant T_UTF8_ENCODED => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 20 ▶use constant T_COMPRESSED => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 20 my $Metadata_Format = "LLLCC";
25 20 my $Metadata_Length = 14;
26  
27 # Eschewing Moose and hash-based objects for this class to get the extra speed.
28 # Eventually will probably write in C anyway.
29  
30 4286 ▶sub key { $_[0]->[f_key] }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
31 301 ▶sub created_at { $_[0]->[f_created_at] }
Subroutine created_at: called CC=1 SCAR=0.0
Pod documented
32 202 ▶sub early_expires_at { $_[0]->[f_early_expires_at] }
Subroutine early_expires_at: called CC=1 SCAR=0.0
Pod undocumented
33 4838 ▶sub expires_at { $_[0]->[f_expires_at] }
Subroutine expires_at: called CC=1 SCAR=0.0
Pod documented
34 4687 ▶sub serializer { $_[0]->[f_serializer] }
Subroutine serializer: called CC=1 SCAR=0.0
Pod undocumented
35 91 ▶sub _is_transformed { $_[0]->[f_is_transformed] }
Subroutine _is_transformed: called CC=1 SCAR=0.0
36 6579 ▶sub size { $_[0]->[f_size] }
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
37  
38 sub set_early_expires_at {
39 210 ▶ $_[0]->[f_early_expires_at] = $_[1];
Subroutine set_early_expires_at: called CC=1 SCAR=0.0
Pod undocumented
40 210 undef $_[0]->[f_packed_data];
41 }
42  
43 sub set_expires_at {
44 210 ▶ $_[0]->[f_expires_at] = $_[1];
Subroutine set_expires_at: called CC=1 SCAR=0.0
Pod undocumented
45 210 undef $_[0]->[f_packed_data];
46 }
47  
48 ## no critic (ProhibitManyArgs)
49 sub new {
50 4244 ▶ my ( $class, $key, $value, $created_at, $early_expires_at, $expires_at,
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
51 $serializer, $compress_threshold )
52 = @_;
53  
54 # Serialize/encode value if necessary - does this belong here, or in
55 # Driver.pm?
56 #
57 4244 my $is_transformed = 0;
58 4244 my $raw_value = $value;
59 4244 my $size;
60 4244 ▶ if ($serializer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer) { } 3985 259 4244
61 3985 ▶ if ( ref($raw_value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $raw_value) { } 211 3774 3985
elsif (&Encode::is_utf8($raw_value)) { } 98 3676 3774
62 211 $raw_value = $serializer->serialize($raw_value);
63 211 $is_transformed |= T_SERIALIZED;
64 }
65 elsif ( Encode::is_utf8($raw_value) ) {
66 98 $raw_value = Encode::encode( utf8 => $raw_value );
67 98 $is_transformed |= T_UTF8_ENCODED;
68 }
69 3985 ▶ if ( defined($compress_threshold)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compress_threshold and length $raw_value > $compress_threshold) 7 3978 3985
Condition100%3/3
defined $compress_threshold and length $raw_value > $compress_threshold
!ll&&!rl&&r
397177
MC/DC100%2/2
defined $compress_threshold and length $raw_value > $compress_threshold
defined $compress_threshold length $raw_value > $compress_threshold
Truth table100%3/3
A and B
A= defined $compress_thresholdB= length $raw_value > $compress_threshold
ABresult
0X0
100
111
70 && length($raw_value) > $compress_threshold )
71 {
72 7 require Compress::Zlib;
73 7 $raw_value = Compress::Zlib::memGzip($raw_value);
74 7 $is_transformed |= T_COMPRESSED;
75 }
76 3985 $size = length($raw_value) + $Metadata_Length;
77 }
78 else {
79 259 $size = 1;
80 }
81  
82 # Not sure where this should be set and checked
83 #
84 4244 my $cache_version = 1;
85  
86 4244 return bless [
87 $key, $raw_value, $serializer, $created_at,
88 $early_expires_at, $expires_at, $is_transformed, $cache_version,
89 $value, undef, $size
90 ], $class;
91 }
92  
93 sub unpack_from_data {
94 11818 ▶ my ( $class, $key, $data, $serializer ) = @_;
Subroutine unpack_from_data: called CC=2 SCAR=6.9
Pod undocumented
95  
96 11818 ▶ return $data if !$serializer;
Branch: 1 branch
BranchTrueFalseTotal
unless $serializer 1230 10588 11818
97 10588 my $metadata = substr( $data, 0, $Metadata_Length );
98 10588 my $raw_value = substr( $data, $Metadata_Length );
99 10588 my $obj = bless [
100 $key, $raw_value,
101 $serializer, unpack( $Metadata_Format, $metadata )
102 ],
103 $class;
104 10588 $obj->[f_packed_data] = $data;
105 10588 $obj->[f_size] = length($data);
106 10588 return $obj;
107 }
108  
109 sub pack_to_data {
110 4454 ▶ my ($self) = @_;
Subroutine pack_to_data: called CC=3 SCAR=11.0
Pod undocumented
111  
112 4454 ▶ return $self if !$self->serializer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->serializer 277 4177 4454
113 4177 ▶ if ( !defined( $self->[f_packed_data] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[9]) 4177 0 4177
114 my $data = pack( $Metadata_Format,
115 4177 ( @{$self} )[ f_created_at .. f_cache_version ] )
116 . $self->[f_raw_value];
117 4177 $self->[f_packed_data] = $data;
118 }
119 4177 return $self->[f_packed_data];
120 }
121  
122 sub is_expired {
123 10293 ▶ my ($self) = @_;
Subroutine is_expired: called CC=5 SCAR=16.1
Pod documented
124  
125 10293 my $expires_at = $self->[f_expires_at];
126 10293 ▶ return undef if $expires_at == CHI_Max_Time;
Branch: 1 branch
BranchTrueFalseTotal
if $expires_at == 4294967295 2125 8168 10293
127  
128 8168 ▶ my $time = $CHI::Driver::Test_Time || time();
Condition66%2/3
$CHI::Driver::Test_Time || time
l!l&&r!l&&!r
76685000
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
129 8168 my $early_expires_at = $self->[f_early_expires_at];
130  
131 8168 ▶ return $time >= $early_expires_at
Condition100%6/6
$time >= $early_expires_at && ($time >= $expires_at || rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at))
!ll&&!rl&&r
173324573978
$time >= $expires_at or rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at)
l!l&&r!l&&!r
223417442457
MC/DC100%3/3
$time >= $early_expires_at && ($time >= $expires_at || rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at))
$time >= $early_expires_at $time >= $expires_at rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at)
Truth table100%4/4
A && B or C
A= $time >= $early_expires_atB= $time >= $expires_atC= rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at)
ABCresult
0XX0
1000
11X1
1011
132 && (
133 $time >= $expires_at
134 || (
135 rand() < (
136 ( $time - $early_expires_at ) /
137 ( $expires_at - $early_expires_at )
138 )
139 )
140 );
141 }
142  
143 sub value {
144 6441 ▶ my ($self) = @_;
Subroutine value: called CC=5 SCAR=16.1
Pod documented
145  
146 6441 ▶ if ( !defined $self->[f_value] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[8]) 5497 944 6441
147 5497 my $value = $self->[f_raw_value];
148 5497 my $is_transformed = $self->[f_is_transformed];
149 5497 ▶ if ( $is_transformed & T_COMPRESSED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_transformed & 4) 6 5491 5497
150 6 require Compress::Zlib;
151 6 $value = Compress::Zlib::memGunzip($value);
152 }
153 5497 ▶ if ( $is_transformed & T_SERIALIZED ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_transformed & 1) { } 233 5264 5497
elsif ($is_transformed & 2) { } 77 5187 5264
154 233 $value = $self->serializer->deserialize($value);
155 }
156 elsif ( $is_transformed & T_UTF8_ENCODED ) {
157 77 $value = Encode::decode( utf8 => $value );
158 }
159 5497 $self->[f_value] = $value;
160 }
161 6441 return $self->[f_value];
162 }
163  
164 # get_* aliases for backward compatibility with Cache::Cache
165 #
166 20 *get_created_at = \&created_at;
167 20 *get_expires_at = \&expires_at;
168  
169 20 1;
170  
171 __END__
« CHI/Driver/Role/IsSizeAware.pm ↑ Summary CHI/Types.pm »