blib/lib/Cache/RedisDB.pm

filter: Statementstmt 38.8% 26 / 67 Branchbran 8.3% 1 / 12 Conditioncond 8.3% 2 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 52.6% 10 / 19 Podpod 100% 11 / 11 totaltotal 34.0% 50 / 147 CC 16 SCAR 27.2
CC
16
Cov
28%
set
56.0
mget
12.0
_cache_key
12.0
CRAP 15.3
↑ 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;
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.13';
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 METHODS
34  
35 =head2 redis_uri
36  
37 Returns a C<< redis:// >> redis URI which will be used for the initial Redis connection.
38  
39 This will default to localhost on the standard port, and can be overridden with the
40 C<REDIS_CACHE_SERVER> environment variable.
41  
42 =cut
43  
44 2 ▶sub redis_uri {
Subroutine redis_uri: called CC=3 SCAR=11.7
Pod documented
45  
46 2 ▶ my $redis_uri = $ENV{REDIS_CACHE_SERVER} // 'redis://127.0.0.1';
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
47  
48 # Probably a legacy TCP host:port
49 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
50  
51 2 return $redis_uri;
52 }
53  
54 =head2 redis_connection
55  
56 Creates new connection to a Redis server and returns the corresponding L<RedisDB> object.
57  
58 =cut
59  
60 1 ▶sub redis_connection {
Subroutine redis_connection: called CC=1 SCAR=0.0
Pod documented
61 return RedisDB->new(
62 url => redis_uri(),
63 reconnect_attempts => 3,
64 1 ▶ on_connect_error => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 confess "Cannot connect: " . redis_uri();
66 1 });
67 }
68  
69 =head2 redis
70  
71 Returns a singleton L<RedisDB> instance.
72  
73 =cut
74  
75 1 ▶sub redis {
Subroutine redis: called CC=2 SCAR=9.2
Pod documented
76 1 state $redis;
77 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
78 0 return $redis;
79 }
80  
81 =head2 get
82  
83 Takes a C<$namespace> and C<$key> parameter, and returns the scalar value
84 corresponding to that cache entry.
85  
86 This will automatically deserialise data stored with L<Sereal>. If no data
87 is found, this will return C<undef>.
88  
89 =cut
90  
91 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
92 0 my ($self, $namespace, $key) = @_;
93 0 my $res = redis()->get(_cache_key($namespace, $key));
94 0 ▶ if (looks_like_sereal($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_sereal($res)) 0 0 0
95 0 state $decoder = Sereal::Decoder->new();
96 0 $res = $decoder->decode($res);
97 }
98 0 return $res;
99 }
100  
101 =head2 mget
102  
103 Retrieve values for multiple keys in a single call.
104  
105 Similar to L</get>, this takes a C<$namespace> as the first parameter,
106 but it also accepts a list of C<@keys> to look up.
107  
108 Returns an arrayref in the same order as the original keys. For any
109 key that had no value, the resulting arrayref will contain C<undef>.
110  
111 =cut
112  
113 0 ▶sub mget {
Subroutine mget: not called CC=3 SCAR=24.8
Pod documented
114 0 my ($self, $namespace, @keys) = @_;
115 0 my $res = Cache::RedisDB::redis()->mget(map { Cache::RedisDB::_cache_key($namespace, $_) } @keys);
116 0 state $decoder = Sereal::Decoder->new();
117 0 ▶ return [map { ($_ && looks_like_sereal($_)) ? $decoder->decode($_) : $_ } @$res];
Branch: 1 branch
BranchTrueFalseTotal
$_ && looks_like_sereal($_) ? : 0 0 0
Condition0%0/3
$_ and looks_like_sereal($_)
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and looks_like_sereal($_)
$_ looks_like_sereal($_)
Truth table0%0/3
A and B
A= $_B= looks_like_sereal($_)
ABresult
0X0
100
111
118 }
119  
120 =head2 set
121  
122 Creates or updates a Redis key under C<$namespace>, C<$key> using the scalar C<$value>.
123 Also takes an optional C<$exptime> as expiration time in seconds.
124  
125 $redis->set($namespace, $key, $value);
126 $redis->set($namespace, $key, $value, $expiry_time);
127  
128 Can also be provided a callback which will be executed once the command completes.
129  
130 =cut
131  
132 0 ▶sub set {
Subroutine set: not called CC=7 SCAR=40.3
Pod documented
133 0 my ($self, $namespace, $key, $value, $exptime, $callback) = @_;
134 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
135 0 state $encoder = Sereal::Encoder->new({
136 freeze_callbacks => 1,
137 });
138 0 $value = $encoder->encode($value);
139 }
140 0 my $cache_key = _cache_key($namespace, $key);
141 0 ▶ if (defined $exptime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exptime) { } 0 0 0
142 0 $exptime = int(1000 * $exptime);
143 # PX milliseconds -- Set the specified expire time, in milliseconds
144 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
145 } else {
146 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
147 }
148 }
149  
150 =head2 set_nw($namespace, $key, $value[, $exptime])
151  
152 Same as I<set> but do not wait confirmation from server. If the server returns
153 an error, it will be ignored.
154  
155 =cut
156  
157 0 ▶sub set_nw {
Subroutine set_nw: not called CC=1 SCAR=6.9
Pod documented
158 0 my ($self, $namespace, $key, $value, $exptime) = @_;
159 0 return $self->set($namespace, $key, $value, $exptime, RedisDB::IGNORE_REPLY);
160 }
161  
162 =head2 del($namespace, $key1[, $key2, ...])
163  
164 Delete given keys and associated values from the cache. I<$namespace> is common for all keys.
165 Returns number of deleted keys.
166  
167 =cut
168  
169 0 ▶sub del {
Subroutine del: not called CC=1 SCAR=6.9
Pod documented
170 0 my ($self, $namespace, @keys) = @_;
171 0 return redis->del(map { _cache_key($namespace, $_) } @keys);
172 }
173  
174 =head2 keys($namespace)
175  
176 Return an arrayref of all known keys in the provided C<$namespace>.
177  
178 =cut
179  
180 0 ▶sub keys : method { ## no critic (ProhibitBuiltinHomonyms)
Subroutine keys: not called CC=1 SCAR=6.9
Pod documented
181 0 my ($self, $namespace) = @_;
182 0 my $prefix = _cache_key($namespace, undef);
183 0 my $pl = length($prefix);
184 0 return [map { substr($_, $pl) } @{redis()->keys($prefix . '*')}];
185 }
186  
187 =head2 ttl($namespace, $key)
188  
189 Return the Time To Live (in seconds) of a key in the provided I<$namespace>.
190  
191 =cut
192  
193 0 ▶sub ttl {
Subroutine ttl: not called CC=2 SCAR=17.9
Pod documented
194 0 my ($self, $namespace, $key) = @_;
195  
196 0 my $ms = redis()->pttl(_cache_key($namespace, $key));
197 # We pessimistically round to the start of the second where it
198 # will disappear. While slightly wrong, it is likely less confusing.
199 # Nonexistent (or already expired) keys should return 0;
200 0 ▶ return ($ms <= 0) ? 0 : int($ms / 1000);
Branch: 1 branch
BranchTrueFalseTotal
$ms <= 0 ? : 0 0 0
201 }
202  
203 0 ▶sub _cache_key {
Subroutine _cache_key: not called CC=3 SCAR=24.8
204 0 my ($namespace, $key) = @_;
205 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
206 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
207  
208 0 return $namespace . '::' . $key;
209 }
210  
211 =head3 flushall
212  
213 Delete all keys and associated values from the cache.
214  
215 =cut
216  
217 0 ▶sub flushall {
Subroutine flushall: not called CC=1 SCAR=6.9
Pod documented
218 0 return redis()->flushall();
219 }
220  
221 =head1 AUTHOR
222  
223 binary.com, C<< <perl at binary.com> >>
224  
225 =head1 BUGS
226  
227 Please report any bugs or feature requests to C<bug-cache-redisdb at rt.cpan.org>, or through
228 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Cache-RedisDB>. I will be notified, and then you'll
229 automatically be notified of progress on your bug as I make changes.
230  
231 =head1 SUPPORT
232  
233 You can find documentation for this module with the perldoc command.
234  
235 perldoc Cache::RedisDB
236  
237 You can also look for information at:
238  
239 =over 4
240  
241 =item * RT: CPAN's request tracker (report bugs here)
242  
243 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Cache-RedisDB>
244  
245 =item * AnnoCPAN: Annotated CPAN documentation
246  
247 L<http://annocpan.org/dist/Cache-RedisDB>
248  
249 =item * CPAN Ratings
250  
251 L<http://cpanratings.perl.org/d/Cache-RedisDB>
252  
253 =item * Search CPAN
254  
255 L<http://search.cpan.org/dist/Cache-RedisDB/>
256  
257 =back
258  
259 =cut
260  
261 3 1; # End of Cache::RedisDB
↑ Summary