line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::Redis; |
2
|
18
|
|
|
18
|
|
4175037
|
use Mojo::Base 'Mojo::EventEmitter'; |
|
18
|
|
|
|
|
272
|
|
|
18
|
|
|
|
|
136
|
|
3
|
|
|
|
|
|
|
|
4
|
18
|
|
|
18
|
|
36870
|
use Mojo::URL; |
|
18
|
|
|
|
|
129071
|
|
|
18
|
|
|
|
|
149
|
|
5
|
18
|
|
|
18
|
|
9419
|
use Mojo::Redis::Connection; |
|
18
|
|
|
|
|
99
|
|
|
18
|
|
|
|
|
148
|
|
6
|
18
|
|
|
18
|
|
9448
|
use Mojo::Redis::Cache; |
|
18
|
|
|
|
|
48
|
|
|
18
|
|
|
|
|
137
|
|
7
|
18
|
|
|
18
|
|
9157
|
use Mojo::Redis::Cursor; |
|
18
|
|
|
|
|
45
|
|
|
18
|
|
|
|
|
129
|
|
8
|
18
|
|
|
18
|
|
11307
|
use Mojo::Redis::Database; |
|
18
|
|
|
|
|
56
|
|
|
18
|
|
|
|
|
125
|
|
9
|
18
|
|
|
18
|
|
9946
|
use Mojo::Redis::PubSub; |
|
18
|
|
|
|
|
48
|
|
|
18
|
|
|
|
|
133
|
|
10
|
18
|
|
|
18
|
|
680
|
use Scalar::Util 'blessed'; |
|
18
|
|
|
|
|
46
|
|
|
18
|
|
|
|
|
18464
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '3.29'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$ENV{MOJO_REDIS_URL} ||= 'redis://localhost:6379'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has encoding => 'UTF-8'; |
17
|
|
|
|
|
|
|
has max_connections => 5; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has protocol_class => do { |
20
|
|
|
|
|
|
|
my $class = $ENV{MOJO_REDIS_PROTOCOL}; |
21
|
|
|
|
|
|
|
$class ||= eval { require Protocol::Redis::XS; Protocol::Redis::XS->VERSION('0.06'); 'Protocol::Redis::XS' }; |
22
|
|
|
|
|
|
|
$class ||= 'Protocol::Redis::Faster'; |
23
|
|
|
|
|
|
|
eval "require $class; 1" or die $@; |
24
|
|
|
|
|
|
|
$class; |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has pubsub => sub { |
28
|
|
|
|
|
|
|
my $self = shift; |
29
|
|
|
|
|
|
|
my $pubsub = Mojo::Redis::PubSub->new(redis => $self); |
30
|
|
|
|
|
|
|
Scalar::Util::weaken($pubsub->{redis}); |
31
|
|
|
|
|
|
|
return $pubsub; |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has url => sub { Mojo::URL->new($ENV{MOJO_REDIS_URL}) }; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
1
|
18
|
sub cache { Mojo::Redis::Cache->new(redis => shift, @_) } |
37
|
0
|
0
|
|
0
|
1
|
0
|
sub cursor { Mojo::Redis::Cursor->new(redis => shift, command => [@_ ? @_ : (scan => 0)]) } |
38
|
12
|
|
|
12
|
1
|
5236
|
sub db { Mojo::Redis::Database->new(redis => shift) } |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new { |
41
|
12
|
|
|
12
|
1
|
10956
|
my $class = shift; |
42
|
12
|
100
|
100
|
|
|
125
|
return $class->SUPER::new(@_) unless @_ % 2 and ref $_[0] ne 'HASH'; |
43
|
7
|
|
|
|
|
19
|
my $url = shift; |
44
|
7
|
100
|
66
|
|
|
88
|
$url = Mojo::URL->new($url) unless blessed $url and $url->isa('Mojo::URL'); |
45
|
7
|
|
|
|
|
1594
|
return $class->SUPER::new(url => $url, @_); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _connection { |
49
|
16
|
|
|
16
|
|
58
|
my ($self, %args) = @_; |
50
|
|
|
|
|
|
|
|
51
|
16
|
|
66
|
|
|
90
|
$args{ioloop} ||= Mojo::IOLoop->singleton; |
52
|
16
|
|
|
|
|
152
|
my $conn = Mojo::Redis::Connection->new( |
53
|
|
|
|
|
|
|
encoding => $self->encoding, |
54
|
|
|
|
|
|
|
protocol => $self->protocol_class->new(api => 1), |
55
|
|
|
|
|
|
|
url => $self->url->clone, |
56
|
|
|
|
|
|
|
%args |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
16
|
|
|
|
|
1325
|
Scalar::Util::weaken($self); |
60
|
16
|
|
|
5
|
|
145
|
$conn->on(connect => sub { $self->emit(connection => $_[0]) }); |
|
5
|
|
|
|
|
70
|
|
61
|
16
|
|
|
|
|
210
|
$conn; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _blocking_connection { |
65
|
5
|
|
|
5
|
|
3197
|
my $self = shift->_fork_safety; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Existing connection |
68
|
5
|
|
|
|
|
8
|
my $conn = $self->{blocking_connection}; |
69
|
5
|
50
|
66
|
|
|
29
|
return $conn->encoding($self->encoding) if $conn and $conn->is_connected; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# New connection |
72
|
5
|
100
|
|
|
|
21
|
return $self->{blocking_connection} = $self->_connection(ioloop => $conn ? $conn->ioloop : Mojo::IOLoop->new); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _dequeue { |
76
|
10
|
|
|
10
|
|
86
|
my $self = shift->_fork_safety; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# Exsting connection |
79
|
10
|
0
|
|
|
|
16
|
while (my $conn = shift @{$self->{queue} || []}) { return $conn->encoding($self->encoding) if $conn->is_connected } |
|
0
|
100
|
|
|
|
0
|
|
|
10
|
|
|
|
|
49
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# New connection |
82
|
10
|
|
|
|
|
41
|
return $self->_connection; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _enqueue { |
86
|
10
|
|
|
10
|
|
33
|
my ($self, $conn) = @_; |
87
|
10
|
|
100
|
|
|
39
|
my $queue = $self->{queue} ||= []; |
88
|
10
|
100
|
100
|
|
|
43
|
push @$queue, $conn if $conn->is_connected and $conn->url eq $self->url and $conn->ioloop eq Mojo::IOLoop->singleton; |
|
|
|
66
|
|
|
|
|
89
|
10
|
|
|
|
|
2538
|
shift @$queue while @$queue > $self->max_connections; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub _fork_safety { |
93
|
15
|
|
|
15
|
|
25
|
my $self = shift; |
94
|
15
|
50
|
66
|
|
|
105
|
delete @$self{qw(blocking_connection pid queue)} unless ($self->{pid} //= $$) eq $$; # Fork-safety |
95
|
15
|
|
|
|
|
27
|
$self; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=encoding utf8 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 NAME |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Mojo::Redis - Redis driver based on Mojo::IOLoop |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 SYNOPSIS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 Blocking |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
use Mojo::Redis; |
111
|
|
|
|
|
|
|
my $redis = Mojo::Redis->new; |
112
|
|
|
|
|
|
|
$redis->db->set(foo => 42); |
113
|
|
|
|
|
|
|
$redis->db->expire(foo => 600); |
114
|
|
|
|
|
|
|
warn $redis->db->get('foo'); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 Promises |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$redis->db->get_p("mykey")->then(sub { |
119
|
|
|
|
|
|
|
print "mykey=$_[0]\n"; |
120
|
|
|
|
|
|
|
})->catch(sub { |
121
|
|
|
|
|
|
|
warn "Could not fetch mykey: $_[0]"; |
122
|
|
|
|
|
|
|
})->wait; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 Pipelining |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Pipelining is built into the API by sending a lot of commands and then use |
127
|
|
|
|
|
|
|
L to wait for all the responses. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Mojo::Promise->all( |
130
|
|
|
|
|
|
|
$db->set_p($key, 10), |
131
|
|
|
|
|
|
|
$db->incrby_p($key, 9), |
132
|
|
|
|
|
|
|
$db->incr_p($key), |
133
|
|
|
|
|
|
|
$db->get_p($key), |
134
|
|
|
|
|
|
|
$db->incr_p($key), |
135
|
|
|
|
|
|
|
$db->get_p($key), |
136
|
|
|
|
|
|
|
)->then(sub { |
137
|
|
|
|
|
|
|
@res = map {@$_} @_; |
138
|
|
|
|
|
|
|
})->wait; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 DESCRIPTION |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L is a Redis driver that use the L, which makes it |
143
|
|
|
|
|
|
|
integrate easily with the L framework. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
It tries to mimic the same interface as L, L and |
146
|
|
|
|
|
|
|
L, but the methods for talking to the database vary. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This module is in no way compatible with the 1.xx version of C |
149
|
|
|
|
|
|
|
and this version also tries to fix a lot of the confusing methods in |
150
|
|
|
|
|
|
|
C related to pubsub. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This module is currently EXPERIMENTAL, and bad design decisions will be fixed |
153
|
|
|
|
|
|
|
without warning. Please report at |
154
|
|
|
|
|
|
|
L if you find this module |
155
|
|
|
|
|
|
|
useful, annoying or if you simply find bugs. Feedback can also be sent to |
156
|
|
|
|
|
|
|
C. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 EVENTS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 connection |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$cb = $redis->on(connection => sub { my ($redis, $connection) = @_; }); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Emitted when L connects to the Redis. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 encoding |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
$str = $redis->encoding; |
171
|
|
|
|
|
|
|
$redis = $redis->encoding("UTF-8"); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
The value of this attribute will be passed on to |
174
|
|
|
|
|
|
|
L when a new connection is created. This |
175
|
|
|
|
|
|
|
means that updating this attribute will not change any connection that is |
176
|
|
|
|
|
|
|
in use. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Default value is "UTF-8". |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 max_connections |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
$int = $redis->max_connections; |
183
|
|
|
|
|
|
|
$redis = $redis->max_connections(5); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Maximum number of idle database handles to cache for future use, defaults to |
186
|
|
|
|
|
|
|
5. (Default is subject to change) |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 protocol_class |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$str = $redis->protocol_class; |
191
|
|
|
|
|
|
|
$redis = $redis->protocol_class("Protocol::Redis::XS"); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Default to L if the optional module is available and at |
194
|
|
|
|
|
|
|
least version 0.06, or falls back to L. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 pubsub |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
$pubsub = $redis->pubsub; |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Lazy builds an instance of L for this object, instead of |
201
|
|
|
|
|
|
|
returning a new instance like L does. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head2 url |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
$url = $redis->url; |
206
|
|
|
|
|
|
|
$redis = $redis->url(Mojo::URL->new("redis://localhost/3")); |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Holds an instance of L that describes how to connect to the Redis server. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 METHODS |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 db |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
$db = $redis->db; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Returns an instance of L. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head2 cache |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
$cache = $redis->cache(%attrs); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Returns an instance of L. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 cursor |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
$cursor = $redis->cursor(@command); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Returns an instance of L with |
229
|
|
|
|
|
|
|
L set to the arguments passed. See |
230
|
|
|
|
|
|
|
L. for possible commands. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 new |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
$redis = Mojo::Redis->new("redis://localhost:6379/1"); |
235
|
|
|
|
|
|
|
$redis = Mojo::Redis->new(Mojo::URL->new->host("/tmp/redis.sock")); |
236
|
|
|
|
|
|
|
$redis = Mojo::Redis->new(\%attrs); |
237
|
|
|
|
|
|
|
$redis = Mojo::Redis->new(%attrs); |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Object constructor. Can coerce a string into a L and set L |
240
|
|
|
|
|
|
|
if present. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 AUTHORS |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
Jan Henning Thorsen - C |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
Dan Book - C |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Copyright (C) 2018, Jan Henning Thorsen. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
This program is free software, you can redistribute it and/or modify it under |
253
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |