-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathREADME
182 lines (129 loc) · 4.95 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
NAME
AnyEvent::Redis - Non-blocking Redis client
SYNOPSIS
use AnyEvent::Redis;
my $redis = AnyEvent::Redis->new(
host => '127.0.0.1',
port => 6379,
encoding => 'utf8',
on_error => sub { warn @_ },
on_cleanup => sub { warn "Connection closed: @_" },
);
# callback based
$redis->set( 'foo'=> 'bar', sub { warn "SET!" } );
$redis->get( 'foo', sub { my $value = shift } );
my ($key, $value) = ('list_key', 123);
$redis->lpush( $key, $value );
$redis->lpop( $key, sub { my $value = shift });
# condvar based
my $cv = $redis->lpop( $key );
$cv->cb(sub { my $value = $_[0]->recv });
DESCRIPTION
AnyEvent::Redis is a non-blocking (event-driven) Redis client.
This module is an AnyEvent user; you must install and use a supported
event loop.
ESTABLISHING A CONNECTION
To create a new connection, use the new() method with the following
attributes:
host => <HOSTNAME>
Required. The hostname or literal address of the server.
port => <PORT>
Optional. The server port.
encoding => <ENCODING>
Optional. Encode and decode data (when storing and retrieving,
respectively) according to *ENCODING* ("utf8" is recommended or see
Encode::Supported for details on possible *ENCODING* values).
Omit if you intend to handle raw binary data with this connection.
on_error => $cb->($errmsg)
Optional. Callback that will be fired if a connection or
database-level error occurs. The error message will be passed to the
callback as the sole argument.
on_cleanup => $cb->($errmsg)
Optional. Callback that will be fired if a connection error occurs.
The error message will be passed to the callback as the sole
argument. After this callback, errors will be reported for all
outstanding requests.
METHODS
All methods supported by your version of Redis should be supported.
Normal commands
There are two alternative approaches for handling results from commands:
* AnyEvent::CondVar based:
my $cv = $redis->command(
# arguments to command
);
# Then...
my $res;
eval {
# Could die()
$res = $cv->recv;
};
warn $@ if $@;
# or...
$cv->cb(sub {
my ($cv) = @_;
my ($result, $err) = $cv->recv
});
* Callback:
$redis->command(
# arguments,
sub {
my ($result, $err) = @_;
});
(Callback is a wrapper around the $cv approach.)
Transactions (MULTI/EXEC)
Redis transactions begin with a "multi" command and end with an "exec"
command. Commands in between are not executed immediately when they're
sent. On receipt of the "exec", the server executes all the saved
commands atomically, and returns all their results as one bulk reply.
After a transaction is finished, results for each individual command are
reported in the usual way. Thus, by the time any of these callbacks is
called, the entire transaction is finished for better or worse.
Results of the "exec" (containing all the other results) will be
returned as an array reference containing all of the individual results.
This may in some cases make callbacks on the individual commands
unnecessary, or vice versa. In this bulk reply, errors reported for each
individual command are represented by objects of class
"AnyEvent::Redis::Error", which will respond to a "->message" method
call with that error message.
It is not permitted to nest transactions. This module does not permit
subscription-related commands in a transaction.
Subscriptions
The subscription methods ("subscribe" and "psubscribe") must be used
with a callback:
my $cv = $redis->subscribe("test", sub {
my ($message, $channel[, $actual_channel]) = @_;
# ($actual_channel is provided for pattern subscriptions.)
});
The $cv condition will be met on unsubscribing from the channel.
Due to limitations of the Redis protocol the only valid commands on a
connection with an active subscription are subscribe and unsubscribe
commands.
Common methods
* get
* set
* hset
* hget
* lpush
* lpop
The Redis command reference (<http://redis.io/commands>) lists all
commands Redis supports.
REQUIREMENTS
This requires Redis >= 1.2.
COPYRIGHT
Tatsuhiko Miyagawa <[email protected]> 2009-
LICENSE
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
AUTHORS
Tatsuhiko Miyagawa
David Leadbeater
Chia-liang Kao
franck cuny
Lee Aylward
Joshua Barratt
Jeremy Zawodny
Leon Brocard
Michael S. Fischer
Chip Salzenberg
SEE ALSO
Redis, AnyEvent