| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package EV::Memcached; |
|
2
|
8
|
|
|
8
|
|
1132896
|
use strict; |
|
|
8
|
|
|
|
|
12
|
|
|
|
8
|
|
|
|
|
248
|
|
|
3
|
8
|
|
|
8
|
|
30
|
use warnings; |
|
|
8
|
|
|
|
|
10
|
|
|
|
8
|
|
|
|
|
443
|
|
|
4
|
8
|
|
|
8
|
|
582
|
use EV; |
|
|
8
|
|
|
|
|
2154
|
|
|
|
8
|
|
|
|
|
196
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
BEGIN { |
|
7
|
8
|
|
|
8
|
|
26
|
use XSLoader; |
|
|
8
|
|
|
|
|
19
|
|
|
|
8
|
|
|
|
|
315
|
|
|
8
|
8
|
|
|
8
|
|
23
|
our $VERSION = '0.02'; |
|
9
|
8
|
|
|
|
|
6622
|
XSLoader::load __PACKAGE__, $VERSION; |
|
10
|
|
|
|
|
|
|
} |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
1; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
EV::Memcached - asynchronous memcached client on libev |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use EV; |
|
21
|
|
|
|
|
|
|
use EV::Memcached; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $mc = EV::Memcached->new( |
|
24
|
|
|
|
|
|
|
host => '127.0.0.1', |
|
25
|
|
|
|
|
|
|
port => 11211, |
|
26
|
|
|
|
|
|
|
on_error => sub { warn "memcached: @_" }, |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$mc->set('foo', 'bar', sub { |
|
30
|
|
|
|
|
|
|
my ($ok, $err) = @_; |
|
31
|
|
|
|
|
|
|
warn "set failed: $err" if $err; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$mc->get('foo', sub { |
|
34
|
|
|
|
|
|
|
my ($value, $err) = @_; |
|
35
|
|
|
|
|
|
|
print "foo = $value\n"; # bar |
|
36
|
|
|
|
|
|
|
$mc->disconnect; |
|
37
|
|
|
|
|
|
|
}); |
|
38
|
|
|
|
|
|
|
}); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
EV::run; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
A pure-XS memcached client built on the L event loop. Implements the |
|
45
|
|
|
|
|
|
|
memcached binary protocol directly -- no external C client library is |
|
46
|
|
|
|
|
|
|
needed. All commands are non-blocking; results are delivered through |
|
47
|
|
|
|
|
|
|
callbacks dispatched by the EV loop. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Highlights: |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=over |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item * |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Binary protocol with pipelining, multi-get via GETKQ + NOOP fence, and |
|
56
|
|
|
|
|
|
|
fire-and-forget quiet variants (SETQ, FLUSHQ). |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item * |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
TCP and Unix socket transports, optional SASL PLAIN authentication |
|
61
|
|
|
|
|
|
|
(automatic re-auth on reconnect). |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item * |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Flow control via C, local C with optional |
|
66
|
|
|
|
|
|
|
replay across reconnects, configurable connect / command / waiting |
|
67
|
|
|
|
|
|
|
timeouts. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item * |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Predictable lifecycle: pending callbacks always fire (with the |
|
72
|
|
|
|
|
|
|
disconnect reason on teardown), DESTROY is reentrancy-safe across |
|
73
|
|
|
|
|
|
|
callback contexts. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=back |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
L applications can use this module unchanged, since AnyEvent |
|
78
|
|
|
|
|
|
|
runs on top of EV when EV is loaded. |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 ENCODING |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
This module treats all keys and values as byte strings. Encode UTF-8 |
|
83
|
|
|
|
|
|
|
strings before passing them in: |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
use Encode; |
|
86
|
|
|
|
|
|
|
$mc->set(foo => encode_utf8($val), sub { ... }); |
|
87
|
|
|
|
|
|
|
$mc->get('foo', sub { |
|
88
|
|
|
|
|
|
|
my $val = decode_utf8($_[0]); |
|
89
|
|
|
|
|
|
|
}); |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 CALLBACK CONVENTIONS |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Every command callback receives C<($result, $err)>. On success C<$err> |
|
94
|
|
|
|
|
|
|
is C; on protocol error C<$err> holds a string like C |
|
95
|
|
|
|
|
|
|
or C. On a cache miss for C/C, B arguments |
|
96
|
|
|
|
|
|
|
are C (a miss is not an error). |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Callback exceptions are caught with C and reported via C |
|
99
|
|
|
|
|
|
|
so a stray C never unwinds the libev event loop. To abort on |
|
100
|
|
|
|
|
|
|
errors, set a flag and break the loop; do not rely on C |
|
101
|
|
|
|
|
|
|
propagating out of a callback. |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 new(%options) |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Construct an instance. All options are optional; with none, the client |
|
108
|
|
|
|
|
|
|
is unconfigured and you must call C / C later. |
|
109
|
|
|
|
|
|
|
Specifying C (or C) at construction time triggers an |
|
110
|
|
|
|
|
|
|
immediate non-blocking connect. |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $mc = EV::Memcached->new( |
|
113
|
|
|
|
|
|
|
host => '127.0.0.1', |
|
114
|
|
|
|
|
|
|
port => 11211, |
|
115
|
|
|
|
|
|
|
on_error => sub { warn "@_" }, |
|
116
|
|
|
|
|
|
|
); |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head3 Connection |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item host => $str |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item port => $int (default 11211) |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
TCP host and port. Mutually exclusive with C. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item path => $str |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Unix socket path. Mutually exclusive with C. |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item loop => $ev_loop |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
EV loop to attach to. Default: C. |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item priority => $num (-2 to +2) |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
EV watcher priority. Higher = serviced before other EV watchers. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item keepalive => $seconds |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
TCP keepalive idle time. Set to 0 to disable. Ignored on Unix sockets. |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head3 Timeouts and flow control |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=over |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item connect_timeout => $ms |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Abort an in-progress non-blocking connect after this many milliseconds. |
|
153
|
|
|
|
|
|
|
0 = no timeout (default). Does not apply to Unix sockets or to |
|
154
|
|
|
|
|
|
|
immediately-completing localhost connects. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item command_timeout => $ms |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Disconnect with C<"command timeout"> error if no response arrives |
|
159
|
|
|
|
|
|
|
within this interval. The timer resets on every response from the |
|
160
|
|
|
|
|
|
|
server. 0 = no timeout (default). |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item max_pending => $num |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Cap on concurrent in-flight commands. Excess commands are held in a |
|
165
|
|
|
|
|
|
|
local waiting queue. 0 = unlimited (default). |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item waiting_timeout => $ms |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Maximum time a command may sit in the waiting queue before its callback |
|
170
|
|
|
|
|
|
|
fires with C<"waiting timeout">. 0 = unlimited (default). |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item resume_waiting_on_reconnect => $bool |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
If true, the waiting queue survives a disconnect and is replayed on |
|
175
|
|
|
|
|
|
|
reconnect. Default: false. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=back |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head3 Reconnect |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item reconnect => $bool |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Enable automatic reconnection on transport errors. |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item reconnect_delay => $ms (default 1000) |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Delay before each reconnect attempt. The delay is always honored via a |
|
190
|
|
|
|
|
|
|
timer; setting it to 0 still defers through the event loop (no |
|
191
|
|
|
|
|
|
|
synchronous retry recursion). |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item max_reconnect_attempts => $num |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Give up after this many consecutive failures and emit |
|
196
|
|
|
|
|
|
|
C<"max reconnect attempts reached">. 0 = unlimited (default). |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=back |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head3 Authentication |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=over |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=item username => $str |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item password => $str |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
SASL PLAIN credentials. When both are set, the client authenticates |
|
209
|
|
|
|
|
|
|
after every successful connect (and reconnect). Pre-connect commands |
|
210
|
|
|
|
|
|
|
sit in the waiting queue until SASL completes. Requires a memcached |
|
211
|
|
|
|
|
|
|
build with SASL support and the C<-S> flag. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=back |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head3 Event handlers |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=over |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item on_error => $cb->($errstr) |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Connection-level error callback. Default: write the message to |
|
222
|
|
|
|
|
|
|
C via C. Callbacks are run under C, so any |
|
223
|
|
|
|
|
|
|
C in a custom handler is demoted to a warning -- use an explicit |
|
224
|
|
|
|
|
|
|
flag if you need to terminate. |
|
225
|
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=item on_connect => $cb->() |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Fires once the connection is fully established (after SASL, when |
|
229
|
|
|
|
|
|
|
applicable). |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item on_disconnect => $cb->() |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Fires after a disconnect, after pending callbacks have been cancelled. |
|
234
|
|
|
|
|
|
|
For server-initiated close, this fires before C. |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=back |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 LIFECYCLE |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head2 connect($host, [$port]) |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Connect to a TCP host. Port defaults to 11211. Stops any pending |
|
243
|
|
|
|
|
|
|
auto-reconnect timer and clears any prior C setting. |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 connect_unix($path) |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Connect via Unix domain socket. Stops any pending auto-reconnect timer |
|
248
|
|
|
|
|
|
|
and clears any prior C setting. |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head2 disconnect |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
Disconnect cleanly. Cancels any pending reconnect, drains pending |
|
253
|
|
|
|
|
|
|
command callbacks with C<(undef, "disconnected")>, then fires |
|
254
|
|
|
|
|
|
|
C. For an intentional disconnect, C does B |
|
255
|
|
|
|
|
|
|
fire -- that distinction lets you tell user-initiated teardown from |
|
256
|
|
|
|
|
|
|
server-side close. |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 is_connected |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
Returns true while a session is established B in progress (TCP |
|
261
|
|
|
|
|
|
|
handshake / SASL exchange). Commands issued in the connecting phase |
|
262
|
|
|
|
|
|
|
are queued and sent on completion. |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=head2 quit([$cb]) |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Send a memcached C and let the server close the connection. |
|
267
|
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=head1 STORAGE COMMANDS |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
Each command's callback receives C<($result, $err)>. C<$result> is C<1> |
|
271
|
|
|
|
|
|
|
on success. |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head2 set($key, $value, [$expiry, [$flags,]] [$cb]) |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Store unconditionally. Without C<$cb> this becomes fire-and-forget |
|
276
|
|
|
|
|
|
|
(SETQ): no response is received and any server-side failure is silently |
|
277
|
|
|
|
|
|
|
dropped. |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head2 add($key, $value, [$expiry, [$flags,]] [$cb]) |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Store only if the key does not exist. Errors with C if |
|
282
|
|
|
|
|
|
|
present. |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head2 replace($key, $value, [$expiry, [$flags,]] [$cb]) |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Store only if the key already exists. Errors with C if |
|
287
|
|
|
|
|
|
|
absent. |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head2 cas($key, $value, $cas, [$expiry, [$flags,]] [$cb]) |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Compare-and-swap. The C<$cas> token comes from a prior C / |
|
292
|
|
|
|
|
|
|
C / C. Errors with C on token mismatch or |
|
293
|
|
|
|
|
|
|
C if the key disappeared. |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head2 append($key, $data, [$cb]) |
|
296
|
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Append bytes to an existing value. Errors with C if the |
|
298
|
|
|
|
|
|
|
key does not exist. Without C<$cb>, errors are silently dropped. |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=head2 prepend($key, $data, [$cb]) |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
Prepend bytes to an existing value. Same error and fire-and-forget |
|
303
|
|
|
|
|
|
|
semantics as C. |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head2 delete($key, [$cb]) |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
Delete a key. Errors with C if absent. |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 RETRIEVAL COMMANDS |
|
310
|
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head2 get($key, [$cb->($value, $err)]) |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
Retrieve a value. On a cache miss, both C<$value> and C<$err> are |
|
314
|
|
|
|
|
|
|
C -- a miss is not an error. |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head2 gets($key, [$cb->($info, $err)]) |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Like C but returns C<{ value =E ..., flags =E ..., cas =E ... }>. |
|
319
|
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head2 mget(\@keys, [$cb->(\%values, $err)]) |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
Multi-get, internally pipelined as a sequence of GETKQ packets |
|
323
|
|
|
|
|
|
|
terminated by a NOOP fence. Returns a hash containing only the keys |
|
324
|
|
|
|
|
|
|
that were hits: |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
$mc->mget([qw(k1 k2 k3)], sub { |
|
327
|
|
|
|
|
|
|
my ($values, $err) = @_; |
|
328
|
|
|
|
|
|
|
# $values = { k1 => 'v1', k3 => 'v3' } # k2 was a miss |
|
329
|
|
|
|
|
|
|
}); |
|
330
|
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=head2 mgets(\@keys, [$cb->(\%info, $err)]) |
|
332
|
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
Like C but each value carries metadata: |
|
334
|
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
$mc->mgets([qw(k1 k2)], sub { |
|
336
|
|
|
|
|
|
|
my ($info, $err) = @_; |
|
337
|
|
|
|
|
|
|
# $info = { k1 => { value => 'v', flags => 0, cas => 123 } } |
|
338
|
|
|
|
|
|
|
}); |
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=head1 ATOMIC COUNTERS |
|
341
|
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=head2 incr($key, [$delta, [$initial, [$expiry,]]] [$cb->($new_value, $err)]) |
|
343
|
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
Atomic increment. C<$delta> defaults to 1. C<$expiry> defaults to |
|
345
|
|
|
|
|
|
|
C<0xFFFFFFFF>, which means "do not auto-create" (the call then errors |
|
346
|
|
|
|
|
|
|
with C). Pass any other expiry to auto-create with |
|
347
|
|
|
|
|
|
|
C<$initial>: |
|
348
|
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
$mc->incr('counter', 1, sub { ... }); # require existing |
|
350
|
|
|
|
|
|
|
$mc->incr('counter', 1, 100, 300, sub { ... }); # auto-create at 100, 5min TTL |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
C<$new_value> is the post-increment counter value. |
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=head2 decr($key, [$delta, [$initial, [$expiry,]]] [$cb->($new_value, $err)]) |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
Atomic decrement. Memcached clamps the result at 0 (never negative). |
|
357
|
|
|
|
|
|
|
Same auto-create semantics as C. |
|
358
|
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=head1 EXPIRATION |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
=head2 touch($key, $expiry, [$cb]) |
|
362
|
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
Update an existing key's expiration without fetching the value. Errors |
|
364
|
|
|
|
|
|
|
with C if absent. |
|
365
|
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head2 gat($key, $expiry, [$cb->($value, $err)]) |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
Get-and-touch: retrieve and update expiration in one round-trip. Same |
|
369
|
|
|
|
|
|
|
miss semantics as C. |
|
370
|
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
=head2 gats($key, $expiry, [$cb->($info, $err)]) |
|
372
|
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
Get-and-touch with metadata. Same shape as C. |
|
374
|
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
=head1 SERVER COMMANDS |
|
376
|
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
=head2 flush([$expiry,] [$cb]) |
|
378
|
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
Invalidate every item. Optional delay in seconds before the flush takes |
|
380
|
|
|
|
|
|
|
effect. Without C<$cb>, sent as fire-and-forget (FLUSHQ). |
|
381
|
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=head2 noop([$cb]) |
|
383
|
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
No-operation round-trip. Useful as a pipeline fence to wait until all |
|
385
|
|
|
|
|
|
|
previously-sent commands have been processed. |
|
386
|
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
=head2 version([$cb->($version, $err)]) |
|
388
|
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
Server version string. |
|
390
|
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
=head2 stats([$name,] [$cb->(\%stats, $err)]) |
|
392
|
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
Server statistics. Without C<$name>, returns the default stats group. |
|
394
|
|
|
|
|
|
|
Common groups: C, C, C, C, C. |
|
395
|
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
=head1 AUTHENTICATION |
|
397
|
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
=head2 sasl_auth($username, $password, [$cb]) |
|
399
|
|
|
|
|
|
|
|
|
400
|
|
|
|
|
|
|
Authenticate via SASL PLAIN. Auto-invoked on connect when both |
|
401
|
|
|
|
|
|
|
C and C were passed to the constructor; call |
|
402
|
|
|
|
|
|
|
manually only when authenticating after a no-auth construction. |
|
403
|
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
=head2 sasl_list_mechs([$cb->($mechs, $err)]) |
|
405
|
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
Query the server's supported mechanisms; returns a space-separated |
|
407
|
|
|
|
|
|
|
string such as C<"PLAIN">. |
|
408
|
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
=head1 LOCAL CONTROL |
|
410
|
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
=head2 skip_pending |
|
412
|
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
Drain the in-flight queue, firing every callback with |
|
414
|
|
|
|
|
|
|
C<(undef, "skipped")>. The connection itself is left intact. |
|
415
|
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
=head2 skip_waiting |
|
417
|
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
Same, but for the local waiting queue (commands not yet sent). |
|
419
|
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
=head2 pending_count |
|
421
|
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
Number of commands sent and awaiting a response. |
|
423
|
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
=head2 waiting_count |
|
425
|
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
Number of commands held in the local waiting queue (because the |
|
427
|
|
|
|
|
|
|
connection is not ready, SASL is in progress, or C is |
|
428
|
|
|
|
|
|
|
saturated). |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
=head1 ACCESSORS |
|
431
|
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
Every option from C has a getter/setter of the same name. Calling |
|
433
|
|
|
|
|
|
|
without arguments reads the current value; with one argument it writes |
|
434
|
|
|
|
|
|
|
and (where meaningful, e.g. C) takes effect immediately. |
|
435
|
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
=over |
|
437
|
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
=item C |
|
439
|
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
=item C |
|
441
|
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
=item C |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
=item C |
|
445
|
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
=item C |
|
447
|
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
=item C |
|
449
|
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
=item C |
|
451
|
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
=item C |
|
453
|
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
Read-only; configure via C. |
|
455
|
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
=item C |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
Reconfigure auto-reconnect at runtime. |
|
459
|
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
=item C |
|
461
|
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
=item C |
|
463
|
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
=item C |
|
465
|
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
Get/set the corresponding handler. Pass C to clear. |
|
467
|
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=back |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
=head1 DESTRUCTION |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
If C<$mc> goes out of scope while commands are in flight or queued, |
|
473
|
|
|
|
|
|
|
every pending and waiting callback fires once with |
|
474
|
|
|
|
|
|
|
C<(undef, "disconnected")>. This holds whether you call C |
|
475
|
|
|
|
|
|
|
first or simply drop the reference. |
|
476
|
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
The clean shutdown idiom is: |
|
478
|
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
$mc->disconnect; # drains queues, fires on_disconnect |
|
480
|
|
|
|
|
|
|
undef $mc; |
|
481
|
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
If a callback closes over C<$mc> (a common mistake -- every reference |
|
483
|
|
|
|
|
|
|
inside a callback closure keeps the object alive), break the cycle |
|
484
|
|
|
|
|
|
|
before dropping the outer reference: |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
$mc->on_error(undef); |
|
487
|
|
|
|
|
|
|
$mc->on_connect(undef); |
|
488
|
|
|
|
|
|
|
$mc->on_disconnect(undef); |
|
489
|
|
|
|
|
|
|
undef $mc; |
|
490
|
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
DESTROY is reentrant-safe: if a callback fired during teardown drops |
|
492
|
|
|
|
|
|
|
the last external reference to a separate C, that |
|
493
|
|
|
|
|
|
|
object's DESTROY is correctly deferred and run once unwound. |
|
494
|
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
=head1 BINARY PROTOCOL NOTES |
|
496
|
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
The wire format is the memcached binary protocol -- a 24-byte header |
|
498
|
|
|
|
|
|
|
plus body, with each request tagged by an opaque field used for |
|
499
|
|
|
|
|
|
|
in-flight matching and pipelining. Multi-get is sent as a run of |
|
500
|
|
|
|
|
|
|
GETKQ packets ending in a NOOP fence: the server emits a response |
|
501
|
|
|
|
|
|
|
only on hit, and the NOOP reply terminates the batch. Fire-and-forget |
|
502
|
|
|
|
|
|
|
C/C use the quiet SETQ / FLUSHQ opcodes so the server |
|
503
|
|
|
|
|
|
|
sends no response at all. |
|
504
|
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
Commands that can legitimately fail (C, C, C, |
|
506
|
|
|
|
|
|
|
C, ...) always use the non-quiet opcode so error responses are |
|
507
|
|
|
|
|
|
|
consumed by the client even when the user passed no callback. Keys are |
|
508
|
|
|
|
|
|
|
validated against the 250-byte protocol limit before any bytes go on |
|
509
|
|
|
|
|
|
|
the wire. |
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
=head1 BENCHMARKS |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
Numbers from C on Linux, TCP loopback, 100-byte |
|
514
|
|
|
|
|
|
|
values, Perl 5.40, memcached 1.6.41: |
|
515
|
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
50K cmds 200K cmds |
|
517
|
|
|
|
|
|
|
Pipeline SET 213K 68K ops/sec |
|
518
|
|
|
|
|
|
|
Pipeline GET 216K 67K ops/sec |
|
519
|
|
|
|
|
|
|
Mixed workload 226K 69K ops/sec |
|
520
|
|
|
|
|
|
|
Fire-and-forget SET 1.13M 1.29M ops/sec (SETQ) |
|
521
|
|
|
|
|
|
|
Multi-get (GETKQ) 1.30M 1.17M ops/sec (per key) |
|
522
|
|
|
|
|
|
|
Sequential round-trip 41K 38K ops/sec |
|
523
|
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
Fire-and-forget is roughly 5x faster than callback mode because there |
|
525
|
|
|
|
|
|
|
is no per-command Perl SV allocation. Multi-get is the fastest read |
|
526
|
|
|
|
|
|
|
path since misses generate no traffic. Callback-mode throughput drops |
|
527
|
|
|
|
|
|
|
as batch size grows because SV allocation for closures dominates; |
|
528
|
|
|
|
|
|
|
realistic workloads (interleaved sends and receives) stay close to the |
|
529
|
|
|
|
|
|
|
50K-command column. |
|
530
|
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
C overhead (200K commands): |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
unlimited ~131K ops/sec |
|
534
|
|
|
|
|
|
|
max_pending=500 ~126K ops/sec |
|
535
|
|
|
|
|
|
|
max_pending=100 ~120K ops/sec |
|
536
|
|
|
|
|
|
|
max_pending=50 ~117K ops/sec |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
Override C, C, C, and |
|
539
|
|
|
|
|
|
|
C to retune. |
|
540
|
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
542
|
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
L, L, L, L, |
|
544
|
|
|
|
|
|
|
L. |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
=head1 AUTHOR |
|
547
|
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
vividsnow |
|
549
|
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
=head1 LICENSE |
|
551
|
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it |
|
553
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
554
|
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=cut |