| 1 | package Redis::ScriptCache; | ||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 2 | ▶ | use 5.10.0; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||
| 8 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Spec qw( | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 10 | catdir | ||||||||||||||||||||||||
| 11 | splitdir | ||||||||||||||||||||||||
| 12 | 2 | ); | |||||||||||||||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 14 | 2 | ▶ | use Digest::SHA1 'sha1_hex'; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||
| 16 | 2 | ▶ | use Class::XSAccessor { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 17 | 2 | getters => [qw( | |||||||||||||||||||||||
| 18 | redis_conn | ||||||||||||||||||||||||
| 19 | script_dir | ||||||||||||||||||||||||
| 20 | _script_cache | ||||||||||||||||||||||||
| 21 | )], | ||||||||||||||||||||||||
| 22 | 2 | }; | |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||
| 24 | 0 | ▶ | sub new { | ||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 25 | 0 | my $class = shift; | |||||||||||||||||||||||
| 26 | 0 | my $self = bless { @_ }, $class; | |||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||
| 28 | # initialize the cache | ||||||||||||||||||||||||
| 29 | 0 | $self->{_script_cache} = {}; | |||||||||||||||||||||||
| 30 | # redis_conn is compulsory | ||||||||||||||||||||||||
| 31 | 0 | ▶ | $self->redis_conn | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 32 | or croak('Need Redis connection'); | ||||||||||||||||||||||||
| 33 | # canonicalize script_dir | ||||||||||||||||||||||||
| 34 | 0 | $self->_set_script_dir; | |||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||
| 36 | 0 | return $self; | |||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _set_script_dir { | ||||||||||||||||||||||
|
Subroutine
_set_script_dir: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||
| 40 | 0 | my( $self, $script_dir ) = @_; | |||||||||||||||||||||||
| 41 | 0 | ▶ | my $script_dir_to_set = $script_dir // $self->script_dir // undef; | ||||||||||||||||||||||
|
Condition0%0/5
($script_dir // $self->script_dir) // undef
$script_dir // $self->script_dir
MC/DC0%0/2
($script_dir // $self->script_dir) // undef
$script_dir
$self->script_dir
Truth table0%0/3
A // B
A= $script_dirB= $self->script_dir
| |||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->{script_dir} = File::Spec->catdir( File::Spec->splitdir( $script_dir_to_set ) ) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 43 | if $script_dir_to_set; | ||||||||||||||||||||||||
| 44 | 0 | return $self; | |||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||
| 47 | 0 | ▶ | sub register_all_scripts { | ||||||||||||||||||||||
|
Subroutine
register_all_scripts: not called CC=4 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||||||
| 49 | 0 | my %args = @_; | |||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||
| 51 | $self->_set_script_dir( $args{script_dir} ) | ||||||||||||||||||||||||
| 52 | 0 | ▶ | if $args{script_dir}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( $self->script_dir ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 55 | 0 | ▶ | for my $file (glob($self->script_dir . '/*.lua')) { | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||
| 56 | 0 | $self->register_file(basename($file)); | |||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||
| 58 | 0 | return $self->scripts; | |||||||||||||||||||||||
| 59 | } else { | ||||||||||||||||||||||||
| 60 | 0 | croak('No script_dir specified'); | |||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||
| 64 | 0 | ▶ | sub register_script { | ||||||||||||||||||||||
|
Subroutine
register_script: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 65 | 0 | my ($self, $script_name, $script) = @_; | |||||||||||||||||||||||
| 66 | 0 | ▶ | $script = $$script if ref($script); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||
| 68 | 0 | $self->{_script_cache}{$script_name} = [ sha1_hex($script), $script ]; | |||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||
| 70 | 0 | return $script_name; | |||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||
| 73 | 0 | ▶ | sub run_script { | ||||||||||||||||||||||
|
Subroutine
run_script: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||
| 74 | 0 | my ($self, $script_name, $args) = @_; | |||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||
| 76 | 0 | my $conn = $self->redis_conn; | |||||||||||||||||||||||
| 77 | 0 | ▶ | my $script = $self->_script_cache->{$script_name} | ||||||||||||||||||||||
|
Condition0%0/2
$self->_script_cache->{$script_name} // croak("Unknown script $script_name")
MC/DC0%0/1
$self->_script_cache->{$script_name} // croak("Unknown script $script_name")
$self->_script_cache->{$script_name}
Truth table0%0/2
A
A= $self->_script_cache->{$script_name}
| |||||||||||||||||||||||||
| 78 | // croak("Unknown script $script_name"); | ||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||
| 80 | 0 | my @result; | |||||||||||||||||||||||
| 81 | 0 | ▶ | my $try = sub { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||
| 82 | 0 | ▶ | $conn->evalsha($$script[0], $args ? @$args : (0)); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 83 | 0 | }; | |||||||||||||||||||||||
| 84 | 0 | my $wantarray = wantarray; | |||||||||||||||||||||||
| 85 | 0 | ▶ | if ( $wantarray ) { | ||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||
| 86 | 0 | @result = eval { $try->() }; | |||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||
| 88 | elsif ( defined $wantarray ) { | ||||||||||||||||||||||||
| 89 | 0 | $result[0] = eval { $try->() }; | |||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||
| 92 | 0 | eval { $try->() }; | |||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 95 | 0 | ▶ | croak $@ unless $@ =~ /NOSCRIPT/; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 96 | 0 | ▶ | return $conn->eval($$script[1], $args ? @$args : (0)); | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | 0 | ▶ | return unless defined $wantarray; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 100 | 0 | ▶ | return $wantarray ? @result : $result[0]; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||
| 103 | 0 | ▶ | sub register_file { | ||||||||||||||||||||||
|
Subroutine
register_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 104 | 0 | my ($self, $path_to_file) = @_; | |||||||||||||||||||||||
| 105 | 0 | ▶ | open my $fh, '<', File::Spec->catdir( $self->script_dir, $path_to_file ) | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 106 | or croak "error opening $path_to_file: $!"; | ||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||
| 108 | 0 | my $script_name = basename( $path_to_file ); | |||||||||||||||||||||||
| 109 | 0 | $script_name =~ s/\.lua$//; | |||||||||||||||||||||||
| 110 | 0 | my $script = do { local $/; <$fh> }; | |||||||||||||||||||||||
| 111 | 0 | return $self->register_script($script_name, $script); | |||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||
| 114 | 0 | ▶ | sub scripts { | ||||||||||||||||||||||
|
Subroutine
scripts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||||||||||||||||
| 116 | 0 | return keys %{ $self->_script_cache }; | |||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||
| 119 | 0 | ▶ | sub flush_all_scripts { | ||||||||||||||||||||||
|
Subroutine
flush_all_scripts: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 120 | 0 | my ($self) = @_; | |||||||||||||||||||||||
| 121 | 0 | eval { | |||||||||||||||||||||||
| 122 | 0 | $self->redis_conn->script_flush(); | |||||||||||||||||||||||
| 123 | 0 | $self->{_script_cache} = {}; | |||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 126 | 0 | croak "redis script_flush failed: $@"; | |||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||
| 128 | 0 | return $self; | |||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||