| 1 | package ShardedKV::Storage::Redis::Hash; | ||||||||||||
| 2 | 2 | $ShardedKV::Storage::Redis::Hash::VERSION = '0.20'; | |||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | # ABSTRACT: Storing hash values in Redis | ||||||||||||
| 5 | 2 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use Redis; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use ShardedKV::Error::ReadFail; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use ShardedKV::Error::WriteFail; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 2 | extends 'ShardedKV::Storage::Redis'; | |||||||||||
| 12 | |||||||||||||
| 13 | 0 | ▶ | sub get { | ||||||||||
|
Subroutine
get: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 14 | 0 | my ($self, $key) = @_; | |||||||||||
| 15 | 0 | my $redis = $self->redis; | |||||||||||
| 16 | 0 | my $hash; | |||||||||||
| 17 | eval { | ||||||||||||
| 18 | 0 | my @foo = $redis->hgetall($key); | |||||||||||
| 19 | 0 | ▶ | unless (@foo % 2 == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 0 | require Data::Dumper; | |||||||||||
| 21 | 0 | die sprintf "PANIC: Should get an even number of keys from hgetall(%s). Got <%s>", | |||||||||||
| 22 | $key, Data::Dumper->new(\@foo)->Useqq(1)->Terse(1)->Indent(1)->Dump; | ||||||||||||
| 23 | } | ||||||||||||
| 24 | 0 | my %foo = @foo; | |||||||||||
| 25 | 0 | ▶ | if(keys %foo) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 26 | 0 | $hash = \%foo; | |||||||||||
| 27 | } | ||||||||||||
| 28 | 0 | 1; | |||||||||||
| 29 | 0 | ▶ | } or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 31 | 0 | my $endpoint = $self->redis_connect_str; | |||||||||||
| 32 | 0 | $self->reset_connection; | |||||||||||
| 33 | 0 | ShardedKV::Error::ReadFail->throw({ | |||||||||||
| 34 | endpoint => $endpoint, | ||||||||||||
| 35 | key => $key, | ||||||||||||
| 36 | storage_type => 'redis', | ||||||||||||
| 37 | message => "Failed to fetch key ($key) from Redis ($endpoint): $error", | ||||||||||||
| 38 | }); | ||||||||||||
| 39 | }; | ||||||||||||
| 40 | 0 | return $hash; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | sub set { | ||||||||||
|
Subroutine
set: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 44 | 0 | my ($self, $key, $value_ref) = @_; | |||||||||||
| 45 | 0 | ▶ | if (ref($value_ref) ne 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 0 | Carp::croak("Value must be a hashref"); | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | my $r = $self->redis; | |||||||||||
| 50 | |||||||||||||
| 51 | 0 | my $rv; | |||||||||||
| 52 | eval { | ||||||||||||
| 53 | 0 | $rv = $r->hmset($key, %$value_ref); | |||||||||||
| 54 | 0 | 1; | |||||||||||
| 55 | 0 | ▶ | } or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 57 | 0 | my $endpoint = $self->redis_connect_str; | |||||||||||
| 58 | 0 | $self->reset_connection; | |||||||||||
| 59 | 0 | ShardedKV::Error::WriteFail->throw({ | |||||||||||
| 60 | endpoint => $endpoint, | ||||||||||||
| 61 | key => $key, | ||||||||||||
| 62 | storage_type => 'redis', | ||||||||||||
| 63 | operation => 'set', | ||||||||||||
| 64 | message => "Failed to store key ($key) to Redis ($endpoint): $error", | ||||||||||||
| 65 | }); | ||||||||||||
| 66 | }; | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | my $expire = $self->expiration_time; | |||||||||||
| 69 | 0 | ▶ | if (defined $expire) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | eval { | ||||||||||||
| 71 | 0 | $r->pexpire( | |||||||||||
| 72 | $key, int(1000*($expire+rand($self->expiration_time_jitter))) | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | 0 | 1; | |||||||||||
| 75 | 0 | ▶ | } or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | ▶ | my $error = $@ || "Zombie Error"; | ||||||||||
|
Condition0%0/2
$@ || 'Zombie Error'
MC/DC0%0/1
$@ || 'Zombie Error'
$@
Truth table0%0/2
A
A= $@
| |||||||||||||
| 77 | 0 | my $endpoint = $self->redis_connect_str; | |||||||||||
| 78 | 0 | $self->reset_connection; | |||||||||||
| 79 | 0 | ShardedKV::Error::WriteFail->throw({ | |||||||||||
| 80 | endpoint => $endpoint, | ||||||||||||
| 81 | key => $key, | ||||||||||||
| 82 | storage_type => 'redis', | ||||||||||||
| 83 | operation => 'expire', | ||||||||||||
| 84 | message => "Failed to store key ($key) to Redis ($endpoint): $error", | ||||||||||||
| 85 | }); | ||||||||||||
| 86 | }; | ||||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 0 | return $rv; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | 2 | ▶ | no Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 93 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 94 | |||||||||||||
| 95 | =pod | ||||||||||||
| 96 | |||||||||||||
| 97 | =head1 NAME | ||||||||||||
| 98 | |||||||||||||
| 99 | ShardedKV::Storage::Redis::Hash - Storing hash values in Redis | ||||||||||||
| 100 | |||||||||||||
| 101 | =head1 VERSION | ||||||||||||
| 102 | |||||||||||||
| 103 | version 0.20 | ||||||||||||
| 104 | |||||||||||||
| 105 | =head1 SYNOPSIS | ||||||||||||
| 106 | |||||||||||||
| 107 | use ShardedKV; | ||||||||||||
| 108 | use ShardedKV::Storage::Redis::Hash; | ||||||||||||
| 109 | ... create ShardedKV... | ||||||||||||
| 110 | my $storage = ShardedKV::Storage::Redis::Hash->new( | ||||||||||||
| 111 | redis_connect_str => 'redisshard1:679', | ||||||||||||
| 112 | expiration_time => 60*60, #1h | ||||||||||||
| 113 | ); | ||||||||||||
| 114 | ... put storage into ShardedKV... | ||||||||||||
| 115 | |||||||||||||
| 116 | # values are scalar references to strings | ||||||||||||
| 117 | $skv->set("foo", {bar => 'baz', cat => 'dog'}); | ||||||||||||
| 118 | my $value_ref = $skv->get("foo"); | ||||||||||||
| 119 | |||||||||||||
| 120 | =head1 DESCRIPTION | ||||||||||||
| 121 | |||||||||||||
| 122 | This subclass of L<ShardedKV::Storage::Redis> implements | ||||||||||||
| 123 | simple string/blob values in Redis. See the documentation | ||||||||||||
| 124 | for C<ShardedKV::Storage::Redis> for the interface of this | ||||||||||||
| 125 | class. | ||||||||||||
| 126 | |||||||||||||
| 127 | The values of a C<ShardedKV::Storage::Redis::Hash> are | ||||||||||||
| 128 | actually scalar references to strings. | ||||||||||||
| 129 | |||||||||||||
| 130 | =head1 SEE ALSO | ||||||||||||
| 131 | |||||||||||||
| 132 | =over 4 | ||||||||||||
| 133 | |||||||||||||
| 134 | =item * | ||||||||||||
| 135 | |||||||||||||
| 136 | L<ShardedKV> | ||||||||||||
| 137 | |||||||||||||
| 138 | =item * | ||||||||||||
| 139 | |||||||||||||
| 140 | L<ShardedKV::Storage> | ||||||||||||
| 141 | |||||||||||||
| 142 | =item * | ||||||||||||
| 143 | |||||||||||||
| 144 | L<ShardedKV::Storage::Redis> | ||||||||||||
| 145 | |||||||||||||
| 146 | =item * | ||||||||||||
| 147 | |||||||||||||
| 148 | L<ShardedKV::Storage::Redis::String> | ||||||||||||
| 149 | |||||||||||||
| 150 | =back | ||||||||||||
| 151 | |||||||||||||
| 152 | =head1 AUTHORS | ||||||||||||
| 153 | |||||||||||||
| 154 | =over 4 | ||||||||||||
| 155 | |||||||||||||
| 156 | =item * | ||||||||||||
| 157 | |||||||||||||
| 158 | Steffen Mueller <smueller@cpan.org> | ||||||||||||
| 159 | |||||||||||||
| 160 | =item * | ||||||||||||
| 161 | |||||||||||||
| 162 | Nick Perez <nperez@cpan.org> | ||||||||||||
| 163 | |||||||||||||
| 164 | =item * | ||||||||||||
| 165 | |||||||||||||
| 166 | Damian Gryski <dgryski@cpan.org> | ||||||||||||
| 167 | |||||||||||||
| 168 | =back | ||||||||||||
| 169 | |||||||||||||
| 170 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 171 | |||||||||||||
| 172 | This software is copyright (c) 2013 by Steffen Mueller. | ||||||||||||
| 173 | |||||||||||||
| 174 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 175 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 176 | |||||||||||||
| 177 | =cut | ||||||||||||
| 178 | |||||||||||||
| 179 | __END__ | ||||||||||||