line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Redis::Script; |
2
|
3
|
|
|
3
|
|
162791
|
use 5.008005; |
|
3
|
|
|
|
|
6
|
|
3
|
3
|
|
|
3
|
|
9
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
58
|
|
4
|
3
|
|
|
3
|
|
9
|
use warnings; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
75
|
|
5
|
3
|
|
|
3
|
|
9
|
use Exporter 'import'; |
|
3
|
|
|
|
|
1
|
|
|
3
|
|
|
|
|
145
|
|
6
|
|
|
|
|
|
|
our @EXPORT_OK = ('redis_eval'); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
1379
|
use Digest::SHA qw(sha1_hex); |
|
3
|
|
|
|
|
7554
|
|
|
3
|
|
|
|
|
231
|
|
11
|
3
|
|
|
3
|
|
16
|
use Carp qw/croak/; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
1084
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
0
|
|
my ($class, %args) = @_; |
15
|
0
|
|
|
|
|
|
my $self = bless { |
16
|
|
|
|
|
|
|
use_evalsha => 1, |
17
|
|
|
|
|
|
|
%args, |
18
|
|
|
|
|
|
|
}, $class; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub eval { |
24
|
0
|
|
|
0
|
1
|
|
my ($self, $redis, $keys, $args) = @_; |
25
|
0
|
0
|
|
|
|
|
if ($self->{use_evalsha}) { |
26
|
0
|
|
|
|
|
|
my $sha = $self->sha1; |
27
|
0
|
|
|
|
|
|
my $ret = eval { $redis->evalsha($sha, scalar(@$keys), @$keys, @$args) }; |
|
0
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
if (my $err = $@) { |
29
|
0
|
0
|
|
|
|
|
croak $err if $err !~ /\[evalsha\] NOSCRIPT No matching script/i; |
30
|
|
|
|
|
|
|
} else { |
31
|
0
|
0
|
0
|
|
|
|
return (wantarray && ref $ret eq 'ARRAY') ? @$ret : $ret; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
my $ret = eval { |
36
|
0
|
|
|
|
|
|
$redis->eval($self->{script}, scalar(@$keys), @$keys, @$args); |
37
|
|
|
|
|
|
|
}; |
38
|
0
|
0
|
|
|
|
|
if (my $err = $@) { |
39
|
0
|
|
|
|
|
|
croak $@; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
0
|
0
|
|
|
|
return (wantarray && ref $ret eq 'ARRAY') ? @$ret : $ret; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub exists { |
46
|
0
|
|
|
0
|
1
|
|
my ($self, $redis) = @_; |
47
|
0
|
|
|
|
|
|
return $redis->script_exists($self->sha1)->[0]; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub load { |
51
|
0
|
|
|
0
|
1
|
|
my ($self, $redis) = @_; |
52
|
0
|
|
|
|
|
|
my $sha = $self->sha1; |
53
|
0
|
|
|
|
|
|
my $redis_sha = $redis->script_load($self->{script}); |
54
|
0
|
0
|
|
|
|
|
if (lc $sha ne lc $redis_sha) { |
55
|
0
|
|
|
|
|
|
croak "SHA is unmatch (expected $sha but redis returns $redis_sha)"; |
56
|
|
|
|
|
|
|
} |
57
|
0
|
|
|
|
|
|
return $sha; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub sha1 { |
61
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
62
|
0
|
|
0
|
|
|
|
return $self->{sha} ||= sha1_hex($self->{script}); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub redis_eval { |
66
|
0
|
|
|
0
|
0
|
|
my ($redis, $script, $keys, $args) = @_; |
67
|
0
|
|
|
|
|
|
return __PACKAGE__->new(script => $script)->eval($redis, $keys, $args); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
__END__ |