-
Notifications
You must be signed in to change notification settings - Fork 13
/
README
258 lines (186 loc) · 7.94 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
NAME
Mojo::Redis2 - Pure-Perl non-blocking I/O Redis driver
VERSION
0.08
DESCRIPTION
THIS MODULE IS UNDER DEVELOPMENT! FEEDBACK WANTED!
Mojo::Redis2 is a pure-Perl non-blocking I/O Redis <http://redis.io>
driver for the Mojolicious real-time framework.
Redis is an open source, BSD licensed, advanced key-value cache and
store. It is often referred to as a data structure server since keys
can contain strings, hashes, lists, sets, sorted sets, bitmaps and
hyperloglogs. - <http://redis.io>.
Features:
* Blocking support
Mojo::Redis2 support blocking methods. NOTE: Calling non-blocking
and blocking methods are supported on the same object, but might
create a new connection to the server.
* Error handling that makes sense
Mojo::Redis was unable to report back errors that was bound to an
operation. Mojo::Redis2 on the other hand always make sure each
callback receive an error message on error.
* One object for different operations
Mojo::Redis had only one connection, so it could not do more than on
blocking operation on the server side at the time (such as BLPOP,
SUBSCRIBE, ...). This object creates new connections pr. blocking
operation which makes it easier to avoid "blocking" bugs.
SYNOPSIS
Blocking
use Mojo::Redis2;
my $redis = Mojo::Redis2->new;
# Will die() on error.
$res = $redis->set(foo => "42"); # $res = OK
$res = $redis->get("foo"); # $res = 42
Non-blocking
Mojo::IOLoop->delay(
sub {
my ($delay) = @_;
$redis->ping($delay->begin)->get("foo", $delay->begin);
},
sub {
my ($delay, $ping_err, $ping, $get_err, $get) = @_;
# On error: $ping_err and $get_err is set to a string
# On success: $ping = "PONG", $get = "42";
},
);
Pub/sub
Mojo::Redis2 can "subscribe" and re-use the same object to "publish" or
run other Redis commands, since it can keep track of multiple
connections to the same Redis server. It will also re-use the same
connection when you (p)subscribe multiple times.
$self->on(message => sub {
my ($self, $message, $channel) = @_;
});
$self->subscribe("some:channel" => sub {
my ($self, $err) = @_;
return $self->publish("myapp:errors" => $err) if $err;
return $self->incr("subscribed:to:some:channel");
});
Mojolicious app
use Mojolicious::Lite;
helper redis => sub { shift->stash->{redis} ||= Mojo::Redis2->new; };
get '/' => sub {
my $c = shift;
$c->delay(
sub {
my ($delay) = @_;
$c->redis->get('some:message', $delay->begin);
},
sub {
my ($delay, $err, $message) = @_;
$c->render(json => { error => $err, message => $message });
},
);
};
app->start;
Error handling
$err in this document is a string containing an error message or empty
string on success.
EVENTS
connection
$self->on(error => sub { my ($self, $info) = @_; ... });
Emitted when a new connection has been established. $info is a hash ref
with:
{
group => $str, # basic, blpop, brpop, brpoplpush, publish, ...
id => $connection_id,
nb => $bool, # blocking/non-blocking
}
Note: The structure of $info is EXPERIMENTAL.
error
$self->on(error => sub { my ($self, $err) = @_; ... });
Emitted if an error occurs that can't be associated with an operation.
message
$self->on(message => sub {
my ($self, $message, $channel) = @_;
});
Emitted when a $message is received on a $channel after it has been
subscribed to.
pmessage
$self->on(pmessage => sub {
my ($self, $message, $channel, $pattern) = @_;
});
Emitted when a $message is received on a $channel matching a $pattern,
after it has been subscribed to.
ATTRIBUTES
encoding
$str = $self->encoding;
$self = $self->encoding('UTF-8');
Holds the encoding using for data from/to Redis. Default is UTF-8.
protocol
$obj = $self->protocol;
$self = $self->protocol($obj);
Holds an object used to parse/generate Redis messages. Defaults to
Protocol::Redis::XS or Protocol::Redis.
Protocol::Redis::XS need to be installed manually.
url
$url = $self->url;
Holds a Mojo::URL object with the location to the Redis server. Default
is "MOJO_REDIS_URL" or "redis://localhost:6379". The "url" need to be
set in constructor. Examples:
Mojo::Redis2->new(url => "redis://x:$auth_key\@$server:$port/$database_index");
Mojo::Redis2->new(url => "redis://10.0.0.42:6379");
Mojo::Redis2->new(url => "redis://10.0.0.42:6379/1");
Mojo::Redis2->new(url => "redis://x:s3cret\@10.0.0.42:6379/1");
METHODS
In addition to the methods listed in this module, you can call these
Redis methods on $self:
append, echo, decr, decrby, del, exists, expire, expireat, get, getbit,
getrange, getset, hdel, hexists, hget, hgetall, hincrby, hkeys, hlen,
hmget, hmset, hset, hsetnx, hvals, incr, incrby, keys, lindex, linsert,
llen, lpop, lpush, lpushx, lrange, lrem, lset, ltrim, mget, move, mset,
msetnx, persist, ping, publish, randomkey, rename, renamenx, rpop,
rpoplpush, rpush, rpushx, sadd, scard, sdiff, sdiffstore, set, setbit,
setex, setnx, setrange, sinter, sinterstore, sismember, smembers, smove,
sort, spop, srandmember, srem, strlen, sunion, sunionstore, ttl, type,
zadd, zcard, zcount, zincrby, zinterstore, zrange, zrangebyscore, zrank,
zrem, zremrangebyrank, zremrangebyscore, zrevrange, zrevrangebyscore,
zrevrank, zscore and zunionstore.
See <http://redis.io/commands> for details.
new
$self = Mojo::Redis2->new(...);
Object constructor. Makes sure "url" is an object.
blpop
$self = $self->blpop(@keys, $timeout, sub { my ($self, $res) = @_; });
This method will issue the BLPOP command on the Redis server, but in its
own connection. This means that $self can still be used to run other
commands instead of being blocking.
Note: This method will only work in a non-blocking environment.
See also <http://redis.io/commands/blpop>.
brpoplpush
$self = $self->brpoplpush($from => $to, $timeout, sub { my ($self, $res) = @_; });
Follows the same API as "blpop". See also
<http://redis.io/commands/brpoplpush>.
brpop
$self = $self->brpop(@keys, $timeout, sub { my ($self, $res) = @_; });
Follows the same API as "blpop". See also
<http://redis.io/commands/brpop>.
bulk
$obj = $self->bulk;
Returns a Mojo::Redis2::Bulk object which can be used to group Redis
operations.
client
$self->client->$method(@args);
Run "CLIENT" commands using Mojo::Redis2::Client.
multi
$txn = $self->multi;
This method does not perform the "MULTI" Redis command, but returns a
Mojo::Redis2::Transaction object instead.
The Mojo::Redis2::Transaction object is a subclass of Mojo::Redis2,
which will run all the Redis commands inside a transaction.
psubscribe
$self = $self->psubscribe(@patterns, sub { my ($self, $err, $res) = @_; ... });
Used to subscribe to channels that match @patterns. Messages arriving
over a matching channel name will result in "pmessage" events.
See <http://redis.io/topics/pubsub> for details.
subscribe
$self = $self->subscribe(@channels, sub { my ($self, $err, $res) = @_; ... });
Used to subscribe to @channels. Messages arriving over a channel will
result in "message" events.
See <http://redis.io/topics/pubsub> for details.
COPYRIGHT AND LICENSE
Copyright (C) 2014, Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it
under the terms of the Artistic License version 2.0.
AUTHOR
Jan Henning Thorsen - "[email protected]"