EntityModel/StorageClass/KVStore/Layer/Memcached.pm

filter: Statementstmt 20.3% 12 / 59 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 17.6% 3 / 17 Podpod 66.6% 6 / 9 totaltotal 20.5% 21 / 102 CC 10 SCAR 17.4
CC
10
Cov
16%
run_pending
12.0
__ANON__
12.0
new
6.0
CRAP 5.7
« EntityModel/Query/ParseSQL.pm ↑ Summary EntityModel/StorageClass/KVStore/Mixin/Deferred.pm »
1 package EntityModel::StorageClass::KVStore::Layer::Memcached;
2 {
3 1 $EntityModel::StorageClass::KVStore::Layer::Memcached::VERSION = '0.102';
4 }
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent qw(EntityModel::StorageClass::KVStore::Layer EntityModel::StorageClass::KVStore::Mixin::Deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
10 0 my $class = shift;
11 0 my %args = @_;
12 0 my $self = $class->SUPER::new;
13 0 $self->{connected} = 0;
14 0 $self->{queue} = [ ];
15 0 require Net::Async::Memcached::Client;
16 $self->{memcached} = Net::Async::Memcached::Client->new(
17 host => $args{host} || 'localhost',
18 loop => $args{loop},
19 0 ▶ on_connected => sub { $self->connection_complete }
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 ▶ );
Condition0%0/2
$args{'host'} || 'localhost'
l!l
00
MC/DC0%0/1
$args{'host'} || 'localhost'
$args{'host'}
Truth table0%0/2
A
A= $args{'host'}
Aresult
11
00
21 0 $self
22 }
23  
24 0 ▶sub memcached { shift->{memcached} }
Subroutine memcached: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 =head2 has_pending
27  
28 Returns true if there are pending tasks in the queue.
29  
30 =cut
31  
32 0 ▶sub has_pending { @{ shift->{queue} } }
Subroutine has_pending: not called CC=1 SCAR=6.9
Pod documented
33  
34 =head2 run_pending
35  
36 Processes any tasks pending in the queue.
37  
38 =cut
39  
40 0 ▶sub run_pending {
Subroutine run_pending: not called CC=3 SCAR=24.8
Pod documented
41 0 my $self = shift;
42 0 ▶ return $self unless $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_connected 0 0 0
43 0 ▶ while(my $task = shift @{$self->{queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $task = shift @{$$self{"queue"};}) 0 0 0
44 0 my $method = shift @$task;
45 0 $self->$method(@$task);
46 }
47 0 $self;
48 }
49  
50 =head2 is_connected
51  
52 Returns true if we have established a connection.
53  
54 =cut
55  
56 0 ▶sub is_connected { shift->{connected} }
Subroutine is_connected: not called CC=1 SCAR=6.9
Pod documented
57  
58 =head2 queue_task
59  
60 Queues a task for processing when we get around to connecting
61 later.
62  
63 Takes the following parameters:
64  
65 =over 4
66  
67 =item * $method - should be 'store' or 'retrieve'
68  
69 =item * @args - any parameters, will be passed verbatim when we get around to
70 calling the method.
71  
72 =back
73  
74 Returns $self.
75  
76 =cut
77  
78 0 ▶sub queue_task {
Subroutine queue_task: not called CC=1 SCAR=6.9
Pod documented
79 0 my $self = shift;
80 0 push @{ $self->{queue} }, [ @_ ];
81 0 $self
82 }
83  
84 0 ▶sub retrieve {
Subroutine retrieve: not called CC=2 SCAR=17.9
Pod documented
85 0 my $self = shift;
86 0 ▶ return $self->queue_task(retrieve => @_) unless $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_connected 0 0 0
87 0 my %args = @_;
88  
89 0 my $on_success = $args{on_success};
90 0 my $k = $args{query};
91 $self->memcached->get(
92 $args{query},
93 0 ▶ on_complete => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
94 0 my %args = @_;
95 0 ▶ $on_success->($args{value}) if $on_success;
Branch: 1 branch
BranchTrueFalseTotal
if $on_success 0 0 0
96 },
97 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
98 0 my %err_args = @_;
99 0 ▶ return $self->retrieval_fallback(%args) if $self->memcached->status_text($err_args{status}) eq 'Key not found';
Branch: 1 branch
BranchTrueFalseTotal
if $self->memcached->status_text($err_args{"status"}) eq "Key not found" 0 0 0
100 0 warn "Failed:\n";
101 0 warn "* $_ => " . $err_args{$_} for keys %err_args;
102 0 die "giving up"
103 }
104 0 );
105 0 $self
106 }
107  
108 =head2 store
109  
110 Store a value in the cache.
111  
112 Takes the following parameters:
113  
114 =over 4
115  
116 =item * $query - query to store under
117  
118 =item * $value - value to store in key (can be undef)
119  
120 =back
121  
122 Returns $self.
123  
124 =cut
125  
126 0 ▶sub store {
Subroutine store: not called CC=2 SCAR=17.9
Pod documented
127 0 my ($self, $k, $v) = @_;
128 0 ▶ return $self->queue_task(store => @_) unless $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_connected 0 0 0
129  
130 $self->memcached->set(
131 $k => $v,
132 0 ▶ on_complete => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
133 0 ▶ on_error => sub { die "Failed because of @_\n" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 );
135 0 $self
136 }
137  
138 =head2 cleanup
139  
140 Takes the following parameters:
141  
142 =over 4
143  
144 =item *
145  
146 =back
147  
148 Returns
149  
150 =cut
151  
152 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod undocumented
153 0 my $self = shift;
154 0 my %args = @_;
155 $self->memcached->disconnect(
156 on_success => $args{on_success}
157 0 ▶ ) if $args{on_success};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"on_success"} 0 0 0
158 0 $self
159 }
160  
161 1 1;
162  
« EntityModel/Query/ParseSQL.pm ↑ Summary EntityModel/StorageClass/KVStore/Mixin/Deferred.pm »