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