blib/lib/Cache/RedisDB.pm

filter: Statementstmt 42.6% 26 / 61 Branchbran 10.0% 1 / 10 Conditioncond 9.5% 2 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 55.5% 10 / 18 Podpod 100% 10 / 10 totaltotal 37.1% 49 / 132 CC 14 SCAR 27.5
CC
14
Cov
32%
set
56.0
_cache_key
12.0
get
6.0
CRAP 15.6
↑ Summary
1 package Cache::RedisDB;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use RedisDB 2.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Sereal qw(looks_like_sereal);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Cache::RedisDB - RedisDB based cache system
13  
14 =head1 DESCRIPTION
15  
16 This is just a wrapper around RedisDB to have a single Redis object and connection per process. By default uses server redis://127.0.0.1, but it may be overwritten by REDIS_CACHE_SERVER environment variable. It transparently handles forks.
17  
18 =head1 COMPATIBILITY AND REQUIREMENTS
19  
20 Redis 2.6.12 and higher strongly recommended. Required if you want to use
21 extended options in ->set().
22  
23 =cut
24  
25 3 our $VERSION = '0.12';
26  
27 =head1 SYNOPSIS
28  
29 use Cache::RedisDB;
30 Cache::RedisDB->set("namespace", "key", "value");
31 Cache::RedisDB->get("namespace", "key");
32  
33 =head1 SUBROUTINES/METHODS
34  
35 =head2 redis_uri
36  
37 Returns redis uri
38  
39 =cut
40  
41 sub redis_uri {
42  
43 2 ▶ my $redis_uri = $ENV{REDIS_CACHE_SERVER} // 'redis://127.0.0.1';
Subroutine redis_uri: called CC=3 SCAR=11.7
Pod documented
Condition50%1/2
$ENV{"REDIS_CACHE_SERVER"} // "redis://127.0.0.1"
l!l
20
MC/DC0%0/1
$ENV{"REDIS_CACHE_SERVER"} // "redis://127.0.0.1"
$ENV{"REDIS_CACHE_SERVER"}
Truth table50%1/2
A
A= $ENV{"REDIS_CACHE_SERVER"}
Aresult
11
00
44  
45 # Probably a legacy TCP host:port
46 2 ▶ $redis_uri = 'redis://' . $redis_uri if ($redis_uri =~ m#^[^/]+:[0-9]{1,5}$#);
Branch: 1 branch
BranchTrueFalseTotal
if $redis_uri =~ m[^[^/]+:[0-9]{1,5}$] 2 0 2
47  
48 2 return $redis_uri;
49 }
50  
51 =head2 redis_connection
52  
53 Creates new connection to redis-server and returns corresponding RedisDB object.
54  
55 =cut
56  
57 sub redis_connection {
58 return RedisDB->new(
59 url => redis_uri(),
60 reconnect_attempts => 3,
61 on_connect_error => sub {
62 1 ▶ confess "Cannot connect: " . redis_uri();
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1 ▶ });
Subroutine redis_connection: called CC=1 SCAR=0.0
Pod documented
64 }
65  
66 =head2 redis
67  
68 Returns RedisDB object connected to the correct redis server.
69  
70 =cut
71  
72 sub redis {
73 1 ▶ state $redis;
Subroutine redis: called CC=2 SCAR=9.2
Pod documented
74 1 ▶ $redis //= redis_connection();
Condition33%1/3
$redis //= redis_connection()
l!l&&r!l&&!r
001
MC/DC0%0/2
$redis //= redis_connection()
$redis redis_connection()
Truth table33%1/3
A //= B
A= $redisB= redis_connection()
ABresult
1X1
011
000
75 0 return $redis;
76 }
77  
78 =head2 get($namespace, $key)
79  
80 Retrieve I<$key> value from the cache.
81  
82 =cut
83  
84 sub get {
85 0 ▶ my ($self, $namespace, $key) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
86 0 my $res = redis->get(_cache_key($namespace, $key));
87 0 ▶ if (looks_like_sereal($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_sereal($res)) 0 0 0
88 0 state $decoder = Sereal::Decoder->new();
89 0 $res = $decoder->decode($res);
90 }
91 0 return $res;
92 }
93  
94 =head2 set($namespace, $key, $value[, $exptime])
95  
96 Assigns I<$value> to the I<$key>. I<$value> should be scalar value.
97 If I<$exptime> specified, it is expiration time in seconds.
98  
99 =cut
100  
101 sub set {
102 0 ▶ my ($self, $namespace, $key, $value, $exptime, $callback) = @_;
Subroutine set: not called CC=7 SCAR=40.3
Pod documented
103 0 ▶ if (not defined $value or ref $value or Encode::is_utf8($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value or &Encode::is_utf8($value)) 0 0 0
Condition0%0/6
not defined $value or ref $value or &Encode::is_utf8($value)
l!l&&r!l&&!r
000
not defined $value or ref $value
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $value or ref $value or &Encode::is_utf8($value)
not defined $value ref $value &Encode::is_utf8($value)
Truth table0%0/4
not(A) or B or C
A= not defined $valueB= ref $valueC= &Encode::is_utf8($value)
ABCresult
1XX1
01X1
0011
0000
104 0 state $encoder = Sereal::Encoder->new({
105 freeze_callbacks => 1,
106 });
107 0 $value = $encoder->encode($value);
108 }
109 0 my $cache_key = _cache_key($namespace, $key);
110 0 ▶ if (defined $exptime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exptime) { } 0 0 0
111 0 $exptime = int(1000 * $exptime);
112 # PX milliseconds -- Set the specified expire time, in milliseconds
113 0 ▶ return redis->set($cache_key, $value, "PX", $exptime, $callback // ());
Condition0%0/3
$callback // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$callback // ()
$callback ()
Truth table0%0/3
A // B
A= $callbackB= ()
ABresult
1X1
011
000
114 } else {
115 0 ▶ return redis->set($cache_key, $value, $callback // ());
Condition0%0/3
$callback // ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$callback // ()
$callback ()
Truth table0%0/3
A // B
A= $callbackB= ()
ABresult
1X1
011
000
116 }
117 }
118  
119 =head2 set_nw($namespace, $key, $value[, $exptime])
120  
121 Same as I<set> but do not wait confirmation from server. If server will return
122 error, there's no way to catch it.
123  
124 =cut
125  
126 sub set_nw {
127 0 ▶ my ($self, $namespace, $key, $value, $exptime) = @_;
Subroutine set_nw: not called CC=1 SCAR=6.9
Pod documented
128 0 return $self->set($namespace, $key, $value, $exptime, RedisDB::IGNORE_REPLY);
129 }
130  
131 =head2 del($namespace, $key1[, $key2, ...])
132  
133 Delete given keys and associated values from the cache. I<$namespace> is common for all keys.
134 Returns number of deleted keys.
135  
136 =cut
137  
138 sub del {
139 0 ▶ my ($self, $namespace, @keys) = @_;
Subroutine del: not called CC=1 SCAR=6.9
Pod documented
140 0 return redis->del(map { _cache_key($namespace, $_) } @keys);
141 }
142  
143 =head2 keys($namespace)
144  
145 Return a list of all known keys in the provided I<$namespace>.
146  
147 =cut
148  
149 sub keys { ## no critic (ProhibitBuiltinHomonyms)
150 0 ▶ my ($self, $namespace) = @_;
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
151 0 my $prefix = _cache_key($namespace, undef);
152 0 my $pl = length($prefix);
153 0 return [map { substr($_, $pl) } @{redis->keys($prefix . '*')}];
154 }
155  
156 =head2 ttl($namespace, $key)
157  
158 Return the Time To Live (in seconds) of a key in the provided I<$namespace>.
159  
160 =cut
161  
162 sub ttl {
163 0 ▶ my ($self, $namespace, $key) = @_;
Subroutine ttl: not called CC=2 SCAR=17.9
Pod documented
164  
165 0 my $ms = redis->pttl(_cache_key($namespace, $key));
166 # We pessimistically round to the start of the second where it
167 # will disappear. While slightly wrong, it is likely less confusing.
168 # Nonexistent (or already expired) keys should return 0;
169 0 ▶ return ($ms <= 0) ? 0 : int($ms / 1000);
Branch: 1 branch
BranchTrueFalseTotal
$ms <= 0 ? : 0 0 0
170 }
171  
172 sub _cache_key {
173 0 ▶ my ($namespace, $key) = @_;
Subroutine _cache_key: not called CC=3 SCAR=24.8
174 0 ▶ $namespace //= '';
Condition0%0/2
$namespace //= ""
l!l
00
MC/DC0%0/1
$namespace //= ""
$namespace
Truth table0%0/2
A
A= $namespace
Aresult
11
00
175 0 ▶ $key //= '';
Condition0%0/2
$key //= ""
l!l
00
MC/DC0%0/1
$key //= ""
$key
Truth table0%0/2
A
A= $key
Aresult
11
00
176  
177 0 return $namespace . '::' . $key;
178 }
179  
180 =head3 flushall
181  
182 Delete all keys and associated values from the cache.
183  
184 =cut
185  
186 sub flushall {
187 0 ▶ return redis->flushall();
Subroutine flushall: not called CC=1 SCAR=6.9
Pod documented
188 }
189  
190 =head1 AUTHOR
191  
192 binary.com, C<< <rakesh at binary.com> >>
193  
194 =head1 BUGS
195  
196 Please report any bugs or feature requests to C<bug-cache-redisdb at rt.cpan.org>, or through
197 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Cache-RedisDB>. I will be notified, and then you'll
198 automatically be notified of progress on your bug as I make changes.
199  
200  
201  
202  
203 =head1 SUPPORT
204  
205 You can find documentation for this module with the perldoc command.
206  
207 perldoc Cache::RedisDB
208  
209  
210 You can also look for information at:
211  
212 =over 4
213  
214 =item * RT: CPAN's request tracker (report bugs here)
215  
216 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Cache-RedisDB>
217  
218 =item * AnnoCPAN: Annotated CPAN documentation
219  
220 L<http://annocpan.org/dist/Cache-RedisDB>
221  
222 =item * CPAN Ratings
223  
224 L<http://cpanratings.perl.org/d/Cache-RedisDB>
225  
226 =item * Search CPAN
227  
228 L<http://search.cpan.org/dist/Cache-RedisDB/>
229  
230 =back
231  
232 =cut
233  
234 3 1; # End of Cache::RedisDB
↑ Summary