| 1 | package Tie::Hash::RedisDB; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.03'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use RedisDB; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | my $json = JSON->new->utf8->allow_nonref; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | ▶ | sub TIEHASH { | ||||||||||||||||||
|
Subroutine
TIEHASH: not called CC=7 SCAR=40.3
| |||||||||||||||||||||
| 16 | 0 | my ($self, $addr, $args) = @_; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # Don't want to be crazy strict, but at least something which implies they know how this works. | ||||||||||||||||||||
| 19 | 0 | my $whatsit = reftype $args; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | croak 'Must supply a lookup element' unless defined $addr; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 0 | ▶ | croak 'Arguments must be supplied as a hash reference.' | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$whatsit // ""
MC/DC0%0/1
$whatsit // ""
$whatsit
Truth table0%0/2
A
A= $whatsit
| |||||||||||||||||||||
| 23 | unless ($whatsit // '') eq 'HASH'; | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # All easy server definition for Cache::RedisDB users | ||||||||||||||||||||
| 26 | 0 | ▶ | my $ruri = $args->{redis_uri} // $ENV{REDIS_CACHE_SERVER}; | ||||||||||||||||||
|
Condition0%0/3
$args->{"redis_uri"} // $ENV{"REDIS_CACHE_SERVER"}
MC/DC0%0/2
$args->{"redis_uri"} // $ENV{"REDIS_CACHE_SERVER"}
$args->{"redis_uri"}
$ENV{"REDIS_CACHE_SERVER"}
Truth table0%0/3
A // B
A= $args->{"redis_uri"}B= $ENV{"REDIS_CACHE_SERVER"}
| |||||||||||||||||||||
| 27 | 0 | ▶ | croak 'Must supply a redis_redis' unless $ruri; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | my $node = { | ||||||||||||||||||||
| 30 | EXP_SECONDS => $args->{expiry}, | ||||||||||||||||||||
| 31 | DEL_ON_UNTIE => 0, | ||||||||||||||||||||
| 32 | 0 | ▶ | WHERE => join(chr(2), ($args->{namespace} // "THRDB"), $addr), | ||||||||||||||||||
|
Condition0%0/2
$args->{"namespace"} // "THRDB"
MC/DC0%0/1
$args->{"namespace"} // "THRDB"
$args->{"namespace"}
Truth table0%0/2
A
A= $args->{"namespace"}
| |||||||||||||||||||||
| 33 | REDIS => RedisDB->new(url => $ruri), | ||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | return bless $node, $self; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub FETCH { | ||||||||||||||||||
|
Subroutine
FETCH: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 41 | 0 | my ($self, $key) = @_; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | my $val = $self->{REDIS}->hget($self->{WHERE}, $key); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | return $val && $json->decode($val); | ||||||||||||||||||
|
Condition0%0/3
$val && $json->decode($val)
MC/DC0%0/2
$val && $json->decode($val)
$val
$json->decode($val)
Truth table0%0/3
A && B
A= $valB= $json->decode($val)
| |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | sub STORE { | ||||||||||||||||||
|
Subroutine
STORE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 49 | 0 | my ($self, $key, $val) = @_; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | my $redis = $self->{REDIS}; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | $redis->hset($self->{WHERE}, $key, $json->encode($val)); | |||||||||||||||||||
| 54 | 0 | ▶ | if (my $expiry = $self->{EXP_SECONDS}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | $redis->expire($self->{WHERE}, $expiry); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | return 1; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | sub DELETE { | ||||||||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 62 | 0 | my ($self, $key) = @_; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | return $self->{REDIS}->hdel($self->{WHERE}, $key); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub CLEAR { | ||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 68 | 0 | my ($self) = @_; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | return $self->{REDIS}->del($self->{WHERE}); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | sub EXISTS { | ||||||||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 74 | 0 | my ($self, $key) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | return $self->{REDIS}->hexists($self->{WHERE}, $key); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | ▶ | sub FIRSTKEY { | ||||||||||||||||||
|
Subroutine
FIRSTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 80 | 0 | my ($self) = @_; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | $self->{_keys} = $self->{REDIS}->hkeys($self->{WHERE}); | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | return $self->NEXTKEY; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub NEXTKEY { | ||||||||||||||||||
|
Subroutine
NEXTKEY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 88 | 0 | my ($self) = @_; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | return shift @{$self->{_keys}}; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub UNTIE { | ||||||||||||||||||
|
Subroutine
UNTIE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 94 | 0 | my ($self) = @_; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | return $self->{DEL_ON_UNTIE} ? $self->CLEAR : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 100 | 0 | my ($self) = @_; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | return $self->UNTIE; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | ▶ | sub delete { | ||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 106 | 0 | my ($self) = @_; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | return $self->{DEL_ON_UNTIE} = 1; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||