| 1 | package CHI::CacheObject; | ||||||||||||||||||||||||||||||||||
| 2 | 19 | $CHI::CacheObject::VERSION = '0.60'; | |||||||||||||||||||||||||||||||||
| 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 | 4305 | ▶ | sub key { $_[0]->[f_key] } | ||||||||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 31 | 317 | ▶ | sub created_at { $_[0]->[f_created_at] } | ||||||||||||||||||||||||||||||||
|
Subroutine
created_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 32 | 221 | ▶ | sub early_expires_at { $_[0]->[f_early_expires_at] } | ||||||||||||||||||||||||||||||||
|
Subroutine
early_expires_at: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 33 | 4892 | ▶ | sub expires_at { $_[0]->[f_expires_at] } | ||||||||||||||||||||||||||||||||
|
Subroutine
expires_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 34 | 4706 | ▶ | 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 | 6600 | ▶ | 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 | 4263 | ▶ | 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 | 4263 | my $is_transformed = 0; | |||||||||||||||||||||||||||||||||
| 58 | 4263 | my $raw_value = $value; | |||||||||||||||||||||||||||||||||
| 59 | 4263 | my $size; | |||||||||||||||||||||||||||||||||
| 60 | 4263 | ▶ | if ($serializer) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 61 | 4004 | ▶ | if ( ref($raw_value) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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 | 4004 | ▶ | if ( defined($compress_threshold) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $compress_threshold and length $raw_value > $compress_threshold
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
| |||||||||||||||||||||||||||||||||||
| 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 | 4004 | $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 | 4263 | my $cache_version = 1; | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 4263 | 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 | 11819 | ▶ | my ( $class, $key, $data, $serializer ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
unpack_from_data: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 11819 | ▶ | return $data if !$serializer; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | 10591 | my $metadata = substr( $data, 0, $Metadata_Length ); | |||||||||||||||||||||||||||||||||
| 98 | 10591 | my $raw_value = substr( $data, $Metadata_Length ); | |||||||||||||||||||||||||||||||||
| 99 | 10591 | my $obj = bless [ | |||||||||||||||||||||||||||||||||
| 100 | $key, $raw_value, | ||||||||||||||||||||||||||||||||||
| 101 | $serializer, unpack( $Metadata_Format, $metadata ) | ||||||||||||||||||||||||||||||||||
| 102 | ], | ||||||||||||||||||||||||||||||||||
| 103 | $class; | ||||||||||||||||||||||||||||||||||
| 104 | 10591 | $obj->[f_packed_data] = $data; | |||||||||||||||||||||||||||||||||
| 105 | 10591 | $obj->[f_size] = length($data); | |||||||||||||||||||||||||||||||||
| 106 | 10591 | return $obj; | |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | sub pack_to_data { | ||||||||||||||||||||||||||||||||||
| 110 | 4473 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
pack_to_data: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 4473 | ▶ | return $self if !$self->serializer; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 113 | 4196 | ▶ | if ( !defined( $self->[f_packed_data] ) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 114 | my $data = pack( $Metadata_Format, | ||||||||||||||||||||||||||||||||||
| 115 | 4196 | ( @{$self} )[ f_created_at .. f_cache_version ] ) | |||||||||||||||||||||||||||||||||
| 116 | . $self->[f_raw_value]; | ||||||||||||||||||||||||||||||||||
| 117 | 4196 | $self->[f_packed_data] = $data; | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | 4196 | return $self->[f_packed_data]; | |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | sub is_expired { | ||||||||||||||||||||||||||||||||||
| 123 | 10295 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
is_expired: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 10295 | my $expires_at = $self->[f_expires_at]; | |||||||||||||||||||||||||||||||||
| 126 | 10295 | ▶ | return undef if $expires_at == CHI_Max_Time; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | 8170 | ▶ | my $time = $CHI::Driver::Test_Time || time(); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$CHI::Driver::Test_Time || time
MC/DC0%0/2
$CHI::Driver::Test_Time || time
$CHI::Driver::Test_Time
time
Truth table66%2/3
A || B
A= $CHI::Driver::Test_TimeB= time
| |||||||||||||||||||||||||||||||||||
| 129 | 8170 | my $early_expires_at = $self->[f_early_expires_at]; | |||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | 8170 | ▶ | 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))
$time >= $expires_at or rand() < ($time - $early_expires_at) / ($expires_at - $early_expires_at)
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)
| |||||||||||||||||||||||||||||||||||
| 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 | 6459 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
value: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 6459 | ▶ | if ( !defined $self->[f_value] ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 5527 | my $value = $self->[f_raw_value]; | |||||||||||||||||||||||||||||||||
| 148 | 5527 | my $is_transformed = $self->[f_is_transformed]; | |||||||||||||||||||||||||||||||||
| 149 | 5527 | ▶ | if ( $is_transformed & T_COMPRESSED ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 150 | 6 | require Compress::Zlib; | |||||||||||||||||||||||||||||||||
| 151 | 6 | $value = Compress::Zlib::memGunzip($value); | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | 5527 | ▶ | if ( $is_transformed & T_SERIALIZED ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 154 | 233 | $value = $self->serializer->deserialize($value); | |||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | elsif ( $is_transformed & T_UTF8_ENCODED ) { | ||||||||||||||||||||||||||||||||||
| 157 | 77 | $value = Encode::decode( utf8 => $value ); | |||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||
| 159 | 5527 | $self->[f_value] = $value; | |||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||
| 161 | 6459 | 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__ | ||||||||||||||||||||||||||||||||||