| 1 | package ShardedKV::Storage::Redis; | ||||||||||||
| 2 | 3 | $ShardedKV::Storage::Redis::VERSION = '0.20'; | |||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | # ABSTRACT: Abstract base class for storing k/v pairs in Redis | ||||||||||||
| 5 | |||||||||||||
| 6 | 3 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use Redis; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use List::Util qw(shuffle); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 3 | ▶ | use ShardedKV::Error::ConnectFail; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use ShardedKV::Error::DeleteFail; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 3 | with 'ShardedKV::Storage'; | |||||||||||
| 14 | |||||||||||||
| 15 | |||||||||||||
| 16 | 3 | has 'redis_connect_str' => ( | |||||||||||
| 17 | is => 'ro', | ||||||||||||
| 18 | isa => 'Str', | ||||||||||||
| 19 | required => 1, | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | |||||||||||||
| 23 | 3 | has 'redis_retry_every' => ( | |||||||||||
| 24 | is => 'ro', | ||||||||||||
| 25 | isa => 'Num', | ||||||||||||
| 26 | default => 500, | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | |||||||||||||
| 29 | |||||||||||||
| 30 | 3 | has 'redis_reconnect_timeout' => ( | |||||||||||
| 31 | is => 'ro', | ||||||||||||
| 32 | isa => 'Num', | ||||||||||||
| 33 | default => 0, | ||||||||||||
| 34 | ); | ||||||||||||
| 35 | |||||||||||||
| 36 | |||||||||||||
| 37 | 3 | has 'redis' => ( | |||||||||||
| 38 | is => 'rw', | ||||||||||||
| 39 | isa => 'Redis', | ||||||||||||
| 40 | lazy => 1, | ||||||||||||
| 41 | builder => '_make_connection', | ||||||||||||
| 42 | clearer => '_clear_connection', | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | |||||||||||||
| 46 | 3 | has 'expiration_time' => ( # in seconds | |||||||||||
| 47 | is => 'rw', | ||||||||||||
| 48 | isa => 'Num', | ||||||||||||
| 49 | ); | ||||||||||||
| 50 | |||||||||||||
| 51 | |||||||||||||
| 52 | 3 | has 'expiration_time_jitter' => ( # in seconds | |||||||||||
| 53 | is => 'rw', | ||||||||||||
| 54 | isa => 'Num', | ||||||||||||
| 55 | default => 0, | ||||||||||||
| 56 | ); | ||||||||||||
| 57 | |||||||||||||
| 58 | |||||||||||||
| 59 | |||||||||||||
| 60 | has 'database_number' => ( | ||||||||||||
| 61 | is => 'rw', | ||||||||||||
| 62 | isa => 'Int', | ||||||||||||
| 63 | 0 | ▶ | trigger => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||
| 65 | 0 | $self->redis->select(shift); | |||||||||||
| 66 | }, | ||||||||||||
| 67 | 3 | ); | |||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub _make_connection { | ||||||||||
|
Subroutine
_make_connection: not called CC=4 SCAR=30.0
| |||||||||||||
| 70 | 0 | my ($self) = @_; | |||||||||||
| 71 | 0 | my $endpoint = $self->redis_connect_str; | |||||||||||
| 72 | my $r = eval { | ||||||||||||
| 73 | 0 | Redis->new( # dies if it can't connect! | |||||||||||
| 74 | server => $endpoint, | ||||||||||||
| 75 | encoding => undef, # no automatic utf8 encoding for performance | ||||||||||||
| 76 | every => $self->redis_retry_every, | ||||||||||||
| 77 | reconnect => $self->redis_reconnect_timeout, | ||||||||||||
| 78 | ); | ||||||||||||
| 79 | 0 | ▶ | } or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 81 | 0 | ShardedKV::Error::ConnectFail->throw({ | |||||||||||
| 82 | endpoint => $endpoint, | ||||||||||||
| 83 | storage_type => 'redis', | ||||||||||||
| 84 | message => "Failed to make a connection to Redis ($endpoint): $error", | ||||||||||||
| 85 | }); | ||||||||||||
| 86 | }; | ||||||||||||
| 87 | 0 | my $dbno = $self->database_number; | |||||||||||
| 88 | 0 | ▶ | $r->select($dbno) if defined $dbno; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 0 | return $r; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | sub delete { | ||||||||||
|
Subroutine
delete: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 94 | 0 | my ($self, $key) = @_; | |||||||||||
| 95 | 0 | my $rv; | |||||||||||
| 96 | eval { | ||||||||||||
| 97 | 0 | $rv = $self->redis->del($key); | |||||||||||
| 98 | 0 | 1; | |||||||||||
| 99 | 0 | ▶ | } or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 101 | 0 | my $endpoint = $self->redis_connect_str; | |||||||||||
| 102 | 0 | $self->reset_connection; | |||||||||||
| 103 | 0 | ShardedKV::Error::DeleteFail->throw({ | |||||||||||
| 104 | endpoint => $endpoint, | ||||||||||||
| 105 | key => $key, | ||||||||||||
| 106 | storage_type => 'redis', | ||||||||||||
| 107 | message => "Failed to delete key ($key) to Redis ($endpoint): $error", | ||||||||||||
| 108 | }); | ||||||||||||
| 109 | }; | ||||||||||||
| 110 | 0 | ▶ | return $rv ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | |||||||||||||
| 114 | 0 | ▶ | sub get { die "Method get() not implemented in abstract base class" } | ||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 115 | |||||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | sub set { die "Method set() not implemented in abstract base class" } | ||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 118 | |||||||||||||
| 119 | 0 | ▶ | sub reset_connection { | ||||||||||
|
Subroutine
reset_connection: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 120 | 0 | my ($self) = @_; | |||||||||||
| 121 | 0 | $self->_clear_connection(); | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | 3 | ▶ | no Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 125 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 126 | |||||||||||||
| 127 | __END__ | ||||||||||||