blib/lib/Cache/Memcached/Mock.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 87.5% 21 / 24 Conditioncond 60.0% 9 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 86.9% 20 / 23 Podpod 0.0% 0 / 13 totaltotal 77.7% 136 / 175 CC 20 SCAR 11.5
CC
20
Cov
87%
get_multi
6.0
new
5.5
set
5.2
CRAP 3.1
↑ Summary
1 # ABSTRACT: A mock class for Cache::Memcached
2 package Cache::Memcached::Mock;
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 our $VERSION = '0.07';
10  
11 sub VALUE () {0}
12 sub TIMESTAMP () {1}
13 sub REFERENCE () {2}
14  
15 # All instances share the memory space
16 8 our %MEMCACHE_STORAGE = ();
17  
18 2 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod undocumented
19 2 my ($self, $key, $value, $expiry_time) = @_;
20 2 ▶ if (exists $MEMCACHE_STORAGE{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Cache::Memcached::Mock::MEMCACHE_STORAGE{$key}) 1 1 2
21 1 return;
22 }
23 1 return $self->set($key, $value, $expiry_time);
24 }
25  
26 7 ▶sub new {
Subroutine new: called CC=5 SCAR=17.0
Pod undocumented
27  
28 7 my ($class, $options) = @_;
29  
30 7 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
31  
32 7 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
07
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
33  
34 # Default memcached size limit
35 7 ▶ $options->{size_limit} ||= 1024 * 1024;
Condition50%1/2
$options->{"size_limit"} ||= 1048576
l!l
07
MC/DC0%0/1
$options->{"size_limit"} ||= 1048576
$options->{"size_limit"}
Truth table50%1/2
A
A= $options->{"size_limit"}
Aresult
11
00
36  
37 # Default unsigned integer bit width when incrementing/decrementing.
38 7 ▶ $options->{bit_width} ||= 32;
Condition50%1/2
$options->{"bit_width"} ||= 32
l!l
07
MC/DC0%0/1
$options->{"bit_width"} ||= 32
$options->{"bit_width"}
Truth table50%1/2
A
A= $options->{"bit_width"}
Aresult
11
00
39  
40 7 my $self = { %{$options} };
41  
42 7 bless $self, $class;
43 7 $self->flush_all();
44  
45 7 return $self;
46 }
47  
48 3 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.4
Pod undocumented
49 3 my ($self, $key) = @_;
50 3 ▶ if (!exists $MEMCACHE_STORAGE{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Cache::Memcached::Mock::MEMCACHE_STORAGE{$key}) 0 3 3
51 0 return;
52 }
53 3 delete $MEMCACHE_STORAGE{$key};
54 3 return 1;
55 }
56  
57 0 ▶sub disconnect_all {
Subroutine disconnect_all: not called CC=1 SCAR=6.9
Pod undocumented
58 return # noop
59 0 }
60  
61 7 ▶sub flush_all {
Subroutine flush_all: called CC=1 SCAR=0.0
Pod undocumented
62 7 %MEMCACHE_STORAGE = ();
63 7 return;
64 }
65  
66 20 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
67 20 my ($self, $key) = @_;
68 20 return $self->get_multi($key)->{$key};
69 }
70  
71 22 ▶sub get_multi {
Subroutine get_multi: called CC=6 SCAR=17.9
Pod undocumented
72 22 my ($self, @keys) = @_;
73 22 my %pairs;
74  
75 22 for my $key (@keys) {
76 28 ▶ if (exists $MEMCACHE_STORAGE{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Cache::Memcached::Mock::MEMCACHE_STORAGE{$key}) 19 9 28
77  
78 # Check if value had an expire time
79 19 my $struct = $MEMCACHE_STORAGE{$key};
80 19 my $expiry_time = $struct->[TIMESTAMP];
81  
82 19 ▶ if (defined $expiry_time && (time > $expiry_time)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expiry_time and time > $expiry_time) { } 2 17 19
Condition100%3/3
defined $expiry_time and time > $expiry_time
!ll&&!rl&&r
1522
MC/DC100%2/2
defined $expiry_time and time > $expiry_time
defined $expiry_time time > $expiry_time
Truth table100%3/3
A and B
A= defined $expiry_timeB= time > $expiry_time
ABresult
0X0
100
111
83 2 delete $MEMCACHE_STORAGE{$key};
84 }
85 else {
86 17 ▶ $pairs{$key}
Branch: 1 branch
BranchTrueFalseTotal
$struct->[2] ? : 0 17 17
87 = $struct->[REFERENCE]
88 ? Storable::thaw($struct->[VALUE])
89 : $struct->[VALUE];
90 }
91 }
92 }
93  
94 22 return \%pairs;
95 }
96  
97 2 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod undocumented
98 2 my ($self, $key, $value, $expiry_time) = @_;
99 2 ▶ if (!exists $MEMCACHE_STORAGE{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Cache::Memcached::Mock::MEMCACHE_STORAGE{$key}) 1 1 2
100 1 return;
101 }
102 1 return $self->set($key, $value, $expiry_time);
103 }
104  
105 18 ▶sub set {
Subroutine set: called CC=5 SCAR=16.4
Pod undocumented
106 18 my ($self, $key, $value, $expiry_time) = @_;
107 18 my $size_limit = $self->_size_limit();
108 18 my $is_ref = 0;
109  
110 18 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 18 18
111 0 $is_ref = 1;
112 0 $value = Storable::nfreeze($value);
113 }
114  
115 # Can't store values longer than (default) 1Mb limit
116 18 ▶ if (defined $value and bytes::length($value) > $size_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and &bytes::length($value) > $size_limit) 1 17 18
Condition66%2/3
defined $value and &bytes::length($value) > $size_limit
!ll&&!rl&&r
0171
MC/DC50%1/2
defined $value and &bytes::length($value) > $size_limit
defined $value &bytes::length($value) > $size_limit
Truth table66%2/3
A and B
A= defined $valueB= &bytes::length($value) > $size_limit
ABresult
0X0
100
111
117 1 return;
118 }
119  
120 17 ▶ if ($expiry_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expiry_time) { } 2 15 17
121 2 $expiry_time += time();
122 }
123 else {
124 15 $expiry_time = undef;
125 }
126  
127 17 $MEMCACHE_STORAGE{$key} = [ $value, $expiry_time, $is_ref ];
128  
129 17 return 1;
130 }
131  
132 0 ▶sub set_servers {
Subroutine set_servers: not called CC=1 SCAR=6.9
Pod undocumented
133 0 my ($self, $servers) = @_;
134 0 return ($self->{servers} = $servers);
135 }
136  
137 0 ▶sub set_compress_threshold {
Subroutine set_compress_threshold: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($self, $comp_thr) = @_;
139 0 return ($self->{compress_threshold} = $comp_thr - 0);
140 }
141  
142 # XXX NIY
143 #sub set_readonly {
144 # my ($self, $readonly) = @_;
145 # return ($self->{readonly} = $readonly);
146 #}
147  
148 12 ▶sub incr {
Subroutine incr: called CC=1 SCAR=0.0
Pod undocumented
149 12 my ($self, $key, $offset) = @_;
150 12 return $self->_incr_or_decr($key, $offset, '_add');
151 }
152  
153 6 ▶sub decr {
Subroutine decr: called CC=1 SCAR=0.0
Pod undocumented
154 6 my ($self, $key, $offset) = @_;
155 6 return $self->_incr_or_decr($key, $offset, '_subtract');
156 }
157  
158 11 ▶sub _add {
Subroutine _add: called CC=1 SCAR=0.0
159 11 my ($self, $x, $y) = @_;
160 11 my $result = $self->_to_uint($x) + $self->_to_uint($y);
161
162 11 return $result % (2 ** $self->_bit_width());
163 }
164  
165 47 ▶sub _bit_width {
Subroutine _bit_width: called CC=1 SCAR=0.0
166 47 my ($self) = @_;
167 47 return $self->{bit_width};
168 }
169  
170 18 ▶sub _incr_or_decr {
Subroutine _incr_or_decr: called CC=3 SCAR=11.0
171 18 my ($self, $key, $offset, $operation) = @_;
172 18 ▶ return unless exists $MEMCACHE_STORAGE{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Cache::Memcached::Mock::MEMCACHE_STORAGE{$key} 2 16 18
173
174 16 ▶ $offset = 1 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 9 7 16
175 16 my $new_val = $self->$operation($MEMCACHE_STORAGE{$key}->[VALUE], $offset);
176
177 16 return ($MEMCACHE_STORAGE{$key}->[VALUE] = $new_val);
178 }
179  
180 18 ▶sub _size_limit {
Subroutine _size_limit: called CC=1 SCAR=0.0
181 18 my ($self) = @_;
182 18 return $self->{size_limit};
183 }
184  
185 5 ▶sub _subtract {
Subroutine _subtract: called CC=2 SCAR=6.9
186 5 my ($self, $x, $y) = @_;
187 5 my $result = $self->_to_uint($x) - $self->_to_uint($y);
188
189 5 ▶ return $result <= 0 ? 0 : $result % (2 ** $self->_bit_width());
Branch: 1 branch
BranchTrueFalseTotal
$result <= 0 ? : 1 4 5
190 }
191  
192 32 ▶sub _to_uint {
Subroutine _to_uint: called CC=1 SCAR=0.0
193 32 my ($self, $n) = @_;
194 32 return $n & (2 ** $self->_bit_width() - 1);
195 }
196  
197 8 1;
198  
199 __END__
↑ Summary