Fast/Cache/Redis.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 69 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package DBIx::Fast::Cache::Redis;
2  
3 use v5.38;
4 use Object::Pad 0.807;
5 use Scalar::Util ();
6  
7 our $VERSION = '0.22';
8  
9 # A DBIx::Fast::Cache backend over an existing Redis client (Redis::Fast or
10 # Redis - anything with get/set/del/incr/scan): one Redis shared by every
11 # process, so cached results and tag invalidation are seen by all of them.
12  
13 class DBIx::Fast::Cache::Redis {
14 field $redis :param :reader; # a connected client object
15 field $prefix :param :reader = 'dbixfast:'; # namespace for every key
16 field $_codec;
17  
18 ADJUST {
19 # Redis::Fast and Redis dispatch commands through AUTOLOAD, so can()
20 # cannot vouch for get/set/del: require an object instead.
21 die "DBIx::Fast::Cache::Redis: redis must be a connected client object\n"
22 unless Scalar::Util::blessed($redis);
23 $_codec = eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->new->canonical->allow_nonref }
24 || do { require JSON::PP; JSON::PP->new->canonical->allow_nonref };
25 }
26  
27 # DBIx::Fast::Cache pushes its TTL down to set() for backends that say so.
28 method supports_ttl () { 1 }
29  
30 method get ($key) {
31 my $raw = $redis->get($prefix . $key);
32 return undef unless defined $raw;
33 # Tag versions are stored by INCR as plain integers.
34 return $raw if $raw =~ /\A-?\d+\z/;
35 return $_codec->decode($raw);
36 }
37  
38 method set ($key, $value, $ttl = undef) {
39 my $raw = ref $value ? $_codec->encode($value) : $value;
40 return $ttl ? $redis->set($prefix . $key, $raw, 'EX', int($ttl + 0.5) || 1)
41 : $redis->set($prefix . $key, $raw);
42 }
43  
44 method remove ($key) { $redis->del($prefix . $key) }
45  
46 method incr ($key) { $redis->incr($prefix . $key) }
47  
48 # Delete every key under the prefix (SCAN, never KEYS: no server stall).
49 method clear () {
50 my $cursor = 0;
51 do {
52 my ($next, $keys) = $redis->scan($cursor, MATCH => "$prefix*", COUNT => 500);
53 $redis->del(@$keys) if $keys && @$keys;
54 $cursor = $next;
55 } while ($cursor);
56 return;
57 }
58 }
59  
60 1;
61  
62 __END__