Redis.pm

filter: Statementstmt 25.1% 173 / 689 Branchbran 12.9% 30 / 232 Conditioncond 8.7% 16 / 183 MC/DCmcdc 4.7% 5 / 105 Subroutinesub 21.6% 18 / 83 Podpod 83.3% 45 / 54 totaltotal 21.3% 287 / 1346 CC 198 SCAR 47.4
CC
198
Cov
20%
connect_to_server
650.0
configure
420.0
handle_pubsub_message
110.0
CRAP 113.9
« Redis/Protocol.pm ↑ Summary Redis/Server/Database.pm »
1 package Net::Async::Redis;
2 # ABSTRACT: Redis support for IO::Async
3  
4 14 ▶use Full::Class qw(:v1), extends => qw(Net::Async::Redis::Commands);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = '6.006';
7 14 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 =head1 NAME
10  
11 Net::Async::Redis - talk to Redis servers via L<IO::Async>
12  
13 =head1 SYNOPSIS
14  
15 use Net::Async::Redis;
16 use Future::AsyncAwait;
17 use IO::Async::Loop;
18 my $loop = IO::Async::Loop->new;
19 $loop->add(my $redis = Net::Async::Redis->new);
20 my $value = await $redis->get('some_key');
21 $value ||= await $redis->set(some_key => 'some_value');
22 print "Value: $value\n";
23  
24 # You can also use ->then chaining, see L<Future> for more details
25 $redis->connect->then(sub {
26 $redis->get('some_key')
27 })->then(sub {
28 my $value = shift;
29 return Future->done($value) if $value;
30 $redis->set(some_key => 'some_value')
31 })->on_done(sub {
32 print "Value: " . shift;
33 })->get;
34  
35 =head1 DESCRIPTION
36  
37 Provides client access for dealing with Redis servers.
38  
39 6ee L<Net::Async::Redis::Commands> for the full list of commands, this list
40 is autogenerated from the official documentation here:
41  
42 L<https://redis.io/commands>
43  
44 This is intended to be a near-complete low-level client module for asynchronous Redis
45 support. See L<Net::Async::Redis::Server> for a (limited) Perl server implementation.
46  
47 This is an unofficial Perl port, and not endorsed by the Redis server maintainers in any
48 way.
49  
50 =head2 Supported features
51  
52 Current features include:
53  
54 =over 4
55  
56 =item * L<all commands|https://redis.io/commands> as of 8.0-M02 (November 2024), see L<https://redis.io/commands> for the methods and parameters
57  
58 =item * L<pub/sub support|https://redis.io/topics/pubsub>, see L</METHODS - Subscriptions> including sharded pubsub
59  
60 =item * L<pipelining|https://redis.io/topics/pipelining>, see L</pipeline_depth>
61  
62 =item * L<transactions|https://redis.io/topics/transactions>, see L</METHODS - Transactions>
63  
64 =item * L<streams|https://redis.io/topics/streams-intro> and consumer groups, via L<Net::Async::Redis::Commands/XADD> and related methods
65  
66 =item * L<client-side caching|https://redis.io/topics/client-side-caching>, see L</METHODS - Clientside caching>
67  
68 =item * L<RESP3/https://github.com/antirez/RESP3/blob/master/spec.md> protocol for Redis 6 and above, allowing pubsub on the same connection as regular commands
69  
70 =item * cluster support via L<Net::Async::Redis::Cluster>, including key specifications from L<https://redis.io/docs/reference/key-specs/> to route commands to
71 the correct node(s)
72  
73 =item * see L<Net::Async::Redis::XS> for a faster XS version (can be 40x faster than the pure Perl version, particularly when parsing large L<Net::Async::Redis::Commands/XREADGROUP> responses)
74  
75 =back
76  
77 =head2 Connecting
78  
79 As with any other L<IO::Async::Notifier>-based module, you'll need to
80 add this to an L<IO::Async::Loop>:
81  
82 my $loop = IO::Async::Loop->new;
83 $loop->add(
84 my $redis = Net::Async::Redis->new
85 );
86  
87 then connect to the server:
88  
89 use Future::AsyncAwait;
90 await $redis->connect;
91 # You could achieve a similar result by passing client_name in
92 # constructor or ->connect parameters
93 await $redis->client_setname("example client");
94  
95 =head2 Key-value handling
96  
97 One of the most common Redis scenarios is as a key/value store. The L</get> and L</set>
98 methods are typically used here:
99  
100 use Future::AsyncAwait;
101 await $redis->connect;
102 $redis->set(some_key => 'some value');
103 my ($value) = await $redis->get('some_key');
104 print "Read back value [$value]\n";
105  
106 See the next section for more information on what these methods are actually returning.
107  
108 =head2 Requests and responses
109  
110 Requests are implemented as methods on the L<Net::Async::Redis> object.
111 These typically return a L<Future> which will resolve once ready:
112  
113 my $future = $redis->incr("xyz")
114 ->on_done(sub {
115 print "result of increment was " . shift . "\n"
116 });
117  
118 For synchronous code, call C<< ->get >> on that L<Future>:
119  
120 print "Database has " . $redis->dbsize->get . " total keys\n";
121  
122 This means you can end up with C<< ->get >> being called on the result of C<< ->get >>,
123 note that these are two very different methods:
124  
125 $redis
126 ->get('some key') # this is being called on $redis, and is issuing a GET request
127 ->get # this is called on the returned Future, and blocks until the value is ready
128  
129 Typical async code would not be expected to use the L<Future/get> method extensively;
130 often only calling it in one place at the top level in the code.
131  
132 =head3 RESP3 and RESP2 compatibility
133  
134 In RESP3 some of the responses are structured differently from RESP2.
135 L<Net::Async::Redis> guarantees the same structure unless you have explicitly requested the new types
136 using the L</configure> C<hashrefs> option, which is disabled by default.
137  
138 Generally RESP3 is recommended if you have Redis version 6 or later installed: it allows
139 subscription operations to share the same connection as regular Redis traffic.
140  
141 =cut
142  
143 =head2 Error handling
144  
145 Since L<Future> is used for deferred results, failure is indicated
146 by a failing Future with L<failure category|Future/FAILURE-CATEGORIES>
147 of C<redis>.
148  
149 The L<Future/catch> feature may be useful for handling these:
150  
151 $redis->lpush(key => $value)
152 ->catch(
153 redis => sub { warn "probably an incorrect type, cannot push value"; Future->done }
154 )->get;
155  
156 Note that this module uses L<Future::AsyncAwait> internally.
157  
158 =cut
159  
160 14 ▶use Future::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 14 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 14 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 14 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 14 ▶use URI::redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 14 ▶use Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 14 ▶use Metrics::Any qw($metrics), strict => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 14 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
170 14 ▶use Scalar::Util qw(reftype blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 14 ▶use Net::Async::Redis::Multi;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 14 ▶use Net::Async::Redis::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 14 ▶use Net::Async::Redis::Subscription::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 =head1 CONSTANTS
177  
178 =head2 OPENTRACING_ENABLED
179  
180 Defaults to false, this can be controlled by the C<USE_OPENTRACING>
181 environment variable. This provides a way to set the default opentracing
182 mode for all L<Net::Async::Redis> instances - you can enable/disable
183 for a specific instance via L</configure>:
184  
185 $redis->configure(opentracing => 1);
186  
187 When enabled, this will create a span for every Redis request. See
188 L<OpenTracing::Any> for details.
189  
190 =cut
191  
192 14 ▶use constant OPENTRACING_ENABLED => $ENV{USE_OPENTRACING} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"USE_OPENTRACING"} // 0
l!l
014
MC/DC0%0/1
$ENV{"USE_OPENTRACING"} // 0
$ENV{"USE_OPENTRACING"}
Truth table50%1/2
A
A= $ENV{"USE_OPENTRACING"}
Aresult
11
00
193  
194 =head2 OPENTELEMETRY_ENABLED
195  
196 Defaults to false, this can be controlled by the C<USE_OPENTELEMETRY>
197 environment variable. This provides a way to set the default C<opentelemetry>
198 mode for all L<Net::Async::Redis> instances - you can enable/disable
199 for a specific instance via L</configure>:
200  
201 $redis->configure(opentelemetry => 1);
202  
203 When enabled, this will create a span for every Redis request. See
204 L<OpenTelemetry> or L<https://opentelemetry.io> for details.
205  
206 =cut
207  
208 14 ▶use constant OPENTELEMETRY_ENABLED => $ENV{USE_OPENTELEMETRY} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"USE_OPENTELEMETRY"} // 0
l!l
014
MC/DC0%0/1
$ENV{"USE_OPENTELEMETRY"} // 0
$ENV{"USE_OPENTELEMETRY"}
Truth table50%1/2
A
A= $ENV{"USE_OPENTELEMETRY"}
Aresult
11
00
209  
210 14 our $provider;
211 14 our $tracer;
212 14 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
213 14 if(OPENTRACING_ENABLED) {
214 require OpenTracing::Any;
215 OpenTracing::Any->import(qw($tracer));
216 }
217  
218 14 if(OPENTELEMETRY_ENABLED) {
219 require OpenTelemetry;
220 require OpenTelemetry::Context;
221 require OpenTelemetry::Trace;
222 require OpenTelemetry::Constants;
223 OpenTelemetry::Constants->import(qw( SPAN_STATUS_ERROR SPAN_STATUS_OK ));
224 $provider = OpenTelemetry->tracer_provider;
225 $tracer = $provider->tracer(
226 name => __PACKAGE__,
227 version => __PACKAGE__->VERSION
228 );
229 }
230 }
231  
232 # These only apply to the legacy RESP2 protocol. Since RESP3, connections
233 # are no longer restricted once pubsub activity has started.
234 14 our %ALLOWED_SUBSCRIPTION_COMMANDS = (
235 SUBSCRIBE => 1,
236 PSUBSCRIBE => 1,
237 UNSUBSCRIBE => 1,
238 PUNSUBSCRIBE => 1,
239 PING => 1,
240 QUIT => 1,
241 );
242  
243 # Any of these commands would necessitate switching a RESP2 connection into
244 # a limited pubsub-only mode.
245 14 our %SUBSCRIPTION_COMMANDS = (
246 SUBSCRIBE => 1,
247 PSUBSCRIBE => 1,
248 UNSUBSCRIBE => 1,
249 PUNSUBSCRIBE => 1,
250 MESSAGE => 1,
251 PMESSAGE => 1,
252 );
253 14 our %COMMAND_DEFINITION = %Net::Async::Redis::Commands::COMMAND_DEFINITION;
254  
255 # Add support for secure Redis `rediss://...` URIs
256 0 ▶unless(URI::rediss->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("URI::rediss"->can("new")) 14 0 14
257 14 push @URI::rediss::ISA, qw(URI::redis);
258 0 ▶ *URI::rediss::secure = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
259 }
260  
261 =head1 METHODS
262  
263 B<NOTE>: For a full list of the Redis methods supported by this module,
264 please see L<Net::Async::Redis::Commands>.
265  
266 =cut
267  
268 =head2 configure
269  
270 Applies configuration parameters - currently supports:
271  
272 =over 4
273  
274 =item * C<host>
275  
276 =item * C<port>
277  
278 =item * C<auth>
279  
280 =item * C<database>
281  
282 =item * C<pipeline_depth>
283  
284 =item * C<stream_read_len>
285  
286 =item * C<stream_write_len>
287  
288 =item * C<on_disconnect>
289  
290 =item * C<client_name>
291  
292 =item * C<opentracing>
293  
294 =item * C<opentelemetry>
295  
296 =item * C<protocol> - either 'resp2' or 'resp3', default is autodetect
297  
298 =item * C<hashrefs> - RESP3 (Redis 6.0+) supports more data types, currently the only difference this
299 makes to us is that it now supports hashrefs for key/value pairs. This is disabled by default to ensure
300 compatibility across newer+older versions.
301  
302 =back
303  
304 Note that enabling C<hashrefs> will cause connections to fail if the server does not support RESP3.
305  
306 =cut
307  
308 0 ▶method configure (%args) {
Subroutine configure: not called CC=20 SCAR=60.4
Pod documented
309 0 for (qw(
310 host
311 port
312 username
313 auth
314 database
315 pipeline_depth
316 stream_read_len
317 stream_write_len
318 on_disconnect
319 client_name
320 opentracing
321 opentelemetry
322 protocol
323 hashrefs
324 tls_cert_file
325 tls_key_file
326 tls_ca_file
327 )) {
328 0 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 0 0
329 }
330 0 ▶ die 'invalid protocol requested: ' . $self->{protocol} if defined $self->{protocol} and not $self->{protocol} =~ /^resp[23]$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{"protocol"} and not $$self{"protocol"} =~ /^resp[23]$/u 0 0 0
Condition0%0/3
defined $$self{"protocol"} and not $$self{"protocol"} =~ /^resp[23]$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$self{"protocol"} and not $$self{"protocol"} =~ /^resp[23]$/u
defined $$self{"protocol"} not $$self{"protocol"} =~ /^resp[23]$/u
Truth table0%0/3
A and not(B)
A= defined $$self{"protocol"}B= not $$self{"protocol"} =~ /^resp[23]$/u
ABresult
0X0
100
111
331  
332 # Be more lenient with the URI parameter, since it's tedious to
333 # need the redis:// prefix every time... after all, what else
334 # would we expect it to be?
335 0 ▶ if(exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"uri"}) 0 0 0
336 0 my $uri = delete $args{uri};
337 0 ▶ $uri = "redis://$uri" unless ref($uri) or $uri =~ /^rediss?:/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri or $uri =~ /^rediss?:/u 0 0 0
Condition0%0/3
ref $uri or $uri =~ /^rediss?:/u
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $uri or $uri =~ /^rediss?:/u
ref $uri $uri =~ /^rediss?:/u
Truth table0%0/3
A or B
A= ref $uriB= $uri =~ /^rediss?:/u
ABresult
1X1
011
000
338 0 $self->{uri} = $uri;
339 }
340  
341 0 ▶ if(exists $args{client_side_cache_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"client_side_cache_size"}) 0 0 0
342 0 $self->{client_side_cache_size} = delete $args{client_side_cache_size};
343 0 delete $self->{client_side_cache};
344 0 ▶ if($self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->loop) 0 0 0
345 0 my $conn = delete $self->{client_side_connection};
346 0 ▶ $conn->remove_from_parent if $conn;
Branch: 1 branch
BranchTrueFalseTotal
if $conn 0 0 0
347 }
348 }
349 0 ▶ my $uri = $self->{uri} = URI->new($self->{uri}) unless ref $self->uri;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->uri 0 0 0
350 0 ▶ if($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) 0 0 0
351 0 ▶ $self->{host} //= $uri->host;
Condition0%0/3
$$self{"host"} //= $uri->host
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"host"} //= $uri->host
$$self{"host"} $uri->host
Truth table0%0/3
A //= B
A= $$self{"host"}B= $uri->host
ABresult
1X1
011
000
352 0 ▶ $self->{port} //= $uri->port;
Condition0%0/3
$$self{"port"} //= $uri->port
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"port"} //= $uri->port
$$self{"port"} $uri->port
Truth table0%0/3
A //= B
A= $$self{"port"}B= $uri->port
ABresult
1X1
011
000
353 }
354  
355 0 ▶ die 'hashref support requires RESP3 (Redis version 6+)' if defined $self->{protocol} and $self->{protocol} eq 'resp2' and $self->{hashrefs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{"protocol"} and $$self{"protocol"} eq "resp2" and $$self{"hashrefs"} 0 0 0
Condition0%0/6
defined $$self{"protocol"} and $$self{"protocol"} eq "resp2" and $$self{"hashrefs"}
!ll&&!rl&&r
000
defined $$self{"protocol"} and $$self{"protocol"} eq "resp2"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $$self{"protocol"} and $$self{"protocol"} eq "resp2" and $$self{"hashrefs"}
defined $$self{"protocol"} $$self{"protocol"} eq "resp2" $$self{"hashrefs"}
Truth table0%0/4
A and B and C
A= defined $$self{"protocol"}B= $$self{"protocol"} eq "resp2"C= $$self{"hashrefs"}
ABCresult
0XX0
10X0
1100
1111
356  
357 0 ▶ die 'opentelemetry requested but not available, set USE_OPENTELEMETRY=1 in the environment to enable' if $self->opentelemetry and not OPENTELEMETRY_ENABLED;
Branch: 1 branch
BranchTrueFalseTotal
if 0 0 0
Condition0%0/2
$self->opentelemetry and
!ll
00
MC/DC0%0/1
$self->opentelemetry and
$self->opentelemetry
Truth table0%0/2
A
A= $self->opentelemetry
Aresult
00
11
358 0 $self->next::method(%args)
359 14 }
360  
361 =head2 host
362  
363 Returns the host or IP address for the Redis server.
364  
365 =cut
366  
367 0 ▶method host { $self->{host} }
Subroutine host: not called CC=1 SCAR=1.2
Pod documented
368  
369 =head2 port
370  
371 Returns the port used for connecting to the Redis server.
372  
373 =cut
374  
375 0 ▶method port { $self->{port} }
Subroutine port: not called CC=1 SCAR=1.2
Pod documented
376  
377 =head2 database
378  
379 Returns the database index used when connecting to the Redis server.
380  
381 See the L<Net::Async::Redis::Commands/select> method for details.
382  
383 =cut
384  
385 0 ▶method database { $self->{database} }
Subroutine database: not called CC=1 SCAR=1.2
Pod documented
386  
387 =head2 uri
388  
389 Returns the Redis endpoint L<URI> instance.
390  
391 =cut
392  
393 0 ▶method uri { $self->{uri} //= URI->new('redis://localhost') }
Subroutine uri: not called CC=2 SCAR=14.0
Pod documented
Condition0%0/3
$$self{"uri"} //= "URI"->new("redis://localhost")
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"uri"} //= "URI"->new("redis://localhost")
$$self{"uri"} "URI"->new("redis://localhost")
Truth table0%0/3
A //= B
A= $$self{"uri"}B= "URI"->new("redis://localhost")
ABresult
1X1
011
000
394  
395 =head2 stream_read_len
396  
397 Returns the buffer size when reading from a Redis connection.
398  
399 Defaults to 1MB, reduce this if you're dealing with a lot of connections and
400 want to minimise memory usage. Alternatively, if you're reading large amounts
401 of data and spend too much time in needless C<epoll_wait> calls, try a larger
402 value.
403  
404 =cut
405  
406 0 ▶method stream_read_len { $self->{stream_read_len} //= 1048576 }
Subroutine stream_read_len: not called CC=2 SCAR=13.0
Pod documented
Condition0%0/2
$$self{"stream_read_len"} //= 1048576
l!l
00
MC/DC0%0/1
$$self{"stream_read_len"} //= 1048576
$$self{"stream_read_len"}
Truth table0%0/2
A
A= $$self{"stream_read_len"}
Aresult
11
00
407  
408 =head2 stream_write_len
409  
410 Returns the buffer size when writing to Redis connections, in bytes. Defaults to 1MB.
411  
412 See L</stream_read_len>.
413  
414 =cut
415  
416 0 ▶method stream_write_len { $self->{stream_write_len} //= 1048576 }
Subroutine stream_write_len: not called CC=2 SCAR=13.0
Pod documented
Condition0%0/2
$$self{"stream_write_len"} //= 1048576
l!l
00
MC/DC0%0/1
$$self{"stream_write_len"} //= 1048576
$$self{"stream_write_len"}
Truth table0%0/2
A
A= $$self{"stream_write_len"}
Aresult
11
00
417  
418 =head2 client_name
419  
420 Returns the name used for this client when connecting.
421  
422 =cut
423  
424 0 ▶method client_name { $self->{client_name} }
Subroutine client_name: not called CC=1 SCAR=1.2
Pod documented
425  
426 =head1 METHODS - Connection
427  
428 =head2 connect
429  
430 Connects to the Redis server.
431  
432 Will use the L</configure>d parameters if available, but as a convenience
433 can be passed additional parameters which will then be applied as if you
434 had called L</configure> with those beforehand. This also means that they
435 will be preserved for subsequent L</connect> calls.
436  
437 =cut
438  
439 0 ▶method connect (%args) {
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
440 0 ▶ return $self->{connection} if $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"connection"} 0 0 0
441 0 $self->{connection_in_progress} = 1;
442 0 ▶ my $f = $self->connect_to_server(%args)->on_ready(sub ($f) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
443 0 $log->tracef('connection call complete - %s', $f->state);
444 0 $self->{connection_in_progress} = 0;
445 0 ▶ delete $self->{connection} unless $f->is_done
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_done 0 0 0
446 0 });
447 0 return $self->{connection} = $f;
448 14 }
449  
450 0 ▶async method connect_to_server (%args) {
Subroutine connect_to_server: not called CC=25 SCAR=64.8
Pod undocumented
451 0 ▶ $self->configure(%args) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 0 0 0
452 0 my $uri = $self->uri->clone;
453 0 for (qw(host port)) {
454 0 ▶ $uri->$_($self->$_) if defined $self->$_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$_ 0 0 0
455 }
456  
457 # 0 is the default anyway, no need to apply in that case
458 0 ▶ $uri->path('/' . $self->database) if $self->database;
Branch: 1 branch
BranchTrueFalseTotal
if $self->database 0 0 0
459  
460 0 my $auth = $self->{auth};
461 # Our configured values are used in preference to the URI,
462 # to support separation of sensitive information (pull URI
463 # from config with defaults for user/password, then override
464 # those with secret values taken from another source).
465 0 ▶ my $username = $self->{username} // 'default';
Condition0%0/2
$$self{"username"} // "default"
l!l
00
MC/DC0%0/1
$$self{"username"} // "default"
$$self{"username"}
Truth table0%0/2
A
A= $$self{"username"}
Aresult
11
00
466 0 ▶ if(!defined($auth) and defined($uri->userinfo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $auth and defined $uri->userinfo) 0 0 0
Condition0%0/3
not defined $auth and defined $uri->userinfo
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $auth and defined $uri->userinfo
not defined $auth defined $uri->userinfo
Truth table0%0/3
not(A) and B
A= not defined $authB= defined $uri->userinfo
ABresult
0X0
100
111
467 0 (my $userpart, $auth) = split ':', $uri->userinfo, 2;
468 0 ▶ $username = $userpart if length $userpart;
Branch: 1 branch
BranchTrueFalseTotal
if length $userpart 0 0 0
469 }
470 0 $log->tracef('About to start connection to %s', "$uri");
471 0 ▶ my $tls = $self->{tls} // $uri->secure // 0;
Condition0%0/5
($$self{"tls"} // $uri->secure) // 0
l!l
00
$$self{"tls"} // $uri->secure
l!l&&r!l&&!r
000
MC/DC0%0/2
($$self{"tls"} // $uri->secure) // 0
$$self{"tls"} $uri->secure
Truth table0%0/3
A // B
A= $$self{"tls"}B= $uri->secure
ABresult
1X1
011
000
472 0 ▶ require IO::Async::SSL if $tls;
Branch: 1 branch
BranchTrueFalseTotal
if $tls 0 0 0
473 0 await $self->loop->later;
474  
475 0 ▶ my $sock = await $self->loop->connect(
Branch: 1 branch
BranchTrueFalseTotal
$tls ? : 0 0 0
Condition0%0/2
$uri->port // 6379
l!l
00
MC/DC0%0/1
$uri->port // 6379
$uri->port
Truth table0%0/2
A
A= $uri->port
Aresult
11
00
476 service => $uri->port // 6379,
477 host => $uri->host,
478 socktype => 'stream',
479 $tls ? (
480 extensions => ['SSL'],
481 $self->ssl_options->%*,
482 ) : (),
483 );
484 0 $self->{endpoint} = join ':', $sock->peerhost, $sock->peerport;
485 0 $self->{local_endpoint} = join ':', $sock->sockhost, $sock->sockport;
486 0 my $proto = $self->wire_protocol;
487 my $stream = IO::Async::Stream->new(
488 handle => $sock,
489 read_len => $self->stream_read_len,
490 write_len => $self->stream_write_len,
491 # Arbitrary multipliers for our stream values,
492 # in a memory-constrained environment it's expected
493 # that ->stream_read_len would be configured with
494 # low enough values for this not to be a concern.
495 read_high_watermark => 16 * $self->stream_read_len,
496 read_low_watermark => 2 * $self->stream_read_len,
497 on_closed => $self->curry::weak::notify_close,
498 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
499 0 $proto->parse($_[1]);
500 0 0
501 }
502 0 );
503 0 $self->add_child($stream);
504 Scalar::Util::weaken(
505 0 $self->{stream} = $stream
506 );
507  
508 try {
509 # Pretend we tried and failed if the old version was specifically requested
510 0 ▶ die 'ERR unknown command' if $self->{protocol} and $self->{protocol} eq 'resp2';
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"protocol"} and $$self{"protocol"} eq "resp2" 0 0 0
Condition0%0/3
$$self{"protocol"} and $$self{"protocol"} eq "resp2"
!ll&&!rl&&r
000
MC/DC0%0/2
$$self{"protocol"} and $$self{"protocol"} eq "resp2"
$$self{"protocol"} $$self{"protocol"} eq "resp2"
Truth table0%0/3
A and B
A= $$self{"protocol"}B= $$self{"protocol"} eq "resp2"
ABresult
0X0
100
111
511  
512 # Try issuing a HELLO to detect RESP3 or above
513 0 ▶ await $self->hello(
Branch: 2 branches
BranchTrueFalseTotal
defined $auth ? : 0 0 0
defined $self->client_name ? : 0 0 0
514 3, defined($auth) ? (
515 qw(AUTH), $username, $auth
516 ) : (), defined($self->client_name) ? (
517 qw(SETNAME), $self->client_name
518 ) : ()
519 );
520 0 $log->tracef('RESP3 detected');
521 0 $self->{protocol_level} = 'resp3';
522  
523 0 $proto->{hashrefs} = $self->{hashrefs};
524 0 $proto->{protocol} = $self->{protocol_level};
525 0 } catch ($e) {
526 # If we had an auth failure or invalid client name, all bets are off:
527 # immediately raise those back to the caller
528 0 ▶ die $e unless $e =~ /ERR unknown command/;
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /ERR unknown command/u 0 0 0
529  
530 0 $log->tracef('Older Redis version detected, dropping back to RESP2 protocol');
531 0 $self->{protocol_level} = 'resp2';
532  
533 0 ▶ die 'extended data structures require RESP3 (Redis version 6+)' if $self->{hashrefs};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"hashrefs"} 0 0 0
534  
535 0 $proto->{hashrefs} = $self->{hashrefs};
536 0 $proto->{protocol} = $self->{protocol_level};
537  
538 0 ▶ await $self->auth($auth) if defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $auth 0 0 0
539 0 ▶ await $self->client_setname($self->client_name) if defined $self->client_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->client_name 0 0 0
540 }
541  
542 0 ▶ if($uri->database) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->database) 0 0 0
543 try {
544 0 $log->tracef('Select database %s', $uri->database);
545 0 await $self->select($uri->database);
546 0 } catch($e) {
547 0 die 'Failed to switch database on Redis connection - ' . $e;
548 }
549 }
550 0 ▶ if($self->is_client_side_cache_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_client_side_cache_enabled) 0 0 0
551 try {
552 0 $log->tracef('Client-side cache requested');
553 0 await $self->client_side_connection;
554 0 $log->tracef('Client-side connection established');
555 0 } catch($e) {
556 0 ▶ die 'This version of Redis does not support clientside caching' if $e =~ /Unknown subcommand .*tracking/i;
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /Unknown subcommand .*tracking/iu 0 0 0
557 0 $log->errorf('Clientside cache setup failure in ->connect - %s', $e);
558 0 die 'Failed to enable clientside caching on Redis connection - ' . $e;
559 }
560 }
561 0 return $stream;
562 14 }
563  
564 =head2 connected
565  
566 Establishes a connection if needed, otherwise returns an immediately-available
567 L<Future> instance.
568  
569 =cut
570  
571 0 ▶method connected {
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
572 0 ▶ return $self->{connection} if $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"connection"} 0 0 0
573 0 $self->connect;
574 14 }
575  
576 =head2 endpoint
577  
578 The string describing the remote endpoint.
579  
580 =cut
581  
582 0 ▶method endpoint { $self->{endpoint} }
Subroutine endpoint: not called CC=1 SCAR=1.2
Pod documented
583  
584 =head2 local_endpoint
585  
586 A string describing the local endpoint, usually C<host:port>.
587  
588 =cut
589  
590 0 ▶method local_endpoint { $self->{local_endpoint} }
Subroutine local_endpoint: not called CC=1 SCAR=1.2
Pod documented
591  
592 =head1 METHODS - Subscriptions
593  
594 See L<https://redis.io/topics/pubsub> for more details on this topic.
595 There's also more details on the internal implementation in Redis here:
596 L<https://making.pusher.com/redis-pubsub-under-the-hood/>.
597  
598 B<NOTE>: On Redis versions prior to 6.0, you will need a I<separate> connection
599 for subscriptions; you cannot share a connection for regular requests once
600 any of the L</subscribe> or L</psubscribe> methods have been called on an
601 existing connection.
602  
603 With Redis 6.0, a newer protocol version (RESP3) is used by default, and
604 this is quite happy to support pubsub activity on the same connection
605 as other traffic.
606  
607 =cut
608  
609 =head2 psubscribe
610  
611 Subscribes to a pattern.
612  
613 Example:
614  
615 # Subscribe to 'info::*' channels, i.e. any message
616 # that starts with the C<info::> prefix, and prints them
617 # with a timestamp.
618 $redis_connection->psubscribe('info::*')
619 ->then(sub {
620 my $sub = shift;
621 $sub->map('payload')
622 ->each(sub {
623 print localtime . ' ' . $_ . "\n";
624 })->retain
625 })->get;
626 # this will block until the subscribe is confirmed. Note that you can't publish on
627 # a connection that's handling subscriptions due to Redis protocol restrictions.
628 $other_redis_connection->publish('info::example', 'a message here')->get;
629  
630 Returns a L<Future> which resolves to a L<Net::Async::Redis::Subscription> instance.
631  
632 =cut
633  
634 0 ▶async method psubscribe ($pattern) {
Subroutine psubscribe: not called CC=4 SCAR=30.0
Pod documented
635 0 ▶ $self->{pending_subscription_pattern_channel}{$pattern} //= $self->future('pattern_subscription[' . $pattern . ']');
Condition0%0/3
$$self{"pending_subscription_pattern_channel"}{$pattern} //= $self->future('pattern_subscription[' . $pattern . ']')
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"pending_subscription_pattern_channel"}{$pattern} //= $self->future('pattern_subscription[' . $pattern . ']')
$$self{"pending_subscription_pattern_channel"}{$pattern} $self->future('pattern_subscription[' . $pattern . ']')
Truth table0%0/3
A //= B
A= $$self{"pending_subscription_pattern_channel"}{$pattern}B= $self->future('pattern_subscription[' . $pattern . ']')
ABresult
1X1
011
000
636 0 await $self->execute_command(qw(PSUBSCRIBE), $pattern);
637 0 ▶ $self->{pubsub} //= 0;
Condition0%0/2
$$self{"pubsub"} //= 0
l!l
00
MC/DC0%0/1
$$self{"pubsub"} //= 0
$$self{"pubsub"}
Truth table0%0/2
A
A= $$self{"pubsub"}
Aresult
11
00
638 0 ▶ return $self->{subscription_pattern_channel}{$pattern} //= Net::Async::Redis::Subscription->new(
Condition0%0/3
$$self{"subscription_pattern_channel"}{$pattern} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"subscription_pattern_channel"}{$pattern} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
$$self{"subscription_pattern_channel"}{$pattern} "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
Truth table0%0/3
A //= B
A= $$self{"subscription_pattern_channel"}{$pattern}B= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $pattern)
ABresult
1X1
011
000
639 redis => $self,
640 channel => $pattern
641 )
642 14 }
643  
644 =head2 subscribe
645  
646 Subscribes to one or more channels.
647  
648 Returns a L<Future> which resolves to a L<Net::Async::Redis::Subscription> instance.
649  
650 Example:
651  
652 # Subscribe to 'notifications' channel,
653 # print the first 5 messages, then unsubscribe
654 $redis->subscribe('notifications')
655 ->then(sub {
656 my $sub = shift;
657 $sub->events
658 ->map('payload')
659 ->take(5)
660 ->say
661 ->completed
662 })->then(sub {
663 $redis->unsubscribe('notifications')
664 })->get
665  
666 =cut
667  
668 0 ▶async method subscribe (@channels) {
Subroutine subscribe: not called CC=3 SCAR=24.8
Pod documented
669 my @pending = map {
670 0 ▶ $self->{pending_subscription_channel}{$_} //= $self->future('subscription[' . $_ . ']')
Condition0%0/3
$$self{"pending_subscription_channel"}{$_} //= $self->future('subscription[' . $_ . ']')
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"pending_subscription_channel"}{$_} //= $self->future('subscription[' . $_ . ']')
$$self{"pending_subscription_channel"}{$_} $self->future('subscription[' . $_ . ']')
Truth table0%0/3
A //= B
A= $$self{"pending_subscription_channel"}{$_}B= $self->future('subscription[' . $_ . ']')
ABresult
1X1
011
000
671 } @channels;
672 0 await $self->execute_command(qw(SUBSCRIBE), @channels);
673 0 ▶ $self->{pubsub} //= 0;
Condition0%0/2
$$self{"pubsub"} //= 0
l!l
00
MC/DC0%0/1
$$self{"pubsub"} //= 0
$$self{"pubsub"}
Truth table0%0/2
A
A= $$self{"pubsub"}
Aresult
11
00
674 0 await Future->wait_all(@pending);
675 0 $log->tracef('Subscriptions established, we are go');
676 0 return @{$self->{subscription_channel}}{@channels};
677 14 }
678  
679 =head2 ssubscribe
680  
681 Subscribes to one or more sharded channels.
682  
683 This behaves similarly to L</subscribe>, but applies to messages received on a specific
684 shard. This is mostly relevant in a cluster context, where subscriptions can be localised
685 to one shard (group of nodes) in the cluster to improve performance.
686  
687 More details are in the L<sharded pubsub documentation|https://redis.io/topics/pubsub#sharded-pubsub>.
688  
689 Returns a L<Future> which resolves to a L<Net::Async::Redis::Subscription> instance.
690  
691 Example:
692  
693 # Subscribe to 'notifications' channel,
694 # print the first 5 messages, then unsubscribe
695 $redis->subscribe('notifications')
696 ->then(sub {
697 my $sub = shift;
698 $sub->events
699 ->map('payload')
700 ->take(5)
701 ->say
702 ->completed
703 })->then(sub {
704 $redis->unsubscribe('notifications')
705 })->get
706  
707 =cut
708  
709 0 ▶async method ssubscribe (@channels) {
Subroutine ssubscribe: not called CC=3 SCAR=24.8
Pod documented
710 my @pending = map {
711 0 ▶ $self->{pending_subscription_channel}{$_} //= $self->future('subscription[' . $_ . ']')
Condition0%0/3
$$self{"pending_subscription_channel"}{$_} //= $self->future('subscription[' . $_ . ']')
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"pending_subscription_channel"}{$_} //= $self->future('subscription[' . $_ . ']')
$$self{"pending_subscription_channel"}{$_} $self->future('subscription[' . $_ . ']')
Truth table0%0/3
A //= B
A= $$self{"pending_subscription_channel"}{$_}B= $self->future('subscription[' . $_ . ']')
ABresult
1X1
011
000
712 } @channels;
713 0 await $self->execute_command(qw(SSUBSCRIBE), @channels);
714 0 ▶ $self->{pubsub} //= 0;
Condition0%0/2
$$self{"pubsub"} //= 0
l!l
00
MC/DC0%0/1
$$self{"pubsub"} //= 0
$$self{"pubsub"}
Truth table0%0/2
A
A= $$self{"pubsub"}
Aresult
11
00
715 0 await Future->wait_all(@pending);
716 0 $log->tracef('Subscriptions established, we are go');
717 0 return @{$self->{subscription_channel}}{@channels};
718 14 }
719  
720 =head1 METHODS - Transactions
721  
722 =head2 multi
723  
724 Executes the given code in a Redis C<MULTI> transaction.
725  
726 This will cause each of the requests to be queued on the server, then applied in a single
727 atomic transaction.
728  
729 Note that the commands will resolve only after the transaction is committed: for example,
730 when the L</set> command is issued, Redis will return C<QUEUED>. This information
731 is B<not> used as the result - we only pass through the immediate
732 response if there was an error. The L<Future> representing
733 the response will be marked as done once the C<EXEC> command is applied and we have the
734 results back.
735  
736 Example:
737  
738 $redis->multi(sub {
739 my $tx = shift;
740 $tx->incr('some::key')->on_done(sub { print "Final value for incremented key was " . shift . "\n"; });
741 $tx->set('other::key => 'test data')
742 })->then(sub {
743 my ($success, $failure) = @_;
744 return Future->fail("Had $failure failures, expecting everything to succeed") if $failure;
745 print "$success succeeded\m";
746 return Future->done;
747 })->retain;
748  
749 =cut
750  
751 0 ▶async method multi ($code) {
Subroutine multi: not called CC=3 SCAR=24.8
Pod documented
752 0 ▶ die 'Need a coderef' unless $code and reftype($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless $code and builtin::reftype($code) eq "CODE" 0 0 0
Condition0%0/3
$code and builtin::reftype($code) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$code and builtin::reftype($code) eq "CODE"
$code builtin::reftype($code) eq "CODE"
Truth table0%0/3
A and B
A= $codeB= builtin::reftype($code) eq "CODE"
ABresult
0X0
100
111
753  
754 0 my $multi = Net::Async::Redis::Multi->new(
755 redis => $self,
756 );
757 0 await $self->execute_command(qw(MULTI));
758 0 return await $multi->exec($code);
759 14 }
760  
761 =head1 METHODS - Clientside caching
762  
763 Enable clientside caching by passing a true value for C<client_side_caching_enabled> in
764 L</configure> or L</new>. This is currently B<experimental>, and only operates on
765 L<Net::Async::Redis::Commands/get> requests.
766  
767 See L<https://redis.io/topics/client-side-caching> for more details on this feature.
768  
769 =cut
770  
771 0 ▶async method client_side_connection {
Subroutine client_side_connection: not called CC=3 SCAR=24.8
Pod undocumented
772 0 ▶ return if $self->{client_side_connection};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"client_side_connection"} 0 0 0
773  
774 0 ▶ if($self->{protocol_level} eq 'resp3') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"protocol_level"} eq "resp3") 0 0 0
775 0 $log->tracef('Client side cache uses same connection due to RESP3');
776 0 Scalar::Util::weaken($self->{client_side_connection} = $self);
777 0 await $self->enable_clientside_cache($self);
778 0 return;
779 }
780  
781 0 $log->tracef('Client side cache needs a new connection due to RESP2');
782 $self->{client_side_connection} = my $redis = ref($self)->new(
783 host => $self->host,
784 port => $self->port,
785 auth => $self->{auth},
786 0 );
787 0 $self->add_child($redis);
788 0 await $redis->connect;
789 0 await $self->enable_clientside_cache($redis);
790 0 return;
791 14 }
792  
793 =head2 clientside_cache_events
794  
795 A L<Ryu::Source> which emits key names as they are invalidated.
796  
797 With client-side caching enabled, can be used to monitor which keys
798 are changing.
799  
800 =cut
801  
802 0 ▶method clientside_cache_events {
Subroutine clientside_cache_events: not called CC=2 SCAR=17.9
Pod documented
803 0 ▶ $self->{clientside_cache_events} // die 'no client-side cache available yet'
Condition0%0/2
$$self{"clientside_cache_events"} // die("no client-side cache available yet")
l!l
00
MC/DC0%0/1
$$self{"clientside_cache_events"} // die("no client-side cache available yet")
$$self{"clientside_cache_events"}
Truth table0%0/2
A
A= $$self{"clientside_cache_events"}
Aresult
11
00
804 14 }
805  
806 =head2 client_side_cache_ready
807  
808 Returns a L<Future> representing the client-side cache connection status,
809 if there is one.
810  
811 =cut
812  
813 0 ▶method client_side_cache_ready {
Subroutine client_side_cache_ready: not called CC=2 SCAR=17.9
Pod documented
814 0 ▶ my $f = $self->{client_side_cache_ready} or return Future->fail('client-side cache is not enabled');
Branch: 1 branch
BranchTrueFalseTotal
my $f = $$self{"client_side_cache_ready"} or return "Future"->fail("client-side cache is not enabled") 0 0 0
815 0 return $f->without_cancel;
816 14 }
817  
818 =head2 client_side_cache
819  
820 Returns the L<Cache::LRU> instance used for the client-side cache.
821  
822 =cut
823  
824 0 ▶method client_side_cache {
Subroutine client_side_cache: not called CC=2 SCAR=17.9
Pod documented
825 0 ▶ $self->{client_side_cache} //= Cache::LRU->new(
Condition0%0/3
$$self{"client_side_cache"} //= "Cache::LRU"->new("size", $self->client_side_cache_size)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"client_side_cache"} //= "Cache::LRU"->new("size", $self->client_side_cache_size)
$$self{"client_side_cache"} "Cache::LRU"->new("size", $self->client_side_cache_size)
Truth table0%0/3
A //= B
A= $$self{"client_side_cache"}B= "Cache::LRU"->new("size", $self->client_side_cache_size)
ABresult
1X1
011
000
826 size => $self->client_side_cache_size,
827 );
828 14 }
829  
830 =head2 is_client_side_cache_enabled
831  
832 Returns true if the client-side cache is enabled.
833  
834 =cut
835  
836 0 ▶method is_client_side_cache_enabled { ($self->{client_side_cache_size} // 0) > 0 }
Subroutine is_client_side_cache_enabled: not called CC=2 SCAR=13.0
Pod documented
Condition0%0/2
$$self{"client_side_cache_size"} // 0
l!l
00
MC/DC0%0/1
$$self{"client_side_cache_size"} // 0
$$self{"client_side_cache_size"}
Truth table0%0/2
A
A= $$self{"client_side_cache_size"}
Aresult
11
00
837  
838 =head2 client_side_cache_size
839  
840 Returns the current client-side cache size, as a number of entries.
841  
842 =cut
843  
844 0 ▶method client_side_cache_size { $self->{client_side_cache_size} }
Subroutine client_side_cache_size: not called CC=1 SCAR=1.2
Pod documented
845  
846 # For now, we're only caching the GET/SET requests. Client-side caching
847 # support in the Redis server covers other commands, though: eventually
848 # we'll be extending this for all read commands.
849 0 ▶async method get ($k) {
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
850 0 ▶ return await $self->execute_command(qw(GET), $k) unless $self->is_client_side_cache_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_client_side_cache_enabled 0 0 0
851  
852 0 my $cache = $self->client_side_cache;
853 0 $log->tracef('Check cache for [%s]', $k);
854  
855 0 my $v = $cache->get($k);
856 0 ▶ if(ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 0 0
857 0 $log->tracef('Key [%s] already being requested, joining', $k);
858 0 return await $v->without_cancel;
859 } else {
860 0 ▶ return $v if exists $cache->{_entries}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$cache{"_entries"}{$k} 0 0 0
861 }
862  
863 0 $log->tracef('Key [%s] was not cached', $k);
864 0 my $f = $self->execute_command(qw(GET), $k);
865 # Set our cache entry regardless of whether it completes
866 # immediately or not...
867 0 $cache->set(
868 $k => $f
869 );
870 # ... and then rewrite or remove the cache entry once we get a response:
871 0 ▶ $f->on_ready(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
872 0 ▶ if($f->is_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_done) { } 0 0 0
873 # If we had an invalidation message, we may have removed
874 # our cache entry already: we shouldn't cache this value if so.
875 $cache->set($k => $f->get)
876 0 ▶ if exists $cache->{_entries}{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$cache{"_entries"}{$k} 0 0 0
877 } else {
878 # Clear up after any failure or cancellation,
879 # since they may be temporary
880 0 $cache->remove($k);
881 }
882 0 });
883 0 return await $f;
884 14 };
885  
886  
887 =head1 METHODS - Generic
888  
889 =head2 keys
890  
891 =cut
892  
893 0 ▶method keys ($match) {
Subroutine keys: not called CC=2 SCAR=17.9
Pod documented
894 0 ▶ $match //= '*';
Condition0%0/2
$match //= "*"
l!l
00
MC/DC0%0/1
$match //= "*"
$match
Truth table0%0/2
A
A= $match
Aresult
11
00
895 0 return $self->execute_command(qw(KEYS), $match);
896 14 }
897  
898 =head2 watch_keyspace
899  
900 A convenience wrapper around the keyspace notifications API.
901  
902 Provides the necessary setup to establish a C<PSUBSCRIBE> subscription
903 on the C<__keyspace@*__> namespace, setting the configuration required
904 for this to start emitting events, and then calls C<$code> with each
905 event.
906  
907 Note that this will switch the connection into pubsub mode on versions
908 of Redis older than 6.0, so it will no longer be available for any
909 other activity. This limitation does not apply on Redis 6 or above.
910  
911 Use C<*> to listen for all keyspace changes.
912  
913 Resolves to a L<Ryu::Source> instance.
914  
915 =cut
916  
917 0 ▶async method watch_keyspace ($pattern, $code) {
Subroutine watch_keyspace: not called CC=4 SCAR=30.0
Pod documented
918 0 ▶ $pattern //= '*';
Condition0%0/2
$pattern //= "*"
l!l
00
MC/DC0%0/1
$pattern //= "*"
$pattern
Truth table0%0/2
A
A= $pattern
Aresult
11
00
919 0 my $sub_name = '__keyspace@*__:' . $pattern;
920 0 ▶ $self->{have_notify} ||= await $self->config_set(
Condition0%0/3
$$self{"have_notify"} ||= <custom op await>
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"have_notify"} ||= <custom op await>
$$self{"have_notify"} <custom op await>
Truth table0%0/3
A ||= B
A= $$self{"have_notify"}B= <custom op await>
ABresult
1X1
011
000
921 'notify-keyspace-events', 'Kg$xe'
922 );
923 0 my $sub = await $self->psubscribe($sub_name);
924 0 my $ev = $sub->events;
925 0 ▶ $ev->each(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
926 0 my $message = $_;
927 0 $log->tracef('Keyspace notification for channel %s, type %s, payload %s', map $message->$_, qw(channel type payload));
928 0 my $k = $message->channel;
929 0 $k =~ s/^[^:]+://;
930 0 my $f = $code->($message->payload, $k);
931 0 ▶ $self->adopt_future($f) if blessed($f) and $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($f) and $f->isa("Future") 0 0 0
Condition0%0/3
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
932 0 ▶ }) if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
933 0 return $ev;
934 14 }
935  
936 =head2 pipeline_depth
937  
938 Number of requests awaiting responses before we start queuing.
939 This defaults to an arbitrary value of 100 requests.
940  
941 Note that this does not apply when in L<transaction|METHODS - Transactions> (C<MULTI>) mode.
942  
943 See L<https://redis.io/topics/pipelining> for more details on this concept.
944  
945 =cut
946  
947 0 ▶method pipeline_depth { $self->{pipeline_depth} //= 100 }
Subroutine pipeline_depth: not called CC=2 SCAR=13.0
Pod documented
Condition0%0/2
$$self{"pipeline_depth"} //= 100
l!l
00
MC/DC0%0/1
$$self{"pipeline_depth"} //= 100
$$self{"pipeline_depth"}
Truth table0%0/2
A
A= $$self{"pipeline_depth"}
Aresult
11
00
948  
949 =head2 opentracing
950  
951 Indicates whether L<OpenTracing::Any> support is enabled.
952  
953 =cut
954  
955 0 ▶method opentracing { $self->{opentracing} }
Subroutine opentracing: not called CC=1 SCAR=1.2
Pod documented
956  
957 =head2 opentelemetry
958  
959 Indicates whether L<OpenTelemetry> support is enabled.
960  
961 =cut
962  
963 0 ▶method opentelemetry { $self->{opentelemetry} }
Subroutine opentelemetry: not called CC=1 SCAR=1.2
Pod documented
964  
965 =head1 METHODS - Deprecated
966  
967 This are still supported, but no longer recommended.
968  
969 =cut
970  
971 0 ▶method bus {
Subroutine bus: not called CC=2 SCAR=17.9
Pod undocumented
972 0 ▶ $self->{bus} //= do {
Condition0%0/3
$$self{"bus"} //= do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"bus"} //= do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
$$self{"bus"} do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
Truth table0%0/3
A //= B
A= $$self{"bus"}B= do { require Mixin::Event::Dispatch::Bus; "Mixin::Event::Dispatch::Bus"->VERSION(2); "Mixin::Event::Dispatch::Bus"->new }
ABresult
1X1
011
000
973 0 require Mixin::Event::Dispatch::Bus;
974 0 Mixin::Event::Dispatch::Bus->VERSION(2.000);
975 0 Mixin::Event::Dispatch::Bus->new
976 }
977 14 }
978  
979 =head1 METHODS - Internal
980  
981 =cut
982  
983 =head2 on_message
984  
985 Called for each incoming message.
986  
987 Passes off the work to L</handle_pubsub_message> or the next queue
988 item, depending on whether we're dealing with subscriptions at the moment.
989  
990 =cut
991  
992 0 ▶method on_message ($data) {
Subroutine on_message: not called CC=5 SCAR=34.0
Pod documented
993 0 ▶ $log->tracef('Incoming message: %s, pending = %s', $data, join ',', map { $_->[0] } $self->{pending}->@*) if $log->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::Async::Redis::log->is_trace 0 0 0
994  
995 0 ▶ if($self->{protocol_level} eq 'resp2' and exists $self->{pubsub} and exists $SUBSCRIPTION_COMMANDS{uc $data->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"protocol_level"} eq "resp2" and exists $self->{"pubsub"} and exists $SUBSCRIPTION_COMMANDS{uc $data->[0]}) 0 0 0
Condition0%0/6
$$self{"protocol_level"} eq "resp2" and exists $self->{"pubsub"} and exists $SUBSCRIPTION_COMMANDS{uc $data->[0]}
!ll&&!rl&&r
000
$$self{"protocol_level"} eq "resp2" and exists $self->{"pubsub"}
!ll&&!rl&&r
000
MC/DC0%0/3
$$self{"protocol_level"} eq "resp2" and exists $self->{"pubsub"} and exists $SUBSCRIPTION_COMMANDS{uc $data->[0]}
$$self{"protocol_level"} eq "resp2" exists $self->{"pubsub"} exists $SUBSCRIPTION_COMMANDS{uc $data->[0]}
Truth table0%0/4
A and B and C
A= $$self{"protocol_level"} eq "resp2"B= exists $self->{"pubsub"}C= exists $SUBSCRIPTION_COMMANDS{uc $data->[0]}
ABCresult
0XX0
10X0
1100
1111
996 0 return $self->handle_pubsub_message(@$data);
997 }
998  
999 0 return $self->complete_message($data);
1000 14 }
1001  
1002 0 ▶method complete_message ($data) {
Subroutine complete_message: not called CC=4 SCAR=30.0
Pod undocumented
1003 0 ▶ my $next = shift @{$self->{pending}} or die "No pending handler";
Branch: 1 branch
BranchTrueFalseTotal
my $next = shift @{$$self{"pending"};} or die "No pending handler" 0 0 0
1004 0 ▶ $self->next_in_pipeline if @{$self->{awaiting_pipeline}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$$self{"awaiting_pipeline"};} 0 0 0
1005  
1006 # This shouldn't happen, preferably
1007 0 ▶ $log->errorf("our [%s] entry is ready, original was [%s]??", $data, $next->[0]) if $next->[1]->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $next->[1]->is_ready 0 0 0
1008 0 $next->[1]->done($data);
1009 0 return;
1010 14 }
1011  
1012 =head2 next_in_pipeline
1013  
1014 Attempt to process next pending request when in pipeline mode.
1015  
1016 =cut
1017  
1018 0 ▶method next_in_pipeline {
Subroutine next_in_pipeline: not called CC=4 SCAR=30.0
Pod documented
1019 0 my $depth = $self->pipeline_depth;
1020 0 ▶ until($depth and $self->{pending}->@* >= $depth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($depth and @{$$self{"pending"}} >= $depth) 0 0 0
Condition0%0/3
$depth and @{$$self{"pending"}} >= $depth
!ll&&!rl&&r
000
MC/DC0%0/2
$depth and @{$$self{"pending"}} >= $depth
$depth @{$$self{"pending"}} >= $depth
Truth table0%0/3
A and B
A= $depthB= @{$$self{"pending"}} >= $depth
ABresult
0X0
100
111
1021 0 ▶ return unless my $next = shift @{$self->{awaiting_pipeline}};
Branch: 1 branch
BranchTrueFalseTotal
unless my $next = shift @{$$self{"awaiting_pipeline"};} 0 0 0
1022 0 my $cmd = join ' ', @{$next->[0]};
1023 0 $log->tracef("Have free space in pipeline, sending %s", $cmd);
1024 0 push @{$self->{pending}}, [ $cmd, $next->[1] ];
1025 0 my $data = $self->wire_protocol->encode_from_client(@{$next->[0]});
1026 0 $self->stream->write($data);
1027 }
1028 # Ensure last ->write is in void context
1029 0 return;
1030 14 }
1031  
1032 =head2 on_error_message
1033  
1034 Called when there's an error response.
1035  
1036 =cut
1037  
1038 0 ▶method on_error_message ($data) {
Subroutine on_error_message: not called CC=3 SCAR=24.8
Pod documented
1039 0 $log->tracef('Incoming error message: %s', $data);
1040  
1041 0 ▶ my $next = shift @{$self->{pending}} or die "No pending handler";
Branch: 1 branch
BranchTrueFalseTotal
my $next = shift @{$$self{"pending"};} or die "No pending handler" 0 0 0
1042 0 $next->[1]->fail($data);
1043 0 ▶ $self->next_in_pipeline if @{$self->{awaiting_pipeline}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$$self{"awaiting_pipeline"};} 0 0 0
1044 0 return;
1045 14 }
1046  
1047 =head2 handle_pubsub_message
1048  
1049 Deal with an incoming pubsub-related message.
1050  
1051 =cut
1052  
1053 0 ▶method handle_pubsub_message ($type, @details) {
Subroutine handle_pubsub_message: not called CC=10 SCAR=47.0
Pod documented
1054 0 $type = lc $type;
1055 0 ▶ if($type eq 'message' or $type eq 'smessage') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "message" or $type eq "smessage") 0 0 0
Condition0%0/3
$type eq "message" or $type eq "smessage"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "message" or $type eq "smessage"
$type eq "message" $type eq "smessage"
Truth table0%0/3
A or B
A= $type eq "message"B= $type eq "smessage"
ABresult
1X1
011
000
1056 0 my ($channel, $payload) = @details;
1057 0 ▶ if(my $sub = $self->{subscription_channel}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $$self{'subscription_channel'}{$channel}) { } 0 0 0
1058 0 my $msg = Net::Async::Redis::Subscription::Message->new(
1059 type => $type,
1060 channel => $channel,
1061 payload => $payload,
1062 redis => $self,
1063 subscription => $sub
1064 );
1065 0 $sub->events->emit($msg);
1066 } else {
1067 0 $log->warnf('Have message for unknown channel [%s]', $channel);
1068 }
1069 0 ▶ $self->bus->invoke_event(message => [ $type, $channel, $payload ]) if exists $self->{bus};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"bus"} 0 0 0
1070 0 return;
1071 }
1072 0 ▶ if($type eq 'pmessage') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "pmessage") 0 0 0
1073 0 my ($pattern, $channel, $payload) = @details;
1074 0 ▶ if(my $sub = $self->{subscription_pattern_channel}{$pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $$self{'subscription_pattern_channel'}{$pattern}) { } 0 0 0
1075 0 my $msg = Net::Async::Redis::Subscription::Message->new(
1076 type => $type,
1077 pattern => $pattern,
1078 channel => $channel,
1079 payload => $payload,
1080 redis => $self,
1081 subscription => $sub
1082 );
1083 0 $sub->events->emit($msg);
1084 } else {
1085 0 $log->warnf('Have message for unknown channel [%s]', $channel);
1086 }
1087 0 ▶ $self->bus->invoke_event(message => [ $type, $channel, $payload ]) if exists $self->{bus};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"bus"} 0 0 0
1088 0 return;
1089 }
1090 0 ▶ if($type =~ /invalidate$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /invalidate$/u) 0 0 0
1091 0 my ($channel) = @details;
1092 0 for my $k ($channel->@*) {
1093 0 $log->tracef('have invalidation type %s with channel %s', $type, $k);
1094 0 $self->clientside_cache_events->emit($k);
1095 }
1096 0 return;
1097 }
1098  
1099 # Looks like this isn't a message, it's a response to (un)subscribe
1100 0 return $self->handle_pubsub_response($type, @details);
1101 14 }
1102  
1103 0 ▶method handle_pubsub_response ($type, @details) {
Subroutine handle_pubsub_response: not called CC=9 SCAR=45.0
Pod undocumented
1104 0 my ($channel, $payload) = @details;
1105 0 $type = lc $type;
1106 0 ▶ my $k = (substr $type, 0, 1) eq 'p' ? 'subscription_pattern_channel' : 'subscription_channel';
Branch: 1 branch
BranchTrueFalseTotal
substr($type, 0, 1) eq 'p' ? : 0 0 0
1107 0 ▶ if($type =~ /unsubscribe$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /unsubscribe$/u) { } 0 0 0
elsif ($type =~ /subscribe$/u) { } 0 0 0
1108 0 --$self->{pubsub};
1109 0 ▶ if(my $sub = delete $self->{$k}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = delete $$self{$k}{$channel}) { } 0 0 0
1110 0 $log->tracef('Removed subscription for [%s]', $channel);
1111 } else {
1112 0 $log->warnf('Have unsubscription for unknown channel [%s]', $channel);
1113 }
1114 } elsif($type =~ /subscribe$/) {
1115 0 ▶ $log->tracef('Have %s subscription for [%s]', (exists $self->{$k}{$channel} ? 'existing' : 'new'), $channel);
Branch: 1 branch
BranchTrueFalseTotal
exists $$self{$k}{$channel} ? : 0 0 0
1116 0 ++$self->{pubsub};
1117 0 ▶ $self->{$k}{$channel} //= Net::Async::Redis::Subscription->new(
Condition0%0/3
$$self{$k}{$channel} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{$k}{$channel} //= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
$$self{$k}{$channel} "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
Truth table0%0/3
A //= B
A= $$self{$k}{$channel}B= "Net::Async::Redis::Subscription"->new("redis", $self, "channel", $channel)
ABresult
1X1
011
000
1118 redis => $self,
1119 channel => $channel
1120 );
1121 0 ▶ $self->{'pending_' . $k}{$channel}->done($payload) unless $self->{'pending_' . $k}{$channel}->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{'pending_' . $k}{$channel}->is_done 0 0 0
1122 } else {
1123 0 $log->warnf('have unknown pubsub message type %s with channel %s payload %s', $type, $channel, $payload);
1124 }
1125  
1126 0 ▶ return $self->complete_message([ $type, @details ]) unless $self->{protocol_level} eq 'resp2';
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"protocol_level"} eq "resp2" 0 0 0
1127 14 }
1128  
1129 =head2 stream
1130  
1131 Represents the L<IO::Async::Stream> instance for the active Redis connection.
1132  
1133 =cut
1134  
1135 0 ▶method stream { $self->{stream} }
Subroutine stream: not called CC=1 SCAR=1.2
Pod documented
1136  
1137 =head2 notify_close
1138  
1139 Called when the socket is closed.
1140  
1141 =cut
1142  
1143 0 ▶method notify_close {
Subroutine notify_close: not called CC=7 SCAR=40.3
Pod documented
1144 # Also clear our connection future so that the next request is triggered appropriately
1145 0 my $conn = delete $self->{connection};
1146 0 ▶ $self->{connection}->fail if $self->{connection} and not $self->{connection}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"connection"} and not $$self{"connection"}->is_ready 0 0 0
Condition0%0/3
$$self{"connection"} and not $$self{"connection"}->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$$self{"connection"} and not $$self{"connection"}->is_ready
$$self{"connection"} not $$self{"connection"}->is_ready
Truth table0%0/3
A and not(B)
A= $$self{"connection"}B= not $$self{"connection"}->is_ready
ABresult
0X0
100
111
1147  
1148 # If we think we have an existing connection, it needs removing:
1149 # there's no guarantee that it's in a usable state.
1150 0 ▶ if(my $stream = delete $self->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stream = delete $self->{"stream"}) 0 0 0
1151 0 $stream->close_now;
1152 }
1153  
1154 # Clear out anything in the pending queue - we normally wouldn't expect anything to
1155 # have ready status here, but no sense failing on a failure. Note that we aren't
1156 # filtering out the list via grep because some of these Futures may be interdependent.
1157 $_->[1]->fail(
1158 'Server connection is no longer active',
1159 redis => 'disconnected'
1160 0 ) for grep { !$_->[1]->is_ready } splice @{$self->{pending}};
1161  
1162 # Subscriptions also need clearing up
1163 0 $_->cancel for values %{$self->{subscription_channel}};
1164 0 $self->{subscription_channel} = {};
1165 0 $_->cancel for values %{$self->{subscription_pattern_channel}};
1166 0 $self->{subscription_pattern_channel} = {};
1167  
1168 0 $self->maybe_invoke_event(disconnect => );
1169 14 }
1170  
1171 =head2 command_label
1172  
1173 Generate a label for the given command list.
1174  
1175 =cut
1176  
1177 0 ▶method command_label (@cmd) {
Subroutine command_label: not called CC=2 SCAR=17.9
Pod documented
1178 0 ▶ return join ' ', @cmd if $cmd[0] eq 'KEYS';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd[0] eq "KEYS" 0 0 0
1179 0 return $cmd[0];
1180 14 }
1181  
1182 =head2 span_for_future
1183  
1184 See L<https://opentelemetry.io/docs/specs/semconv/database/redis/> for current semantic conventions around Redis.
1185  
1186 =cut
1187  
1188 0 ▶method span_for_future ($f, %args) {
Subroutine span_for_future: not called CC=3 SCAR=24.8
Pod documented
1189 0 return $f unless OPENTELEMETRY_ENABLED;
1190  
1191 0 ▶ $args{name} //= $f->label // 'Future';
Condition0%0/4
$args{"name"} //= $f->label // "Future"
l!l
00
$f->label // "Future"
l!l
00
MC/DC0%0/1
$args{"name"} //= $f->label // "Future"
$args{"name"}
Truth table0%0/2
A
A= $args{"name"}
Aresult
11
00
1192 0 my $span = $tracer->create_span(
1193 %args,
1194 parent => OpenTelemetry::Context->current
1195 );
1196  
1197 0 my $context = OpenTelemetry::Trace->context_with_span($span);
1198  
1199 0 ▶ return $f->on_ready(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1200 0 dynamically OpenTelemetry::Context->current = $context;
1201 0 ▶ if($f->is_done) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f->is_done) { } 0 0 0
elsif ($f->is_cancelled) { } 0 0 0
1202 0 $span->set_status(
1203 OpenTelemetry::Constants->SPAN_STATUS_OK
1204 );
1205 } elsif($f->is_cancelled) {
1206 0 $span->set_status(
1207 OpenTelemetry::Constants->SPAN_STATUS_OK
1208 );
1209 } else {
1210 0 my $e = $f->failure;
1211 0 $span->record_exception($e);
1212 0 $span->set_status(
1213 OpenTelemetry::Constants->SPAN_STATUS_ERROR,
1214 $e
1215 );
1216 }
1217 0 $span->end;
1218 0 });
1219 14 }
1220  
1221 =head2 execute_command
1222  
1223 Queues the given command for execution.
1224  
1225 =cut
1226  
1227 0 ▶method execute_command (@cmd) {
Subroutine execute_command: not called CC=4 SCAR=30.0
Pod documented
1228 # This represents the completion of the command
1229 0 my $f = $self->loop->new_future->set_label(
1230 $self->command_label(@cmd)
1231 );
1232 0 ▶ $tracer->span_for_future($f) if $self->opentracing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->opentracing 0 0 0
1233 $self->span_for_future(
1234 $f,
1235 attributes => {
1236 'db.system' => 'redis',
1237 'db.redis.database_index' => $self->database,
1238 0 'db.statement' => join(' ', map { /\s/ ? qq{"$_"} : $_ } @cmd),
1239 }
1240 ) if OPENTELEMETRY_ENABLED && $self->opentelemetry;
1241  
1242 0 my $item = [ \@cmd, $f];
1243 0 ▶ if($self->{connection_in_progress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"connection_in_progress"}) 0 0 0
1244 0 $self->handle_command($item);
1245 0 return $f->retain;
1246 }
1247 0 ▶ my $queue = $self->{_is_multi} // $self->command_queue;
Condition0%0/3
$$self{"_is_multi"} // $self->command_queue
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"_is_multi"} // $self->command_queue
$$self{"_is_multi"} $self->command_queue
Truth table0%0/3
A // B
A= $$self{"_is_multi"}B= $self->command_queue
ABresult
1X1
011
000
1248 # We register this as a command we want to run - it'll be
1249 # sent to the server once nothing else is in the way.
1250 # We currently don't support cancellation - any attempt to
1251 # do so will be ignored, the command will still be executed.
1252 0 ▶ return $queue->push($item)->then(sub { $f })->retain->without_cancel;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1253 14 }
1254  
1255 0 ▶method command_queue {
Subroutine command_queue: not called CC=4 SCAR=30.0
Pod undocumented
1256 0 ▶ return $self->{command_queue} if $self->{command_queue};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"command_queue"} 0 0 0
1257 0 ▶ $self->{command_queue} = my $queue = Future::Queue->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->pipeline_depth ? : 0 0 0
1258 (
1259 $self->pipeline_depth
1260 ? (max_items => $self->pipeline_depth)
1261 : ()
1262 ),
1263 prototype => $self->future
1264 );
1265 0 ▶ $self->{command_processing} ||= $self->command_processing->on_ready(sub { delete $self->{command_processing} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$$self{"command_processing"} ||= $self->command_processing->on_ready(sub { delete $self->{"command_processing"}; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"command_processing"} ||= $self->command_processing->on_ready(sub { delete $self->{"command_processing"}; } )
$$self{"command_processing"} $self->command_processing->on_ready(sub { delete $self->{"command_processing"}; } )
Truth table0%0/3
A ||= B
A= $$self{"command_processing"}B= $self->command_processing->on_ready(sub { delete $self->{"command_processing"}; } )
ABresult
1X1
011
000
1266 0 return $queue;
1267 14 }
1268  
1269 0 ▶async method command_processing {
Subroutine command_processing: not called CC=4 SCAR=30.0
Pod undocumented
1270 0 while(1) {
1271 0 await $self->connected;
1272  
1273 # An active MULTI always takes priority over regular commands
1274 0 ▶ if(my $queue = $self->{multi_queue}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $queue = $$self{'multi_queue'}) { } 0 0 0
1275 try {
1276 0 ▶ while(my $next = await $queue->shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = <custom op await>) 0 0 0
1277 0 $self->handle_command($next);
1278 }
1279 0 } catch ($e) {
1280 0 $log->errorf('Failed processing MULTI commands - %s', $e);
1281 0 delete $self->{multi_queue};
1282 0 die $e;
1283 }
1284 0 delete $self->{multi_queue};
1285 } else {
1286 0 my $queue = $self->{command_queue};
1287 0 ▶ my $next = await $queue->shift
Branch: 1 branch
BranchTrueFalseTotal
my $next = <custom op await> or last 0 0 0
1288 or last;
1289 0 $self->handle_command($next);
1290 }
1291 }
1292 14 }
1293  
1294 0 ▶method handle_command ($details) {
Subroutine handle_command: not called CC=9 SCAR=45.0
Pod undocumented
1295 0 my @cmd = $details->[0]->@*;
1296 0 my $f = $details->[1];
1297 0 ▶ if($f->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_ready) 0 0 0
1298 0 $log->tracef('Ignoring command %s since it is marked as ', \@cmd, $f->state);
1299 0 return $f;
1300 }
1301  
1302 # First, the rules: pubsub or plain
1303 my $is_sub_command = (
1304 0 ▶ $self->{protocol_level} eq 'resp2' and exists $SUBSCRIPTION_COMMANDS{$cmd[0]}
Condition0%0/3
$$self{'protocol_level'} eq 'resp2' && exists $SUBSCRIPTION_COMMANDS{$cmd[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$$self{'protocol_level'} eq 'resp2' && exists $SUBSCRIPTION_COMMANDS{$cmd[0]}
$$self{'protocol_level'} eq 'resp2' exists $SUBSCRIPTION_COMMANDS{$cmd[0]}
Truth table0%0/3
A && B
A= $$self{'protocol_level'} eq 'resp2'B= exists $SUBSCRIPTION_COMMANDS{$cmd[0]}
ABresult
0X0
100
111
1305 );
1306  
1307 return $f->fail(
1308 'Currently in pubsub mode, cannot send regular commands until unsubscribed',
1309 redis =>
1310 0 0 + (keys %{$self->{subscription_channel}}),
1311 0 0 + (keys %{$self->{subscription_pattern_channel}})
1312 0 ▶ ) if $self->{protocol_level} ne 'resp3' and exists $self->{pubsub} and not exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"protocol_level"} ne "resp3" and exists $self->{"pubsub"} and not exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]} 0 0 0
Condition0%0/6
$$self{"protocol_level"} ne "resp3" and exists $self->{"pubsub"} and not exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]}
!ll&&!rl&&r
000
$$self{"protocol_level"} ne "resp3" and exists $self->{"pubsub"}
!ll&&!rl&&r
000
MC/DC0%0/3
$$self{"protocol_level"} ne "resp3" and exists $self->{"pubsub"} and not exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]}
$$self{"protocol_level"} ne "resp3" exists $self->{"pubsub"} not exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]}
Truth table0%0/4
A and B and not(C)
A= $$self{"protocol_level"} ne "resp3"B= exists $self->{"pubsub"}C= not exists $ALLOWED_SUBSCRIPTION_COMMANDS{$cmd[0]}
ABCresult
0XX0
10X0
1100
1111
1313  
1314 0 my $cmd = join ' ', @cmd;
1315  
1316 0 $log->tracef('Outgoing [%s]', $cmd);
1317 0 my $data = $self->wire_protocol->encode_from_client(@cmd);
1318 0 push @{$self->{pending}}, [ $cmd, $f ];
1319  
1320 # Void-context write allows IaStream to combine multiple writes on the same connection.
1321 0 $self->stream->write($data);
1322 0 ▶ if(lc($cmd[0]) eq 'multi') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $cmd[0] eq "multi") 0 0 0
1323 0 ▶ die 'Already processing MULTI, cannot start a new one' if $self->{multi_queue};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"multi_queue"} 0 0 0
1324 0 ▶ $self->{multi_queue} = Future::Queue->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->pipeline_depth ? : 0 0 0
1325 (
1326 $self->pipeline_depth
1327 ? (max_items => $self->pipeline_depth)
1328 : ()
1329 ),
1330 prototype => $self->future
1331 );
1332 }
1333 0 return;
1334 14 }
1335  
1336 0 ▶async method xread (@args) {
Subroutine xread: not called CC=4 SCAR=30.0
Pod documented
1337 0 my $response = await $self->execute_command(qw(XREAD), @args);
1338 0 ▶ return [] unless ref $response;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response 0 0 0
1339  
1340 # protocol_level is detected while connecting checking before this point is wrong.
1341 0 ▶ return $response if $self->{protocol_level} eq 'resp2' || $self->{hashrefs};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"protocol_level"} eq "resp2" or $$self{"hashrefs"} 0 0 0
Condition0%0/3
$$self{"protocol_level"} eq "resp2" or $$self{"hashrefs"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"protocol_level"} eq "resp2" or $$self{"hashrefs"}
$$self{"protocol_level"} eq "resp2" $$self{"hashrefs"}
Truth table0%0/3
A or B
A= $$self{"protocol_level"} eq "resp2"B= $$self{"hashrefs"}
ABresult
1X1
011
000
1342  
1343 0 ▶ my $compatible_response = [ pairmap { [ $a, $b ] } $response->@* ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
1344 0 $log->tracef('Transformed response of xread/xreadgroup into RESP2 format: from %s to %s', $response, $compatible_response);
1345  
1346 0 return $compatible_response;
1347 14 }
1348  
1349 0 ▶async method xreadgroup (@args) {
Subroutine xreadgroup: not called CC=4 SCAR=30.0
Pod documented
1350 0 my $response = await $self->execute_command(qw(XREADGROUP), @args);
1351 0 ▶ return [] unless ref $response;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response 0 0 0
1352  
1353 # protocol_level is detected while connecting checking before this point is wrong.
1354 0 ▶ return $response if $self->{protocol_level} eq 'resp2' || $self->{hashrefs};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"protocol_level"} eq "resp2" or $$self{"hashrefs"} 0 0 0
Condition0%0/3
$$self{"protocol_level"} eq "resp2" or $$self{"hashrefs"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"protocol_level"} eq "resp2" or $$self{"hashrefs"}
$$self{"protocol_level"} eq "resp2" $$self{"hashrefs"}
Truth table0%0/3
A or B
A= $$self{"protocol_level"} eq "resp2"B= $$self{"hashrefs"}
ABresult
1X1
011
000
1355  
1356 0 ▶ my $compatible_response = [ pairmap { [ $a, $b ] } $response->@* ];
Subroutine __ANON__: not called CC=1 SCAR=6.9
1357 0 $log->tracef('Transformed response of xread/xreadgroup into RESP2 format: from %s to %s', $response, $compatible_response);
1358  
1359 0 return $compatible_response;
1360 14 }
1361  
1362 # These have different behaviours depending on whether we use the RESP3
1363 # data structures (hashes etc.) or original RESP2 everything-is-an-array.
1364 #for my $method (qw(zrange zrangebyscore zrevrange zrevrangebyscore)) {
1365 # around $method => async sub {
1366 # my ($code, $self, @args) = @_;
1367 # return await $self->$code(@args) if $self->{hashrefs};
1368 #
1369 # my $response = await $self->$code(@args);
1370 # return $response unless ref $response->[0] eq 'ARRAY';
1371 #
1372 # my $compatible_response = [ map { $_->@* } $response->@* ];
1373 # $log->tracef(
1374 # 'Transformed response of %s into RESP2 format: from %s, to %s',
1375 # $method,
1376 # $response,
1377 # $compatible_response
1378 # );
1379 #
1380 # return $compatible_response;
1381 # };
1382 #}
1383  
1384 =head2 ryu
1385  
1386 A L<Ryu::Async> instance for source/sink creation.
1387  
1388 =cut
1389  
1390 0 ▶method ryu {
Subroutine ryu: not called CC=2 SCAR=17.9
Pod documented
1391 0 ▶ $self->{ryu} ||= do {
Condition0%0/3
$$self{"ryu"} ||= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"ryu"} ||= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
$$self{"ryu"} do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
Truth table0%0/3
A ||= B
A= $$self{"ryu"}B= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
ABresult
1X1
011
000
1392 0 $self->add_child(
1393 my $ryu = Ryu::Async->new
1394 );
1395 0 $ryu
1396 }
1397 14 }
1398  
1399 =head2 future
1400  
1401 Factory method for creating new L<Future> instances.
1402  
1403 =cut
1404  
1405 0 ▶method future (@args) {
Subroutine future: not called CC=1 SCAR=6.9
Pod documented
1406 0 return $self->loop->new_future(@args);
1407 14 }
1408  
1409 =head2 wire_protocol
1410  
1411 Returns the L<Net::Async::Redis::Protocol> instance used for
1412 encoding and decoding messages.
1413  
1414 =cut
1415  
1416 0 ▶method wire_protocol {
Subroutine wire_protocol: not called CC=3 SCAR=24.8
Pod documented
1417 0 ▶ $self->{wire_protocol} ||= do {
Condition0%0/3
$$self{"wire_protocol"} ||= do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "pubsub", $self->curry::weak::handle_pubsub_message, "error", $self->curry::weak::on_error_message, "protocol", $$self{"protocol_level"} || "resp3") }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"wire_protocol"} ||= do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "pubsub", $self->curry::weak::handle_pubsub_message, "error", $self->curry::weak::on_error_message, "protocol", $$self{"protocol_level"} || "resp3") }
$$self{"wire_protocol"} do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "pubsub", $self->curry::weak::handle_pubsub_message, "error", $self->curry::weak::on_error_message, "protocol", $$self{"protocol_level"} || "resp3") }
Truth table0%0/3
A ||= B
A= $$self{"wire_protocol"}B= do { require Net::Async::Redis::Protocol; "Net::Async::Redis::Protocol"->new("handler", $self->curry::weak::on_message, "pubsub", $self->curry::weak::handle_pubsub_message, "error", $self->curry::weak::on_error_message, "protocol", $$self{"protocol_level"} || "resp3") }
ABresult
1X1
011
000
1418 0 require Net::Async::Redis::Protocol;
1419 Net::Async::Redis::Protocol->new(
1420 handler => $self->curry::weak::on_message,
1421 pubsub => $self->curry::weak::handle_pubsub_message,
1422 error => $self->curry::weak::on_error_message,
1423 0 ▶ protocol => $self->{protocol_level} || 'resp3',
Condition0%0/2
$$self{'protocol_level'} || 'resp3'
l!l
00
MC/DC0%0/1
$$self{'protocol_level'} || 'resp3'
$$self{'protocol_level'}
Truth table0%0/2
A
A= $$self{'protocol_level'}
Aresult
11
00
1424 )
1425 };
1426 14 }
1427  
1428 =head2 enable_clientside_cache
1429  
1430 Used internally to prepare for client-side caching: subscribes to the
1431 invalidation events.
1432  
1433 =cut
1434  
1435 0 ▶async method enable_clientside_cache ($redis) {
Subroutine enable_clientside_cache: not called CC=3 SCAR=24.8
Pod documented
1436 0 my $f = $self->{client_side_cache_ready} = $self->loop->new_future;
1437 try {
1438 0 $log->tracef('At this point we want an ID');
1439 0 my $id = await $redis->client_id;
1440 0 $log->tracef('Set up :invalidate sub');
1441 0 my $sub = await $redis->subscribe('__redis__:invalidate');
1442 0 $log->tracef('We now have :invalidate');
1443 0 $self->{clientside_cache_events} = $sub->events;
1444 0 ▶ $sub->events->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1445 0 $log->tracef('Invalidating key %s', $_);
1446 0 $self->client_side_cache->remove($_);
1447 0 });
1448  
1449 # In a multi-connection setup, e.g. RESP2, the notifications need to be
1450 # delivered to the subscription connection via `REDIRECT`
1451 0 ▶ my @args = refaddr($self) != refaddr($redis)
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($self) != builtin::refaddr($redis) ? : 0 0 0
1452 ? (redirect => $id)
1453 : ();
1454 0 $log->tracef('Enable client tracking via %s', $self->can('client_tracking'));
1455 0 await $self->client_tracking('on', @args);
1456 0 $f->done;
1457 0 } catch($e) {
1458 0 ▶ $f->fail($e) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
1459 0 die $e;
1460 }
1461 0 return;
1462 14 }
1463  
1464 =head2 _init
1465  
1466  
1467  
1468 =cut
1469  
1470 0 ▶method _init (@args) {
Subroutine _init: not called CC=5 SCAR=34.0
1471 0 ▶ $self->{protocol_level} //= 'resp2';
Condition0%0/2
$$self{"protocol_level"} //= "resp2"
l!l
00
MC/DC0%0/1
$$self{"protocol_level"} //= "resp2"
$$self{"protocol_level"}
Truth table0%0/2
A
A= $$self{"protocol_level"}
Aresult
11
00
1472 0 ▶ $self->{pending_multi} //= [];
Condition0%0/2
$$self{"pending_multi"} //= []
l!l
00
MC/DC0%0/1
$$self{"pending_multi"} //= []
$$self{"pending_multi"}
Truth table0%0/2
A
A= $$self{"pending_multi"}
Aresult
11
00
1473 0 ▶ $self->{pending} //= [];
Condition0%0/2
$$self{"pending"} //= []
l!l
00
MC/DC0%0/1
$$self{"pending"} //= []
$$self{"pending"}
Truth table0%0/2
A
A= $$self{"pending"}
Aresult
11
00
1474 0 ▶ $self->{awaiting_pipeline} //= [];
Condition0%0/2
$$self{"awaiting_pipeline"} //= []
l!l
00
MC/DC0%0/1
$$self{"awaiting_pipeline"} //= []
$$self{"awaiting_pipeline"}
Truth table0%0/2
A
A= $$self{"awaiting_pipeline"}
Aresult
11
00
1475 0 $self->{opentracing} = OPENTRACING_ENABLED;
1476 0 $self->next::method(@args);
1477 14 }
1478  
1479 =head2 _add_to_loop
1480  
1481  
1482  
1483 =cut
1484  
1485 0 ▶method _add_to_loop ($loop) {
Subroutine _add_to_loop: not called CC=1 SCAR=6.9
1486 0 delete $self->{client_side_connection};
1487 14 }
1488  
1489 =head2 retrieve_full_command_list
1490  
1491 Iterates through all commands defined in Redis, extracting the information about
1492 that command using C<COMMAND INFO>.
1493  
1494 The data is formatted for internal use, converting information such as flags
1495 into hashrefs for easier lookup.
1496  
1497 This information is also used by L</extract_keys_from_command>.
1498  
1499 Returns a hashref, where each key represents a method name (space-separated
1500 commands such as C<CLUSTER NODES> are returned as C<cluster_nodes>). The values
1501 are a restructured form of L<https://redis.io/commands/command>.
1502  
1503 =cut
1504  
1505 0 ▶async method retrieve_full_command_list {
Subroutine retrieve_full_command_list: not called CC=8 SCAR=42.8
Pod documented
1506 0 my %data;
1507 0 my $commands = await $self->command_list;
1508 0 for my $command_name ($commands->@*) {
1509 0 my $method_name = $command_name =~ s/\|/_/gr;
1510 0 my ($info) = (await $self->command_info($command_name))->@*;
1511 0 my ($name, $arity, $flags, $first_key, $last_key, $step, $acl_cat, $tips, $key_spec, $subcommands) = $info->@*;
1512 0 $flags = +{ map { $_ => 1 } $flags->@* };
1513 0 $acl_cat = +{ map { $_ => 1 } $acl_cat->@* };
1514 0 $tips = +{ map { $_ => 1 } $tips->@* };
1515 0 my @key_specs;
1516 0 for my $ks ($key_spec->@*) {
1517 0 $key_spec = +{ $ks->@* };
1518 0 ▶ $key_spec->{flags} &&= +{ map { $_ => 1 } $key_spec->{flags}->@* };
Condition0%0/2
$$key_spec{"flags"} &&= {map({$_, 1;} @{$$key_spec{"flags"}})}
!ll
00
MC/DC0%0/1
$$key_spec{"flags"} &&= {map({$_, 1;} @{$$key_spec{"flags"}})}
$$key_spec{"flags"}
Truth table0%0/2
A
A= $$key_spec{"flags"}
Aresult
00
11
1519 0 ▶ $key_spec->{begin_search} &&= +{ $key_spec->{begin_search}->@* };
Condition0%0/2
$$key_spec{"begin_search"} &&= {@{$$key_spec{"begin_search"}}}
!ll
00
MC/DC0%0/1
$$key_spec{"begin_search"} &&= {@{$$key_spec{"begin_search"}}}
$$key_spec{"begin_search"}
Truth table0%0/2
A
A= $$key_spec{"begin_search"}
Aresult
00
11
1520 0 ▶ $key_spec->{begin_search}{spec} &&= +{ $key_spec->{begin_search}{spec}->@* };
Condition0%0/2
$$key_spec{"begin_search"}{"spec"} &&= {@{$$key_spec{"begin_search"}{"spec"}}}
!ll
00
MC/DC0%0/1
$$key_spec{"begin_search"}{"spec"} &&= {@{$$key_spec{"begin_search"}{"spec"}}}
$$key_spec{"begin_search"}{"spec"}
Truth table0%0/2
A
A= $$key_spec{"begin_search"}{"spec"}
Aresult
00
11
1521 0 ▶ $key_spec->{find_keys} &&= +{ $key_spec->{find_keys}->@* };
Condition0%0/2
$$key_spec{"find_keys"} &&= {@{$$key_spec{"find_keys"}}}
!ll
00
MC/DC0%0/1
$$key_spec{"find_keys"} &&= {@{$$key_spec{"find_keys"}}}
$$key_spec{"find_keys"}
Truth table0%0/2
A
A= $$key_spec{"find_keys"}
Aresult
00
11
1522 0 ▶ $key_spec->{find_keys}{spec} &&= +{ $key_spec->{find_keys}{spec}->@* };
Condition0%0/2
$$key_spec{"find_keys"}{"spec"} &&= {@{$$key_spec{"find_keys"}{"spec"}}}
!ll
00
MC/DC0%0/1
$$key_spec{"find_keys"}{"spec"} &&= {@{$$key_spec{"find_keys"}{"spec"}}}
$$key_spec{"find_keys"}{"spec"}
Truth table0%0/2
A
A= $$key_spec{"find_keys"}{"spec"}
Aresult
00
11
1523 0 push @key_specs, $key_spec;
1524 }
1525  
1526 0 $data{$method_name} = {
1527 name => $name,
1528 arity => $arity,
1529 flags => $flags,
1530 first_key => $first_key,
1531 last_key => $last_key,
1532 step => $step,
1533 acl_cat => $acl_cat,
1534 tips => $tips,
1535 key_spec => \@key_specs,
1536 subcommands => $subcommands
1537 };
1538 }
1539 0 return \%data;
1540 14 }
1541  
1542 =head2 extract_keys_for_command
1543  
1544 Given a command arrayref and a definition for the server, this will
1545 return a list of any keys found in that command.
1546  
1547 Since the logic for this is slightly slow, we are caching the result
1548 unless a specific definition is provided: this is an internal implementation
1549 detail and not something to rely on.
1550  
1551 (the key specification is a relatively new Redis feature - an optimised version
1552 of this logic will be added to L<Net::Async::Redis::XS> in due course, which
1553 should reduce the need for caching)
1554  
1555 Returns a list of keys.
1556  
1557 =cut
1558  
1559 14 my $keyspec_cache = Cache::LRU->new(
1560 size => 10_000
1561 );
1562  
1563 676 ▶sub extract_keys_for_command ($class, $command, $def = undef) {
Subroutine extract_keys_for_command: called CC=30 SCAR=40.5
Pod documented
1564 676 my $cached = $keyspec_cache->get(join "\x{01FF}", $command->@*);
1565 676 ▶ return $cached->@* if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 277 399 676
1566  
1567 399 my $info = $class->extract_spec_for_command($command, $def);
1568 # Commands can have zero or more keyspecs which tell us where to find the key information. Each
1569 # command may have multiple keyspec definitions.
1570 399 my @keys;
1571  
1572 399 my @components = $command->@*;
1573 399 my $cmd = join ' ', $command->@*;
1574  
1575 KEYSPEC:
1576 399 for my $key_spec ($info->{key_spec}->@*) {
1577 my $type = $key_spec->{begin_search}{type}
1578 409 ▶ or next KEYSPEC;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $$key_spec{"begin_search"}{"type"} or next KEYSPEC 409 0 409
1579  
1580 # Instead of trying to track index and offset, we work with a copy of the data - less efficient,
1581 # but easier to get accurate results.
1582 409 my @target = @components;
1583  
1584 # Find the starting index
1585 match($type : eq) {
1586 case('index') {
1587 397 ▶ splice @target, 0, $key_spec->{begin_search}{spec}{index} if $key_spec->{begin_search}{spec}{index};
Branch: 2 branches
BranchTrueFalseTotal
if $$key_spec{"begin_search"}{"spec"}{"index"} 397 0 397
$(Syntax::Keyword::Match/topic) eq 'keyword' ? : 12 0 12
1588 }
1589 case('keyword') {
1590 12 my $target = $key_spec->{begin_search}{spec}{keyword};
1591 12 my $start = $key_spec->{begin_search}{spec}{startfrom};
1592 12 ▶ if($start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) { } 0 12 12
1593 0 ▶ splice @target, $start, -$#target if $start < -1;
Branch: 1 branch
BranchTrueFalseTotal
if $start < -1 0 0 0
1594 0 ▶ pop @target while @target and uc($target[-1]) ne $target;
Branch: 1 branch
BranchTrueFalseTotal
@target and uc $target[-1] ne $target and do { pop @target } 0 0 0
Condition0%0/3
@target and uc $target[-1] ne $target
!ll&&!rl&&r
000
MC/DC0%0/2
@target and uc $target[-1] ne $target
@target uc $target[-1] ne $target
Truth table0%0/3
A and B
A= @targetB= uc $target[-1] ne $target
ABresult
0X0
100
111
1595 0 ▶ next KEYSPEC unless @target;
Branch: 1 branch
BranchTrueFalseTotal
unless @target 0 0 0
1596 0 @target = reverse @target;
1597 } else {
1598 12 ▶ splice @target, 0, $start - 1 if $start > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $start > 1 12 0 12
1599 12 ▶ shift @target while @target and uc($target[0]) ne $target;
Branch: 1 branch
BranchTrueFalseTotal
@target and uc $target[0] ne $target and do { shift @target } 20 12 32
Condition66%2/3
@target and uc $target[0] ne $target
!ll&&!rl&&r
12020
MC/DC50%1/2
@target and uc $target[0] ne $target
@target uc $target[0] ne $target
Truth table66%2/3
A and B
A= @targetB= uc $target[0] ne $target
ABresult
0X0
100
111
1600 12 ▶ next KEYSPEC unless @target;
Branch: 1 branch
BranchTrueFalseTotal
unless @target 12 0 12
1601 0 ▶ shift @target;
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'unknown' ? : 0 0 0
1602 }
1603 }
1604 case('unknown') {
1605 0 die 'Unknown key specification for command ' . $cmd;
1606 }
1607 409 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'index' ? : 397 12 409
1608 0 die 'No key specification for command ' . $cmd;
1609 }
1610 }
1611  
1612 # Find the keys, starting from the index identified above
1613 match($key_spec->{find_keys}{type} : eq) {
1614 case('range') {
1615 373 my $spec = $key_spec->{find_keys}{spec};
1616 373 my $last_key = $spec->{lastkey};
1617 373 ▶ unless($last_key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($last_key) 346 27 373
1618 346 push @keys, shift @target;
1619 346 next KEYSPEC;
1620 }
1621  
1622 27 my $key_step = $spec->{keystep};
1623 27 my $limit = $spec->{limit};
1624  
1625 27 ▶ splice @target, (1 + $last_key) * ($key_step + 1) if $last_key < -1;
Branch: 1 branch
BranchTrueFalseTotal
if $last_key < -1 0 27 27
1626 27 my $target_index = 0 + @target;
1627 27 ▶ $target_index = $last_key * $key_step if $last_key > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $last_key > 0 0 27 27
1628 27 ▶ $target_index = int($target_index / $limit) if $limit > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $limit > 1 0 27 27
1629 27 ▶ $target_index //= 1;
Condition50%1/2
$target_index //= 1
l!l
270
MC/DC0%0/1
$target_index //= 1
$target_index
Truth table50%1/2
A
A= $target_index
Aresult
11
00
1630  
1631 27 ▶ while(@target and $target_index--) {
Branch: 1 branch
BranchTrueFalseTotal
if (@target and $target_index--) 56 27 83
Condition66%2/3
@target and $target_index--
!ll&&!rl&&r
27056
MC/DC50%1/2
@target and $target_index--
@target $target_index--
Truth table66%2/3
A and B
A= @targetB= $target_index--
ABresult
0X0
100
111
1632 56 my ($next) = splice @target, 0, $key_step;
1633 56 push @keys, $next;
1634 }
1635 27 ▶ next KEYSPEC;
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'keynum' ? : 24 0 24
1636 }
1637 case('keynum') {
1638 24 my $spec = $key_spec->{find_keys}{spec};
1639 24 my $key_step = $spec->{keystep};
1640 24 my $count = $target[$spec->{keynumidx}];
1641 24 splice @target, 0, $spec->{firstkey};
1642 24 ▶ while(@target and $count--) {
Branch: 1 branch
BranchTrueFalseTotal
if (@target and $count--) 43 24 67
Condition100%3/3
@target and $count--
!ll&&!rl&&r
61843
MC/DC100%2/2
@target and $count--
@target $count--
Truth table100%3/3
A and B
A= @targetB= $count--
ABresult
0X0
100
111
1643 43 my ($next) = splice @target, 0, $key_step;
1644 43 push @keys, $next;
1645 }
1646 24 ▶ next KEYSPEC;
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'unknown' ? : 0 0 0
1647 }
1648 case('unknown') {
1649 0 die 'Unknown key specification for command ' . $cmd;
1650 }
1651 397 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'range' ? : 373 24 397
1652 0 die 'No key specification for command ' . $cmd;
1653 }
1654 }
1655 }
1656 399 $keyspec_cache->set(join("\x{01FF}", $command->@*), \@keys);
1657 399 return @keys;
1658 }
1659  
1660 399 ▶sub extract_spec_for_command ($class, $command, $def = undef) {
Subroutine extract_spec_for_command: called CC=10 SCAR=24.3
Pod undocumented
1661 399 my $cache = 0;
1662 399 ▶ unless($def) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def) 399 0 399
1663 399 ▶ $def //= \%COMMAND_DEFINITION;
Condition50%1/2
$def //= \%COMMAND_DEFINITION
l!l
0399
MC/DC0%0/1
$def //= \%COMMAND_DEFINITION
$def
Truth table50%1/2
A
A= $def
Aresult
11
00
1664 399 ++$cache;
1665 }
1666  
1667 # The command itself is represented as a method name
1668 399 my (@components) = $command->@*;
1669 399 my ($cmd) = @components;
1670 399 ▶ my $info = $def->{lc $cmd} or die 'command not found: ' . $cmd;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $$def{lc $cmd} or die 'command not found: ' . $cmd 399 0 399
1671  
1672 # Identify the full matching command against the known definitions
1673 399 my $idx = 0;
1674 399 ▶ while(@components && $info->{subcommands} && $info->{subcommands}->@* && $#components >= $idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (@components and $$info{"subcommands"} and @{$$info{"subcommands"}} and $#components >= $idx) 11 399 410
Condition55%5/9
@components and $$info{"subcommands"} and @{$$info{"subcommands"}} and $#components >= $idx
!ll&&!rl&&r
399011
@components and $$info{"subcommands"} and @{$$info{"subcommands"}}
!ll&&!rl&&r
039911
@components and $$info{"subcommands"}
!ll&&!rl&&r
00410
MC/DC25%1/4
@components and $$info{"subcommands"} and @{$$info{"subcommands"}} and $#components >= $idx
@components $$info{"subcommands"} @{$$info{"subcommands"}} $#components >= $idx
Truth table40%2/5
A and B and C and D
A= @componentsB= $$info{"subcommands"}C= @{$$info{"subcommands"}}D= $#components >= $idx
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1675 11 my $next = "${cmd}_" . $components[++$idx];
1676 11 ▶ last unless exists $def->{lc $next};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$def{lc $next} 0 11 11
1677 11 $cmd = $next;
1678 11 $info = $def->{lc $cmd};
1679 }
1680 399 return $info;
1681 }
1682  
1683 =head2 ssl_options
1684  
1685 Extracts the SSL-related options as a hashref for passing
1686 to C<< $loop->connect >>.
1687  
1688 =cut
1689  
1690 0 ▶method ssl_options {
Subroutine ssl_options: not called CC=2 SCAR=17.9
Pod documented
1691 return {
1692 map {
1693 0 defined $self->{"tls_$_"} ? (
1694 0 ▶ "SSL_$_" => $self->{"tls_$_"}
Branch: 1 branch
BranchTrueFalseTotal
defined $$self{"tls_$_"} ? : 0 0 0
1695 ) : ()
1696 } qw(
1697 cert_file
1698 key_file
1699 ca_file
1700 )
1701 };
1702 14 }
1703  
1704 14 1;
1705  
1706 __END__
« Redis/Protocol.pm ↑ Summary Redis/Server/Database.pm »