| 1 | package Cache::Redis; | ||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 13 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | my $_mp; | |||||||||||||||||||||||||||
| 11 | sub _mp { | ||||||||||||||||||||||||||||
| 12 | 0 | ▶ | $_mp ||= Data::MessagePack->new->utf8; | ||||||||||||||||||||||||||
|
Subroutine
_mp: not called CC=2 SCAR=17.9
Condition0%0/3
$_mp ||= "Data::MessagePack"->new->utf8
MC/DC0%0/2
$_mp ||= "Data::MessagePack"->new->utf8
$_mp
"Data::MessagePack"->new->utf8
Truth table0%0/3
A ||= B
A= $_mpB= "Data::MessagePack"->new->utf8
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | sub _mp_serialize { | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | _mp->pack(@_); | ||||||||||||||||||||||||||
|
Subroutine
_mp_serialize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | sub _mp_deserialize { | ||||||||||||||||||||||||||||
| 18 | 0 | ▶ | _mp->unpack(@_); | ||||||||||||||||||||||||||
|
Subroutine
_mp_deserialize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub _mk_serialize { | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $code = shift; | ||||||||||||||||||||||||||
|
Subroutine
_mk_serialize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | return sub { | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $flags; # for future extention | |||||||||||||||||||||||||||
| 28 | 0 | my $store_date = [$data, $flags]; | |||||||||||||||||||||||||||
| 29 | 0 | $code->($store_date); | |||||||||||||||||||||||||||
| 30 | 0 | }; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub _mk_deserialize { | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my $code = shift; | ||||||||||||||||||||||||||
|
Subroutine
_mk_deserialize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | return sub { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my ($org, $flags) = @{$code->($data)}; | |||||||||||||||||||||||||||
| 40 | 0 | $org; | |||||||||||||||||||||||||||
| 41 | 0 | }; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub new { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $args = @_ == 1 ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $default_expires_in = delete $args->{default_expires_in} || 60*60*24 * 30; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args->{'default_expires_in'} || 2592000
MC/DC0%0/1
delete $args->{'default_expires_in'} || 2592000
delete $args->{'default_expires_in'}
Truth table0%0/2
A
A= delete $args->{'default_expires_in'}
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $namespace = delete $args->{namespace} || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args->{'namespace'} || ''
MC/DC0%0/1
delete $args->{'namespace'} || ''
delete $args->{'namespace'}
Truth table0%0/2
A
A= delete $args->{'namespace'}
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $nowait = delete $args->{nowait} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args->{'nowait'} || 0
MC/DC0%0/1
delete $args->{'nowait'} || 0
delete $args->{'nowait'}
Truth table0%0/2
A
A= delete $args->{'nowait'}
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $redis_class = delete $args->{redis_class} || 'Redis'; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args->{'redis_class'} || 'Redis'
MC/DC0%0/1
delete $args->{'redis_class'} || 'Redis'
delete $args->{'redis_class'}
Truth table0%0/2
A
A= delete $args->{'redis_class'}
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my $redis = delete $args->{redis}; | |||||||||||||||||||||||||||
| 54 | 0 | my $serializer = delete $args->{serializer}; | |||||||||||||||||||||||||||
| 55 | 0 | my $serialize_methods = delete $args->{serialize_methods}; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | die '`serializer` and `serialize_methods` is exclusive option' if $serializer && $serialize_methods; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$serializer and $serialize_methods
MC/DC0%0/2
$serializer and $serialize_methods
$serializer
$serialize_methods
Truth table0%0/3
A and B
A= $serializerB= $serialize_methods
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $serializer ||= 'Storable' unless $serialize_methods; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$serializer ||= "Storable"
MC/DC0%0/1
$serializer ||= "Storable"
$serializer
Truth table0%0/2
A
A= $serializer
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | my ($serialize, $deserialize); | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($serializer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($serializer eq 'Storable') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 62 | 0 | require Storable; | |||||||||||||||||||||||||||
| 63 | 0 | $serialize_methods = [\&Storable::nfreeze, \&Storable::thaw]; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif ($serializer eq 'JSON') { | ||||||||||||||||||||||||||||
| 66 | 0 | require JSON::XS; | |||||||||||||||||||||||||||
| 67 | 0 | $serialize_methods = [\&JSON::XS::encode_json, \&JSON::XS::decode_json]; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($serialize_methods) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 0 | $serialize = _mk_serialize $serialize_methods->[0]; | |||||||||||||||||||||||||||
| 73 | 0 | $deserialize = _mk_deserialize $serialize_methods->[1]; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | elsif ($serializer eq 'MessagePack') { | ||||||||||||||||||||||||||||
| 76 | 0 | require Data::MessagePack; | |||||||||||||||||||||||||||
| 77 | 0 | $serialize = \&_mp_serialize; | |||||||||||||||||||||||||||
| 78 | 0 | $deserialize = \&_mp_deserialize; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | die 'Serializer is not found' if !$serialize || !$deserialize; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$serialize and $deserialize
MC/DC0%0/2
$serialize and $deserialize
$serialize
$deserialize
Truth table0%0/3
A and B
A= $serializeB= $deserialize
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | unless ( $redis ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | load $redis_class; | |||||||||||||||||||||||||||
| 84 | 0 | $redis = $redis_class->new( | |||||||||||||||||||||||||||
| 85 | encoding => undef, | ||||||||||||||||||||||||||||
| 86 | %$args | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | bless { | ||||||||||||||||||||||||||||
| 91 | 0 | default_expires_in => $default_expires_in, | |||||||||||||||||||||||||||
| 92 | serialize => $serialize, | ||||||||||||||||||||||||||||
| 93 | deserialize => $deserialize, | ||||||||||||||||||||||||||||
| 94 | redis => $redis, | ||||||||||||||||||||||||||||
| 95 | namespace => $namespace, | ||||||||||||||||||||||||||||
| 96 | nowait => $nowait, | ||||||||||||||||||||||||||||
| 97 | }, $class; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub get { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | $key = $self->{namespace} . $key; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $data = $self->{redis}->get($key); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | defined $data ? $self->{deserialize}->($data) : $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub get_multi { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my ($self, @keys) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_multi: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | @keys = map { $self->{namespace} . $_ } @keys; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my @data = $self->{redis}->mget(@keys); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 116 | 0 | my $ret = {}; | |||||||||||||||||||||||||||
| 117 | 0 | for my $key ( @keys ) { | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ( defined $data[$i] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $ret->{$key} = $self->{deserialize}->($data[$i]); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | $i++; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | $ret; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub set { | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my ($self, $key, $value, $expire) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | $self->_set($key, $value, $expire); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | $self->{redis}->wait_all_responses unless $self->{nowait}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub set_multi { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my ($self, @items) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_multi: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | for my $item ( @items ) { | |||||||||||||||||||||||||||
| 138 | 0 | $self->_set(@$item); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $self->{redis}->wait_all_responses unless $self->{nowait}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub _set { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my ($self, $key, $value, $expire) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 145 | 0 | $key = $self->{namespace} . $key; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | $expire ||= $self->{default_expires_in}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$expire ||= $self->{"default_expires_in"}
MC/DC0%0/2
$expire ||= $self->{"default_expires_in"}
$expire
$self->{"default_expires_in"}
Truth table0%0/3
A ||= B
A= $expireB= $self->{"default_expires_in"}
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | $self->{redis}->setex($key, $expire, $self->{serialize}->($value), sub {}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub get_or_set { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ($self, $key, $code, $expire) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_or_set: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | my $data = $self->get($key); | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | unless (defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | $data = $code->(); | |||||||||||||||||||||||||||
| 157 | 0 | $self->set($key, $data, $expire); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 0 | $data; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub remove { | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $data = $self->get($key); | |||||||||||||||||||||||||||
| 166 | 0 | $key = $self->{namespace} . $key; | |||||||||||||||||||||||||||
| 167 | 0 | $self->{redis}->del($key); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | $data; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | sub nowait_push { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | shift->{redis}->wait_all_responses; | ||||||||||||||||||||||||||
|
Subroutine
nowait_push: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 13 | 1; | |||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||