line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tie::Hash::RedisDB; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
186859
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.03'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
8
|
1
|
|
|
1
|
|
494
|
use JSON; |
|
1
|
|
|
|
|
8020
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
94
|
use Scalar::Util qw(reftype); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
10
|
1
|
|
|
1
|
|
13
|
use RedisDB; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
11
|
1
|
|
|
1
|
|
4
|
use Try::Tiny; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
434
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $json = JSON->new->utf8->allow_nonref; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub TIEHASH { |
16
|
0
|
|
|
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
|
0
|
|
|
|
|
croak 'Must supply a lookup element' unless defined $addr; |
22
|
0
|
0
|
0
|
|
|
|
croak 'Arguments must be supplied as a hash reference.' |
23
|
|
|
|
|
|
|
unless ($whatsit // '') eq 'HASH'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# All easy server definition for Cache::RedisDB users |
26
|
0
|
|
0
|
|
|
|
my $ruri = $args->{redis_uri} // $ENV{REDIS_CACHE_SERVER}; |
27
|
0
|
0
|
|
|
|
|
croak 'Must supply a redis_redis' unless $ruri; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $node = { |
30
|
|
|
|
|
|
|
EXP_SECONDS => $args->{expiry}, |
31
|
|
|
|
|
|
|
DEL_ON_UNTIE => 0, |
32
|
0
|
|
0
|
|
|
|
WHERE => join(chr(2), ($args->{namespace} // "THRDB"), $addr), |
33
|
|
|
|
|
|
|
REDIS => RedisDB->new(url => $ruri), |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
return bless $node, $self; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub FETCH { |
41
|
0
|
|
|
0
|
|
|
my ($self, $key) = @_; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $val = $self->{REDIS}->hget($self->{WHERE}, $key); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
0
|
|
|
|
return $val && $json->decode($val); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub STORE { |
49
|
0
|
|
|
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
|
0
|
|
|
|
|
if (my $expiry = $self->{EXP_SECONDS}) { |
55
|
0
|
|
|
|
|
|
$redis->expire($self->{WHERE}, $expiry); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return 1; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub DELETE { |
62
|
0
|
|
|
0
|
|
|
my ($self, $key) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $self->{REDIS}->hdel($self->{WHERE}, $key); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub CLEAR { |
68
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return $self->{REDIS}->del($self->{WHERE}); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub EXISTS { |
74
|
0
|
|
|
0
|
|
|
my ($self, $key) = @_; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
return $self->{REDIS}->hexists($self->{WHERE}, $key); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub FIRSTKEY { |
80
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
$self->{_keys} = $self->{REDIS}->hkeys($self->{WHERE}); |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
return $self->NEXTKEY; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub NEXTKEY { |
88
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return shift @{$self->{_keys}}; |
|
0
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub UNTIE { |
94
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
95
|
|
|
|
|
|
|
|
96
|
0
|
0
|
|
|
|
|
return $self->{DEL_ON_UNTIE} ? $self->CLEAR : 1; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub DESTROY { |
100
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
return $self->UNTIE; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub delete { |
106
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
return $self->{DEL_ON_UNTIE} = 1; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
__END__ |