/
README
350 lines (294 loc) · 7.31 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
NAME
Mojo::Redis - Asynchronous Redis client for Mojolicious.
SYNOPSIS
use Mojo::Redis;
my $redis = Mojo::Redis->new(server => '127.0.0.1:6379');
# Execute some commands
$redis->ping(
sub {
my ($redis, $res) = @_;
if (defined $res) {
print "Got result: ", $res->[0], "\n";
}
}
);
# Work with keys
# Ommitting the callback still makes it non-blocking and "error" events
# will be called if something terrible goes wrong.
$redis->set(key => 'value');
$redis->get(
key => sub {
my ($redis, $res) = @_;
print "Value of 'key' is $res\n";
}
);
# Cleanup connection
$redis->quit(sub { shift->ioloop->stop });
# Start IOLoop (in case it is not started yet)
$redis->ioloop->start;
Create new Mojo::IOLoop instance if you need to get blocked in a
Mojolicious application.
use Mojolicious::Lite;
use Mojo::Redis;
get '/user' => sub {
my $self = shift->render_later;
my $uid = $self->session('uid');
my $redis = Mojo::Redis->new;
Mojo::IOLoop->delay(
sub {
my ($delay) = @_;
$redis->hgetall("user:$uid", $delay->begin);
},
sub {
my ($delay, $user) = @_;
$self->render_json($user);
},
);
};
websocket '/messages' => sub {
my $self = shift;
my $tx = $self->tx;
my $pub = Mojo::Redis->new;
my $sub = $pub->subscribe('messages');
# message from redis
$sub->on(message => sub {
my ($sub, $message, $channel) = @_; # $channel == messages
$tx->send($message);
});
# message from websocket
$self->on(message => sub {
my ($self, $message) = @_;
$pub->publish(messages => $message);
});
# need to clean up after websocket close
$self->on(finish => sub {
undef $pub;
undef $sub;
undef $tx;
});
};
app->start;
DESCRIPTION
Mojo::Redis is an asynchronous client to Redis <http://redis.io> for
Mojo.
EVENTS
error
$redis->on(error => sub{
my($redis, $error) = @_;
warn "[REDIS ERROR] $error\n";
});
Emitted if error occurred. Called before commands callbacks.
close
$redis->on(close => sub{
my($redis) = @_;
warn "[REDIS DISCONNECT]\n";
});
Emitted when the connection to the server gets closed.
ATTRIBUTES
Mojo::Redis implements the following attributes.
server
my $server = $redis->server;
$redis = $redis->server('127.0.0.1:6379');
$redis = $redis->server('redis://anything:PASSWORD@127.0.0.1:6379/DB_INDEX');
"Redis" server connection string, defaults to '127.0.0.1:6379'. The
latter can be used if you want Mojo::Redis to automatically run "auth"
with "PASSWORD" and/or "select" with "DB_INDEX" on connect. Both AUTH
and DB_INDEX are optional.
ioloop
my $ioloop = $redis->ioloop;
$redis = $redis->ioloop(Mojo::IOLoop->new);
Loop object to use for io operations, by default a Mojo::IOLoop
singleton object will be used.
timeout
my $timeout = $redis->timeout;
$redis = $redis->timeout(100);
Maximum amount of time in seconds a connection can be inactive before
being dropped, defaults to 300.
encoding
my $encoding = $redis->encoding;
$redis = $redis->encoding('UTF-8');
Encoding used for stored data, defaults to "UTF-8".
protocol_redis
use Protocol::Redis::XS;
$redis->protocol_redis("Protocol::Redis::XS");
Protocol::Redis implementation' constructor for parsing. By default
Protocol::Redis will be used. Parser library must support APIv1.
Using Protocol::Redis::XS instead of default choice can speedup parsing.
METHODS
Mojo::Redis supports Redis' methods.
$redis->set(key => 'value);
$redis->get(key => sub { ... });
For more details take a look at "execute" method.
Also Mojo::Redis implements the following ones.
connect
$redis = $redis->connect;
Connect to "Redis" server.
execute
$redis = $redis->execute("ping" => sub {
my ($redis, $result) = @_;
# Process result
});
$redis->execute(lrange => "test", 0, -1 => sub {...});
$redis->execute(set => test => "test_ok");
$redis->execute(
[lrange => "test", 0, -1],
[get => "test"],
[hmset => foo => { one => 1, two => 2 }],
sub {
my($redis, $lrange, $get, $hmset) = @_;
# ...
},
);
Execute specified command on "Redis" server. If error occurred during
request $result will be set to undef, error string can be obtained with
the "error" event.
REDIS METHODS
append
auth
See "server" instead.
bgrewriteaof
bgsave
blpop
brpop
brpoplpush
config_get
config_resetstat
config_set
connected
dbsize
debug_object
debug_segfault
decr
decrby
del
discard
disconnect
echo
exec
exists
expire
expireat
flushall
flushdb
get
getbit
getrange
getset
hdel
hexists
hget
hgetall
hincrby
hkeys
hlen
hmget
hmset
hset
hsetnx
hvals
incr
incrby
info
keys
lastsave
lindex
linsert
llen
lpop
lpush
lpushx
lrange
lrem
lset
ltrim
mget
monitor
move
mset
msetnx
multi
persist
ping
protocol
publish
quit
randomkey
rename
renamenx
rpop
rpoplpush
rpush
rpushx
sadd
save
scard
sdiff
sdiffstore
select
See "server" instead.
set
setbit
setex
setnx
setrange
shutdown
sinter
sinterstore
sismember
slaveof
smembers
smove
sort
spop
srandmember
srem
strlen
subscribe
It's possible to subscribe in two ways:
$self = $redis->subscribe('foo','bar' => sub {
my ($redis, $data) = @_;
});
The above code will overtake the current connection (if any) and put
this object into a pure subscribe mode.
$sub = $redis->subscribe('foo','bar')->on(data => sub {
my ($sub, $data) = @_;
});
Opens up a new connection that subscribes to the given pubsub channels.
Returns an instance of Mojo::Redis::Subscription. The existing $redis
object can still be used to "get" data as expected.
sunion
sunionstore
sync
ttl
type
unwatch
watch
zadd
zcard
zcount
zincrby
zinterstore
zrange
zrangebyscore
zrank
zrem
zremrangebyrank
zremrangebyscore
zrevrange
zrevrangebyscore
zrevrank
zscore
zunionstore
SEE ALSO
Protocol::Redis, Mojolicious, Mojo::IOLoop
SUPPORT
You can contact the developers "marcus" and "batman" on IRC:
<irc://irc.perl.org:6667/#mojo> (#mojo on irc.perl.org)
AUTHOR
Sergey Zasenko, "undef@cpan.org".
Forked from MojoX::Redis and updated to new IOLoop API by Marcus Ramberg
"mramberg@cpan.org" and Jan Henning Thorsen "jhthorsen@cpan.org".
COPYRIGHT AND LICENSE
Copyright (C) 2010-2011, Sergey Zasenko (C) 2012, Marcus Ramberg
This program is free software, you can redistribute it and/or modify it
under the terms of the Artistic License version 2.0.