CHI/CacheObject.pm

filter: Statementstmt 100% 122 / 122 Branchbran 95.8% 23 / 24 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 32 / 32 Podpod 38.4% 5 / 13 totaltotal 95.6% 199 / 208 CC 17 SCAR 9.7
CC
17
Cov
99%
new
6.0
is_expired
5.0
value
5.0
CRAP 2.6
« CHI/Util.pm ↑ Summary CHI/Types.pm »
1 package CHI::CacheObject;
2 19 $CHI::CacheObject::VERSION = '0.61';
3 19 ▶use CHI::Constants qw(CHI_Max_Time);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use constant f_key => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use constant f_raw_value => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use constant f_serializer => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use constant f_created_at => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use constant f_early_expires_at => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use constant f_expires_at => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use constant f_is_transformed => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use constant f_cache_version => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use constant f_value => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use constant f_packed_data => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use constant f_size => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 19 ▶use constant T_SERIALIZED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 19 ▶use constant T_UTF8_ENCODED => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 19 ▶use constant T_COMPRESSED => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 19 my $Metadata_Format = "LLLCC";
25 19 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 4313 ▶sub key { $_[0]->[f_key] }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
31 325 ▶sub created_at { $_[0]->[f_created_at] }
Subroutine created_at: called CC=1 SCAR=0.0
Pod documented
32 229 ▶sub early_expires_at { $_[0]->[f_early_expires_at] }
Subroutine early_expires_at: called CC=1 SCAR=0.0
Pod undocumented
33 4916 ▶sub expires_at { $_[0]->[f_expires_at] }
Subroutine expires_at: called CC=1 SCAR=0.0
Pod documented
34 4713 ▶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 6609 ▶sub size { $_[0]->[f_size] }
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
37  
38 210 ▶sub set_early_expires_at {
Subroutine set_early_expires_at: called CC=1 SCAR=0.0
Pod undocumented
39 210 $_[0]->[f_early_expires_at] = $_[1];
40 210 undef $_[0]->[f_packed_data];
41 }
42  
43 210 ▶sub set_expires_at {
Subroutine set_expires_at: called CC=1 SCAR=0.0
Pod undocumented
44 210 $_[0]->[f_expires_at] = $_[1];
45 210 undef $_[0]->[f_packed_data];
46 }
47  
48 ## no critic (ProhibitManyArgs)
49 4271 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
50 4271 my ( $class, $key, $value, $created_at, $early_expires_at, $expires_at,
51 $serializer, $compress_threshold )
52 = @_;
53  
54 # Serialize/encode value if necessary - does this belong here, or in
55 # Driver.pm?
56 #
57 4271 my $is_transformed = 0;
58 4271 my $raw_value = $value;
59 4271 my $size;
60 4271 ▶ if ($serializer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer) { } 4012 259 4271
61 4012 ▶ if ( ref($raw_value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $raw_value) { } 211 3801 4012
elsif (&Encode::is_utf8($raw_value)) { } 98 3703 3801
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 4012 ▶ if ( defined($compress_threshold)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compress_threshold and length $raw_value > $compress_threshold) 7 4005 4012
Condition100%3/3
defined $compress_threshold and length $raw_value > $compress_threshold
!ll&&!rl&&r
399877
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 4012 $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 4271 my $cache_version = 1;
85  
86 4271 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 11822 ▶sub unpack_from_data {
Subroutine unpack_from_data: called CC=2 SCAR=6.9
Pod undocumented
94 11822 my ( $class, $key, $data, $serializer ) = @_;
95  
96 11822 ▶ return $data if !$serializer;
Branch: 1 branch
BranchTrueFalseTotal
unless $serializer 1229 10593 11822
97 10593 my $metadata = substr( $data, 0, $Metadata_Length );
98 10593 my $raw_value = substr( $data, $Metadata_Length );
99 10593 my $obj = bless [
100 $key, $raw_value,
101 $serializer, unpack( $Metadata_Format, $metadata )
102 ],
103 $class;
104 10593 $obj->[f_packed_data] = $data;
105 10593 $obj->[f_size] = length($data);
106 10593 return $obj;
107 }
108  
109 4481 ▶sub pack_to_data {
Subroutine pack_to_data: called CC=3 SCAR=11.0
Pod undocumented
110 4481 my ($self) = @_;
111  
112 4481 ▶ return $self if !$self->serializer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->serializer 277 4204 4481
113 4204 ▶ if ( !defined( $self->[f_packed_data] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[9]) 4204 0 4204
114 my $data = pack( $Metadata_Format,
115 4204 ( @{$self} )[ f_created_at .. f_cache_version ] )
116 . $self->[f_raw_value];
117 4204 $self->[f_packed_data] = $data;
118 }
119 4204 return $self->[f_packed_data];
120 }
121  
122 10297 ▶sub is_expired {
Subroutine is_expired: called CC=5 SCAR=16.1
Pod documented
123 10297 my ($self) = @_;
124  
125 10297 my $expires_at = $self->[f_expires_at];
126 10297 ▶ return undef if $expires_at == CHI_Max_Time;
Branch: 1 branch
BranchTrueFalseTotal
if $expires_at == 4294967295 2125 8172 10297
127  
128 8172 ▶ my $time = $CHI::Driver::Test_Time || time();
Condition100%2/2
$CHI::Driver::Test_Time || time
l!l
7672500
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
129 8172 my $early_expires_at = $self->[f_early_expires_at];
130  
131 8172 ▶ 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
173324593980
$time >= $expires_at or rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at)
l!l&&r!l&&!r
223417462459
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 6470 ▶sub value {
Subroutine value: called CC=5 SCAR=16.1
Pod documented
144 6470 my ($self) = @_;
145  
146 6470 ▶ if ( !defined $self->[f_value] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[8]) 5510 960 6470
147 5510 my $value = $self->[f_raw_value];
148 5510 my $is_transformed = $self->[f_is_transformed];
149 5510 ▶ if ( $is_transformed & T_COMPRESSED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_transformed & 4) 6 5504 5510
150 6 require Compress::Zlib;
151 6 $value = Compress::Zlib::memGunzip($value);
152 }
153 5510 ▶ if ( $is_transformed & T_SERIALIZED ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_transformed & 1) { } 232 5278 5510
elsif ($is_transformed & 2) { } 77 5201 5278
154 232 $value = $self->serializer->deserialize($value);
155 }
156 elsif ( $is_transformed & T_UTF8_ENCODED ) {
157 77 $value = Encode::decode( utf8 => $value );
158 }
159 5510 $self->[f_value] = $value;
160 }
161 6470 return $self->[f_value];
162 }
163  
164 # get_* aliases for backward compatibility with Cache::Cache
165 #
166 19 *get_created_at = \&created_at;
167 19 *get_expires_at = \&expires_at;
168  
169 19 1;
170  
171 __END__
« CHI/Util.pm ↑ Summary CHI/Types.pm »