blib/lib/Cache/Redis.pm

filter: Statementstmt 13.8% 14 / 101 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 20.0% 4 / 20 Podpod 100% 8 / 8 totaltotal 12.5% 26 / 208 CC 31 SCAR 48.6
CC
31
Cov
9%
new
342.0
set
20.0
_set
20.0
CRAP 128.9
↑ Summary Build »
1 package Cache::Redis;
2 13 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 our $VERSION = '0.12';
7  
8 13 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 my $_mp;
11 sub _mp {
12 0 ▶ $_mp ||= Data::MessagePack->new->utf8;
Subroutine _mp: not called CC=2 SCAR=17.9
Condition0%0/3
$_mp ||= "Data::MessagePack"->new->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$_mp ||= "Data::MessagePack"->new->utf8
$_mp "Data::MessagePack"->new->utf8
Truth table0%0/3
A ||= B
A= $_mpB= "Data::MessagePack"->new->utf8
ABresult
1X1
011
000
13 }
14 sub _mp_serialize {
15 0 ▶ _mp->pack(@_);
Subroutine _mp_serialize: not called CC=1 SCAR=6.9
16 }
17 sub _mp_deserialize {
18 0 ▶ _mp->unpack(@_);
Subroutine _mp_deserialize: not called CC=1 SCAR=6.9
19 }
20  
21 sub _mk_serialize {
22 0 ▶ my $code = shift;
Subroutine _mk_serialize: not called CC=1 SCAR=6.9
23  
24 return sub {
25 0 ▶ my $data = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
26  
27 0 my $flags; # for future extention
28 0 my $store_date = [$data, $flags];
29 0 $code->($store_date);
30 0 };
31 }
32  
33 sub _mk_deserialize {
34 0 ▶ my $code = shift;
Subroutine _mk_deserialize: not called CC=1 SCAR=6.9
35  
36 return sub {
37 0 ▶ my $data = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
38  
39 0 my ($org, $flags) = @{$code->($data)};
40 0 $org;
41 0 };
42 }
43  
44 sub new {
45 0 ▶ my $class = shift;
Subroutine new: not called CC=18 SCAR=58.3
Pod documented
46  
47 0 ▶ my $args = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
48 0 ▶ my $default_expires_in = delete $args->{default_expires_in} || 60*60*24 * 30;
Condition0%0/2
delete $args->{'default_expires_in'} || 2592000
l!l
00
MC/DC0%0/1
delete $args->{'default_expires_in'} || 2592000
delete $args->{'default_expires_in'}
Truth table0%0/2
A
A= delete $args->{'default_expires_in'}
Aresult
11
00
49 0 ▶ my $namespace = delete $args->{namespace} || '';
Condition0%0/2
delete $args->{'namespace'} || ''
l!l
00
MC/DC0%0/1
delete $args->{'namespace'} || ''
delete $args->{'namespace'}
Truth table0%0/2
A
A= delete $args->{'namespace'}
Aresult
11
00
50 0 ▶ my $nowait = delete $args->{nowait} || 0;
Condition0%0/2
delete $args->{'nowait'} || 0
l!l
00
MC/DC0%0/1
delete $args->{'nowait'} || 0
delete $args->{'nowait'}
Truth table0%0/2
A
A= delete $args->{'nowait'}
Aresult
11
00
51 0 ▶ my $redis_class = delete $args->{redis_class} || 'Redis';
Condition0%0/2
delete $args->{'redis_class'} || 'Redis'
l!l
00
MC/DC0%0/1
delete $args->{'redis_class'} || 'Redis'
delete $args->{'redis_class'}
Truth table0%0/2
A
A= delete $args->{'redis_class'}
Aresult
11
00
52  
53 0 my $redis = delete $args->{redis};
54 0 my $serializer = delete $args->{serializer};
55 0 my $serialize_methods = delete $args->{serialize_methods};
56 0 ▶ die '`serializer` and `serialize_methods` is exclusive option' if $serializer && $serialize_methods;
Branch: 1 branch
BranchTrueFalseTotal
if $serializer and $serialize_methods 0 0 0
Condition0%0/3
$serializer and $serialize_methods
!ll&&!rl&&r
000
MC/DC0%0/2
$serializer and $serialize_methods
$serializer $serialize_methods
Truth table0%0/3
A and B
A= $serializerB= $serialize_methods
ABresult
0X0
100
111
57 0 ▶ $serializer ||= 'Storable' unless $serialize_methods;
Branch: 1 branch
BranchTrueFalseTotal
unless $serialize_methods 0 0 0
Condition0%0/2
$serializer ||= "Storable"
l!l
00
MC/DC0%0/1
$serializer ||= "Storable"
$serializer
Truth table0%0/2
A
A= $serializer
Aresult
11
00
58  
59 0 my ($serialize, $deserialize);
60 0 ▶ if ($serializer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer) 0 0 0
61 0 ▶ if ($serializer eq 'Storable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($serializer eq 'Storable') { } 0 0 0
elsif ($serializer eq 'JSON') { } 0 0 0
62 0 require Storable;
63 0 $serialize_methods = [\&Storable::nfreeze, \&Storable::thaw];
64 }
65 elsif ($serializer eq 'JSON') {
66 0 require JSON::XS;
67 0 $serialize_methods = [\&JSON::XS::encode_json, \&JSON::XS::decode_json];
68 }
69 }
70  
71 0 ▶ if ($serialize_methods) {
Branch: 2 branches
BranchTrueFalseTotal
if ($serialize_methods) { } 0 0 0
elsif ($serializer eq 'MessagePack') { } 0 0 0
72 0 $serialize = _mk_serialize $serialize_methods->[0];
73 0 $deserialize = _mk_deserialize $serialize_methods->[1];
74 }
75 elsif ($serializer eq 'MessagePack') {
76 0 require Data::MessagePack;
77 0 $serialize = \&_mp_serialize;
78 0 $deserialize = \&_mp_deserialize;
79 }
80 0 ▶ die 'Serializer is not found' if !$serialize || !$deserialize;
Branch: 1 branch
BranchTrueFalseTotal
unless $serialize and $deserialize 0 0 0
Condition0%0/3
$serialize and $deserialize
!ll&&!rl&&r
000
MC/DC0%0/2
$serialize and $deserialize
$serialize $deserialize
Truth table0%0/3
A and B
A= $serializeB= $deserialize
ABresult
0X0
100
111
81  
82 0 ▶ unless ( $redis ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($redis) 0 0 0
83 0 load $redis_class;
84 0 $redis = $redis_class->new(
85 encoding => undef,
86 %$args
87 );
88 }
89  
90 bless {
91 0 default_expires_in => $default_expires_in,
92 serialize => $serialize,
93 deserialize => $deserialize,
94 redis => $redis,
95 namespace => $namespace,
96 nowait => $nowait,
97 }, $class;
98 }
99  
100 sub get {
101 0 ▶ my ($self, $key) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
102 0 $key = $self->{namespace} . $key;
103  
104 0 my $data = $self->{redis}->get($key);
105  
106 0 ▶ defined $data ? $self->{deserialize}->($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 0 0
107 }
108  
109 sub get_multi {
110 0 ▶ my ($self, @keys) = @_;
Subroutine get_multi: not called CC=3 SCAR=24.8
Pod documented
111 0 @keys = map { $self->{namespace} . $_ } @keys;
112  
113 0 my @data = $self->{redis}->mget(@keys);
114  
115 0 my $i = 0;
116 0 my $ret = {};
117 0 for my $key ( @keys ) {
118 0 ▶ if ( defined $data[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data[$i]) 0 0 0
119 0 $ret->{$key} = $self->{deserialize}->($data[$i]);
120 }
121 0 $i++;
122 }
123  
124 0 $ret;
125 }
126  
127 sub set {
128 0 ▶ my ($self, $key, $value, $expire, $callback) = @_;
Subroutine set: not called CC=4 SCAR=30.0
Pod documented
129  
130 0 ▶ die 'set() requires key and value arguments' unless $key && $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $value 0 0 0
Condition0%0/3
$key and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $value
$key $value
Truth table0%0/3
A and B
A= $keyB= $value
ABresult
0X0
100
111
131  
132 0 my $response = $self->_set($key, $value, $expire, $callback);
133  
134 # return now as the callabck will be called when it's done processing
135 0 ▶ return if ($self->{nowait});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nowait"} 0 0 0
136 0 $self->{redis}->wait_all_responses;
137 0 return $response;
138 }
139  
140 sub set_multi {
141 0 ▶ my ($self, @items) = @_;
Subroutine set_multi: not called CC=3 SCAR=24.8
Pod documented
142  
143 0 for my $item ( @items ) {
144 0 $self->_set(@$item);
145 }
146 0 ▶ $self->{redis}->wait_all_responses unless $self->{nowait};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"nowait"} 0 0 0
147 }
148  
149 sub _set {
150 0 ▶ my ($self, $key, $value, $expire, $callback) = @_;
Subroutine _set: not called CC=4 SCAR=30.0
151 0 $key = $self->{namespace} . $key;
152 0 ▶ $expire ||= $self->{default_expires_in};
Condition0%0/3
$expire ||= $self->{"default_expires_in"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$expire ||= $self->{"default_expires_in"}
$expire $self->{"default_expires_in"}
Truth table0%0/3
A ||= B
A= $expireB= $self->{"default_expires_in"}
ABresult
1X1
011
000
153  
154 0 ▶ if ($self->{nowait} && $callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'nowait'} and $callback) { } 0 0 0
Condition0%0/3
$self->{"nowait"} and $callback
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"nowait"} and $callback
$self->{"nowait"} $callback
Truth table0%0/3
A and B
A= $self->{"nowait"}B= $callback
ABresult
0X0
100
111
155 0 $self->{redis}->setex($key, $expire, $self->{serialize}->($value), $callback);
156 }
157 else {
158 0 $self->{redis}->setex($key, $expire, $self->{serialize}->($value));
159 }
160 }
161  
162 sub get_or_set {
163 0 ▶ my ($self, $key, $code, $expire) = @_;
Subroutine get_or_set: not called CC=2 SCAR=17.9
Pod documented
164  
165 0 my $data = $self->get($key);
166 0 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 0 0
167 0 $data = $code->();
168 0 $self->set($key, $data, $expire);
169 }
170 0 $data;
171 }
172  
173 sub remove {
174 0 ▶ my ($self, $key) = @_;
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
175  
176 0 my $data = $self->get($key);
177 0 $key = $self->{namespace} . $key;
178 0 $self->{redis}->del($key);
179  
180 0 $data;
181 }
182  
183 sub nowait_push {
184 0 ▶ shift->{redis}->wait_all_responses;
Subroutine nowait_push: not called CC=1 SCAR=6.9
Pod documented
185 }
186  
187 13 1;
188 __END__
↑ Summary Build »