Redis.pm

filter: Statementstmt 96.1% 101 / 105 Branchbran 60.0% 18 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.0% 18 / 20 Podpod 0.0% 0 / 8 totaltotal 81.4% 140 / 172 CC 23 SCAR 11.4
CC
23
Cov
87%
_build_redis
5.9
fetch_multi_hashref
5.0
get_keys
4.1
CRAP 3.1
↑ Summary Redis/t/CHIDriverTests.pm »
1 package CHI::Driver::Redis;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use Check::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI::Escape qw(uri_escape uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 extends 'CHI::Driver';
10  
11 1 our $VERSION = '0.08';
12  
13 1 has 'redis' => (
14 is => 'rw',
15 );
16  
17 1 has '_params' => (
18 is => 'rw'
19 );
20  
21 1 has 'prefix'=> (
22 is => 'ro',
23 default => '',
24 );
25  
26 sub BUILD {
27 143 ▶ my ($self, $params) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
28 143 $self->_params($params);
29 }
30  
31 sub _build_redis {
32 2685 ▶ my ($self) = @_;
Subroutine _build_redis: called CC=5 SCAR=17.8
33  
34 2685 my $params = $self->_params;
35  
36 2685 ▶ my $redis_class = $params->{redis_class} || 'Redis';
Condition50%1/2
$params->{'redis_class'} || 'Redis'
l!l
26850
MC/DC0%0/1
$params->{'redis_class'} || 'Redis'
$params->{'redis_class'}
Truth table50%1/2
A
A= $params->{'redis_class'}
Aresult
11
00
37 return $redis_class->new(
38 server => $params->{server} || '127.0.0.1:6379',
39 debug => $params->{debug} || 0,
40 encoding => undef,
41 2685 ▶ (defined $params->{password} ? ( password => $params->{password} ) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'password'} ? : 0 2685 2685
Condition50%2/4
$params->{'server'} || '127.0.0.1:6379'
l!l
02685
$params->{'debug'} || 0
l!l
02685
MC/DC0%0/1
$params->{'server'} || '127.0.0.1:6379'
$params->{'server'}
MC/DC0%0/1
$params->{'debug'} || 0
$params->{'debug'}
Truth table50%1/2
A
A= $params->{'server'}
Aresult
11
00
Truth table50%1/2
A
A= $params->{'debug'}
Aresult
11
00
42 );
43 }
44  
45 sub fetch {
46 1719 ▶ my ($self, $key) = @_;
Subroutine fetch: called CC=2 SCAR=7.0
Pod undocumented
47  
48 1719 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 1719 1719
49  
50 1719 my $eskey = uri_escape($key);
51 1719 my $realkey = $self->prefix . $self->namespace . '||' . $eskey;
52 1719 my $val = $self->redis->get($realkey);
53 1719 return $val;
54 }
55  
56 sub fetch_multi_hashref {
57 11 ▶ my ($self, $keys) = @_;
Subroutine fetch_multi_hashref: called CC=5 SCAR=16.1
Pod undocumented
58  
59 11 ▶ return unless scalar(@{ $keys });
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$keys;} 2 9 11
60  
61 9 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 9 9
62  
63 9 my $ns = $self->prefix . $self->namespace;
64  
65 9 my @keys;
66 9 foreach my $k (@$keys) {
67 61 my $esk = uri_escape($k);
68 61 my $key = $ns . '||' . $esk;
69 61 push @keys, $key;
70 }
71  
72 9 my @vals = $self->redis->mget(@keys);
73  
74 9 my $count = 0;
75 9 my %resp;
76 9 foreach my $k (@$keys) {
77 61 $resp{$k} = $vals[$count];
78 61 $count++;
79 }
80  
81 9 return \%resp;
82 }
83  
84 sub get_keys {
85 121 ▶ my ($self) = @_;
Subroutine get_keys: called CC=4 SCAR=14.0
Pod undocumented
86  
87 121 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 121 121
88  
89 121 my @keys = $self->redis->smembers($self->prefix . $self->namespace);
90  
91 121 my @unesckeys = ();
92  
93 121 foreach my $k (@keys) {
94 # Getting an empty key here for some reason...
95 606 ▶ next unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 606 606
96 606 push(@unesckeys, uri_unescape($k));
97 }
98 121 return @unesckeys;
99 }
100  
101 sub get_namespaces {
102 11 ▶ my ($self) = @_;
Subroutine get_namespaces: called CC=2 SCAR=7.1
Pod undocumented
103  
104 11 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 11 11
105  
106 11 return $self->redis->smembers($self->prefix . 'chinamespaces');
107 }
108  
109 sub remove {
110 125 ▶ my ($self, $key) = @_;
Subroutine remove: called CC=3 SCAR=11.2
Pod undocumented
111  
112 125 ▶ return unless defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 125 125
113  
114 125 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 125 125
115  
116 125 my $ns = $self->prefix . $self->namespace;
117  
118 125 my $skey = uri_escape($key);
119  
120 125 $self->redis->srem($ns, $skey);
121 125 $self->redis->del($ns . '||' . $skey);
122 }
123  
124 sub store {
125 544 ▶ my ($self, $key, $data, $expires_in) = @_;
Subroutine store: called CC=3 SCAR=11.0
Pod undocumented
126  
127 544 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 544 544
128  
129 544 my $ns = $self->prefix . $self->namespace;
130  
131 544 my $skey = uri_escape($key);
132 544 my $realkey = $ns . '||' . $skey;
133  
134 544 $self->redis->sadd($self->prefix . 'chinamespaces', $self->namespace);
135 544 $self->redis->sadd($ns, $skey);
136 544 $self->redis->set($realkey, $data);
137  
138 544 ▶ if (defined($expires_in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expires_in) 2 542 544
139 2 $self->redis->expire($realkey, $expires_in);
140 }
141 }
142  
143 sub clear {
144 108 ▶ my ($self) = @_;
Subroutine clear: called CC=3 SCAR=11.0
Pod undocumented
145  
146 108 ▶ return unless $self->_verify_redis_connection;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_verify_redis_connection 0 108 108
147  
148 108 my $ns = $self->prefix . $self->namespace;
149 108 my @keys = $self->redis->smembers($ns);
150  
151 108 foreach my $k (@keys) {
152 88 $self->redis->srem($ns, $k);
153 88 $self->redis->del($ns . '||' . $k);
154 }
155 }
156  
157 sub _verify_redis_connection {
158 2685 ▶ my ($self) = @_;
Subroutine _verify_redis_connection: called CC=1 SCAR=0.2
159  
160 2685 my $success = 0;
161 try {
162 2685 ▶ if(defined($self->redis)) {
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->redis) 2547 138 2685
163 2547 ▶ if($self->redis->ping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->redis->ping) 2547 0 2547
164 2547 $success = 1;
165 2547 return;
166 }
167 # Bitch if the ping fails
168 0 die "Ping failed.";
169 }
170 } catch {
171 0 ▶ warn "Error pinging redis, attempting to reconnect.\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 2685 };
173  
174 try {
175 2685 ▶ my $redis = $self->_build_redis();
Subroutine __ANON__: called CC=2 SCAR=7.4
176 2685 ▶ if (defined $redis) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redis) { } 2685 0 2685
177 # We apparently connected, success!
178 2685 $self->redis($redis);
179 2685 $success = 1;
180 } else {
181 0 die('Failed to connect to Redis');
182 }
183 } catch {
184 0 ▶ warn "Unable to connect to Redis: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
185 2685 };
186  
187 # Return the success of failure of the verification
188 2685 return $success;
189 }
190  
191 1 __PACKAGE__->meta->make_immutable;
192  
193 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
194  
195 __END__
196  
197 =head1 NAME
198  
199 CHI::Driver::Redis - Redis driver for CHI
200  
201 =head1 SYNOPSIS
202  
203 use CHI;
204  
205 my $foo = CHI->new(
206 driver => 'Redis',
207 namespace => 'foo',
208 server => '127.0.0.1:6379',
209 debug => 0
210 );
211  
212 =head1 DESCRIPTION
213  
214 A CHI driver that uses C<Redis> to store the data. Care has been taken to
215 not have this module fail in fiery ways if the cache is unavailable. It is my
216 hope that if it is failing and the cache is not required for your work, you
217 can ignore it's warnings.
218  
219 =head1 TECHNICAL DETAILS
220  
221 =head2 Namespaces.
222  
223 Redis does not have namespaces. Therefore, we have to do some hoop-jumping.
224  
225 Namespaces are tracked in a set named C<chinamespaces>. This is a list of all
226 the namespaces the driver has seen.
227  
228 Keys in a namespace are stored in a set that shares the name of the namespace.
229 The actual value is stored as "$namespace||key".
230  
231 =head2 Encoding
232  
233 This CHI driver uses Redis.pm. Redis.pm by default automatically
234 encodes values to UTF-8. This driver sets the Redis encoding option
235 to undef to disable automatic encoding.
236  
237 =back
238  
239 =head1 CONSTRUCTOR OPTIONS
240  
241 C<server>, C<debug>, and C<password> are passed to C<Redis>.
242  
243 =head1 ATTRIBUTES
244  
245 =head2 redis
246  
247 Contains the underlying C<Redis> object.
248  
249 =head1 AUTHOR
250  
251 Cory G Watson, C<< <gphat at cpan.org> >>
252  
253 =head1 CONTRIBUTORS
254  
255 Ian Burrell, C<< <iburrell@cpan.org> >>
256  
257 =head1 COPYRIGHT & LICENSE
258  
259 Copyright 2009 Cold Hard Code, LLC.
260  
261 This program is free software; you can redistribute it and/or modify it
262 under the terms of either: the GNU General Public License as published
263 by the Free Software Foundation; or the Artistic License.
264  
265 See http://dev.perl.org/licenses/ for more information.
↑ Summary Redis/t/CHIDriverTests.pm »