blib/lib/Memoize/Memcached.pm

filter: Statementstmt 27.5% 40 / 145 Branchbran 5.7% 3 / 52 Conditioncond 28.5% 2 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 34.6% 9 / 26 Podpod 100% 2 / 2 totaltotal 24.1% 57 / 236 CC 35 SCAR 36.0
CC
35
Cov
22%
_memcached_setup
110.0
flush_cache
72.0
memoize_memcached
42.0
CRAP 36.5
↑ Summary
1 package Memoize::Memcached;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Memoize qw( unmemoize );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Cache::Memcached;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.04';
11  
12 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 $Data::Dumper::Sortkeys = 1;
14  
15  
16 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our @EXPORT = qw( memoize_memcached );
19 2 our @EXPORT_OK = qw( unmemoize flush_cache );
20 2 our %EXPORT_TAGS = (
21 all => [ @EXPORT, @EXPORT_OK ],
22 );
23  
24  
25 2 ▶use fields qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
26 key_prefix
27 expire_time
28 memcached_obj
29 key_error
30 scalar_error
31 2 );
32  
33  
34  
35 2 my %memo_data;
36 my %memcached_config;
37  
38  
39 0 ▶sub memoize_memcached {
Subroutine memoize_memcached: not called CC=6 SCAR=37.4
Pod documented
40 # Be sure to leave @_ intact in case we need to redirect to
41 # 'Memoize::memoize'.
42 0 my ($function, %args) = @_;
43  
44 0 ▶ if (exists $args{LIST_CACHE} or exists $args{ARRAY_CACHE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"LIST_CACHE"} or exists $args{"ARRAY_CACHE"}) 0 0 0
Condition0%0/3
exists $args{"LIST_CACHE"} or exists $args{"ARRAY_CACHE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $args{"LIST_CACHE"} or exists $args{"ARRAY_CACHE"}
exists $args{"LIST_CACHE"} exists $args{"ARRAY_CACHE"}
Truth table0%0/3
A or B
A= exists $args{"LIST_CACHE"}B= exists $args{"ARRAY_CACHE"}
ABresult
1X1
011
000
45 0 carp "Call to 'memoize_memcached' with a cache option passed to 'memoize'";
46 0 goto &Memoize::memoize;
47 }
48  
49 0 ▶ my $memcached_args = delete $args{memcached} || {};
Condition0%0/2
delete $args{'memcached'} || {}
l!l
00
MC/DC0%0/1
delete $args{'memcached'} || {}
delete $args{'memcached'}
Truth table0%0/2
A
A= delete $args{'memcached'}
Aresult
11
00
50 0 ▶ croak "Invalid memcached argument (expected a hash)"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $memcached_args eq "HASH" 0 0 0
51 unless ref $memcached_args eq 'HASH';
52  
53 _memcached_setup(
54 0 %{$memcached_args},
55 memoized_function => $function,
56 );
57 0 $args{LIST_CACHE} = [ HASH => $memo_data{$function}{list_cache} ];
58 0 $args{SCALAR_CACHE} = [ HASH => $memo_data{$function}{scalar_cache} ];
59  
60 # If we are passed a normalizer, we need to keep a version of it
61 # around for flush_cache to use. This breaks encapsulation. And it
62 # is just plain ugly.
63 $memo_data{$function}{normalizer} = Memoize::_make_cref($args{NORMALIZER}, scalar caller)
64 0 ▶ if defined $args{NORMALIZER};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"NORMALIZER"} 0 0 0
65  
66 # Rebuild @_ since there is a good probability we have removed some
67 # arguments meant for us and added the cache arguments.
68 0 @_ = ($function, %args);
69 0 goto &Memoize::memoize;
70 }
71  
72  
73 # Unfortunately, we need to do some magic to make flush_cache sorta
74 # work. I don't think this is enough magic yet.
75  
76 0 ▶sub flush_cache {
Subroutine flush_cache: not called CC=8 SCAR=42.8
Pod documented
77 # If we have exactly 1 argument then we are probably expected to
78 # clear the cache for a single function. Pass this along to
79 # Memoize, even though it cannot be handled correctly at this time
80 # (whatever we do will be wrong, anyway).
81  
82 0 ▶ goto &Memoize::flush_cache if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
83  
84  
85 # If we have more than 1 argument, we are probably expected to clear
86 # a single call signature for a function. This we can almost do
87 # properly.
88  
89 # Even though we can do this "properly", it is still very bad. This
90 # breaks encapsulation pretty disgustingly. With any luck Memoize
91 # will eventually be patched to do this for us...
92  
93 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
94 0 my ($function, @args) = @_;
95 0 my $cur_memo = $memo_data{$function};
96 0 my $normalizer = $memo_data{$function}{normalizer};
97 0 my $array_argstr;
98 my $scalar_argstr;
99 0 ▶ if (defined $normalizer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $normalizer) { } 0 0 0
100 0 ($array_argstr) = $normalizer->(@_);
101 0 $scalar_argstr = $normalizer->(@_);
102 }
103 else { # Default normalizer
104 0 local $^W = 0;
105 0 $array_argstr = $scalar_argstr = join chr(28), @args;
106 }
107 0 for my $cache (qw( list_cache scalar_cache )) {
108 0 for my $argstr ($scalar_argstr, $array_argstr) {
109 0 delete $cur_memo->{$cache}{$argstr};
110 }
111 }
112 0 return 1;
113 }
114  
115  
116 # Currently all memoized functions share memcached config, so just
117 # find the first valid object and flush cache.
118  
119 0 for my $function (keys %memo_data) {
120 0 ▶ next unless $memo_data{$function}{list_obj};
Branch: 1 branch
BranchTrueFalseTotal
unless $memo_data{$function}{"list_obj"} 0 0 0
121 0 $memo_data{$function}{list_obj}{memcached_obj}->flush_all;
122 0 last;
123 }
124  
125 0 return 1;
126 }
127  
128  
129 2 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
130 2 my ($class) = @_;
131  
132 # Search through the arg list for the 'memcached' arg, process it,
133 # and remove it (and its associated value) from the arg list in
134 # anticipation of passing off to Exporter.
135 2 for my $idx ($[ + 1 .. $#_) {
136 3 ▶ my $arg = $_[$idx] || q();
Condition100%2/2
$_[$idx] || ''
l!l
21
MC/DC100%1/1
$_[$idx] || ''
$_[$idx]
Truth table100%2/2
A
A= $_[$idx]
Aresult
11
00
137 3 ▶ next unless $arg eq 'memcached';
Branch: 1 branch
BranchTrueFalseTotal
unless $arg eq "memcached" 2 1 3
138 1 (undef, my $memcached_config) = splice @_, $idx, 2;
139 1 ▶ croak "Invalid memcached config (expected a hash ref)"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $memcached_config eq "HASH" 0 1 1
140 unless ref $memcached_config eq 'HASH';
141 1 %memcached_config = %{$memcached_config};
142 }
143  
144 2 return $class->export_to_level(1, @_);
145 }
146  
147  
148 0 ▶sub _memcached_setup {
Subroutine _memcached_setup: not called CC=10 SCAR=47.0
149 0 my %args = %memcached_config;
150 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
151 0 my $key = shift;
152 0 my $value = shift;
153 0 $args{$key} = $value;
154 }
155  
156 0 my $function = delete $args{memoized_function};
157 0 my $list_key_prefix = delete $args{list_key_prefix};
158 0 my $scalar_key_prefix = delete $args{scalar_key_prefix};
159  
160 0 ▶ $args{key_prefix} = 'memoize-' unless defined $args{key_prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"key_prefix"} 0 0 0
161  
162 0 ▶ croak "Missing function name for memcached setup"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $function 0 0 0
163 unless defined $function;
164 0 my $tie_data = $memo_data{$function} = {
165 list_obj => undef,
166 list_cache => {},
167 scalar_obj => undef,
168 scalar_cache => {},
169 };
170  
171 0 my %cur_args = %args;
172 $cur_args{key_prefix}
173 0 ▶ .= (defined $function ? "$function-" : '-')
Branch: 2 branches
BranchTrueFalseTotal
defined $function ? : 0 0 0
defined $list_key_prefix ? : 0 0 0
174 . (defined $list_key_prefix ? $list_key_prefix : 'list-')
175 ;
176 0 ▶ $tie_data->{list_obj} = tie %{$tie_data->{list_cache}}, __PACKAGE__, %cur_args
Branch: 1 branch
BranchTrueFalseTotal
$tie_data->{"list_obj"} = tie(%{$$tie_data{"list_cache"};}, __PACKAGE__, %cur_args) or die "Error creating list cache" 0 0 0
177 or die "Error creating list cache";
178  
179 0 %cur_args = %args;
180 $cur_args{key_prefix}
181 0 ▶ .= (defined $function ? "$function-" : '-')
Branch: 2 branches
BranchTrueFalseTotal
defined $function ? : 0 0 0
defined $scalar_key_prefix ? : 0 0 0
182 . (defined $scalar_key_prefix ? $scalar_key_prefix : 'scalar-')
183 ;
184 0 ▶ $tie_data->{scalar_obj} = tie %{$tie_data->{scalar_cache}}, __PACKAGE__, %cur_args
Branch: 1 branch
BranchTrueFalseTotal
$tie_data->{"scalar_obj"} = tie(%{$$tie_data{"scalar_cache"};}, __PACKAGE__, %cur_args) or die "Error creating scalar cache" 0 0 0
185 or die "Error creating scalar cache";
186  
187 0 return 1;
188 }
189  
190  
191 0 ▶sub _new {
Subroutine _new: not called CC=2 SCAR=17.9
192 0 my $class = shift;
193 0 ▶ croak "Called new in object context" if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
194 0 my $self = fields::new($class);
195 0 $self->_init(@_);
196 0 return $self;
197 }
198  
199  
200 0 ▶sub _init {
Subroutine _init: not called CC=5 SCAR=34.0
201 0 my $self = shift;
202 0 my %args = @_;
203 0 %{$self} = ();
204  
205 0 $self->{key_prefix} = delete $args{key_prefix};
206 0 ▶ $self->{key_prefix} = q() unless defined $self->{key_prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key_prefix"} 0 0 0
207 0 ▶ $self->{expire_time} = exists $args{expire_time} ? delete $args{expire_time} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'expire_time'} ? : 0 0 0
208  
209 # Default these to false so that we can use Data::Dumper on tied
210 # hashes by default. Yes, it will show them as empty, but I doubt
211 # someone running Dumper on this tied hash would really want to dump
212 # the contents of the memcached cache (and they can't anyway).
213  
214 $self->{$_} = exists $args{$_} ? delete $args{$_} : !1
215 0 ▶ for qw( key_error scalar_error );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} ? : 0 0 0
216  
217 0 $self->{memcached_obj} = Cache::Memcached->new(\%args);
218  
219 0 return $self;
220 }
221  
222  
223 0 ▶sub _get_key {
Subroutine _get_key: not called CC=1 SCAR=6.9
224 0 my $self = shift;
225 0 my $key = shift;
226 0 return $self->{key_prefix} . $key;
227 }
228  
229  
230 0 ▶sub _key_lookup_error {
Subroutine _key_lookup_error: not called CC=1 SCAR=6.9
231 0 croak "Key lookup functionality is not implemented by memcached";
232 }
233  
234  
235 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=1 SCAR=6.9
236 0 my $class = shift;
237 0 return $class->_new(@_);
238 }
239  
240  
241 0 ▶sub STORE {
Subroutine STORE: not called CC=2 SCAR=17.9
242 0 my $self = shift;
243 0 my $key = $self->_get_key(shift);
244 0 my $value = shift;
245 0 my @args = ($key, $value);
246 0 ▶ push @args, $self->{expire_time} if defined $self->{expire_time};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"expire_time"} 0 0 0
247 0 $self->{memcached_obj}->set(@args);
248 0 return $self;
249 }
250  
251  
252 0 ▶sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
253 0 my $self = shift;
254 0 my $key = $self->_get_key(shift);
255 0 return $self->{memcached_obj}->get($key);
256 }
257  
258  
259 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
260 0 my $self = shift;
261 0 return defined $self->FETCH(@_);
262 }
263  
264  
265 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
266 0 my $self = shift;
267 0 my $key = $self->_get_key(shift);
268 0 $self->{memcached_obj}->delete($key);
269 0 return $self;
270 }
271  
272  
273 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
274 0 my $self = shift;
275 # This is not safe because all object share memcached setup.
276 0 $self->{memcached_obj}->flush_all;
277 0 return $self;
278 }
279  
280  
281 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=2 SCAR=17.9
282 0 my $self = shift;
283 0 ▶ return unless $self->{key_error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"key_error"} 0 0 0
284 0 $self->_key_lookup_error;
285 }
286  
287  
288 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
289 0 my $self = shift;
290 0 ▶ return unless $self->{key_error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"key_error"} 0 0 0
291 0 $self->_key_lookup_error;
292 }
293  
294  
295 0 ▶sub SCALAR {
Subroutine SCALAR: not called CC=2 SCAR=17.9
296 0 my $self = shift;
297 0 ▶ return unless $self->{scalar_error};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"scalar_error"} 0 0 0
298 # I think this error still makes sense, since to determine if the
299 # cache has content one would need to first determine if the cache
300 # contains keys.
301 0 $self->_key_lookup_error;
302 }
303  
304  
305 0 ▶sub UNTIE {
Subroutine UNTIE: not called CC=1 SCAR=6.9
306 0 my $self = shift;
307 0 $self->{memcached_obj}->disconnect_all;
308 0 return $self;
309 }
310  
311  
312  
313 2 1;
314  
315 __END__
↑ Summary