0
|
1 # $Id: Memcached.pm 601 2007-07-17 17:47:33Z bradfitz $
|
|
2 #
|
|
3 # Copyright (c) 2003, 2004 Brad Fitzpatrick <brad@danga.com>
|
|
4 #
|
|
5 # See COPYRIGHT section in pod text below for usage and distribution rights.
|
|
6 #
|
|
7
|
|
8 package Cache::Memcached;
|
|
9
|
|
10 use strict;
|
|
11 use warnings;
|
|
12
|
|
13 no strict 'refs';
|
|
14 use Storable ();
|
|
15 use Socket qw( MSG_NOSIGNAL PF_INET PF_UNIX IPPROTO_TCP SOCK_STREAM );
|
|
16 use IO::Handle ();
|
|
17 use Time::HiRes ();
|
|
18 use String::CRC32;
|
|
19 use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN );
|
|
20 use Cache::Memcached::GetParser;
|
|
21 use fields qw{
|
|
22 debug no_rehash stats compress_threshold compress_enable stat_callback
|
|
23 readonly select_timeout namespace namespace_len servers active buckets
|
|
24 pref_ip
|
|
25 bucketcount _single_sock _stime
|
|
26 connect_timeout cb_connect_fail
|
|
27 parser_class
|
|
28 };
|
|
29
|
|
30 # flag definitions
|
|
31 use constant F_STORABLE => 1;
|
|
32 use constant F_COMPRESS => 2;
|
|
33
|
|
34 # size savings required before saving compressed value
|
|
35 use constant COMPRESS_SAVINGS => 0.20; # percent
|
|
36
|
|
37 use vars qw($VERSION $HAVE_ZLIB $FLAG_NOSIGNAL);
|
|
38 $VERSION = "1.24";
|
|
39
|
|
40 BEGIN {
|
|
41 $HAVE_ZLIB = eval "use Compress::Zlib (); 1;";
|
|
42 }
|
|
43
|
|
44 my $HAVE_XS = eval "use Cache::Memcached::GetParserXS; 1;";
|
|
45 $HAVE_XS = 0 if $ENV{NO_XS};
|
|
46
|
|
47 my $parser_class = $HAVE_XS ? "Cache::Memcached::GetParserXS" : "Cache::Memcached::GetParser";
|
|
48 if ($ENV{XS_DEBUG}) {
|
|
49 print "using parser: $parser_class\n";
|
|
50 }
|
|
51
|
|
52 $FLAG_NOSIGNAL = 0;
|
|
53 eval { $FLAG_NOSIGNAL = MSG_NOSIGNAL; };
|
|
54
|
|
55 my %host_dead; # host -> unixtime marked dead until
|
|
56 my %cache_sock; # host -> socket
|
|
57 my @buck2sock; # bucket number -> $sock
|
|
58
|
|
59 my $PROTO_TCP;
|
|
60
|
|
61 our $SOCK_TIMEOUT = 2.6; # default timeout in seconds
|
|
62
|
|
63 sub new {
|
|
64 my Cache::Memcached $self = shift;
|
|
65 $self = fields::new( $self ) unless ref $self;
|
|
66
|
|
67 my $args = (@_ == 1) ? shift : { @_ }; # hashref-ify args
|
|
68
|
|
69 $self->set_servers($args->{'servers'});
|
|
70 $self->{'debug'} = $args->{'debug'} || 0;
|
|
71 $self->{'no_rehash'} = $args->{'no_rehash'};
|
|
72 $self->{'stats'} = {};
|
|
73 $self->{'pref_ip'} = $args->{'pref_ip'} || {};
|
|
74 $self->{'compress_threshold'} = $args->{'compress_threshold'};
|
|
75 $self->{'compress_enable'} = 1;
|
|
76 $self->{'stat_callback'} = $args->{'stat_callback'} || undef;
|
|
77 $self->{'readonly'} = $args->{'readonly'};
|
|
78 $self->{'parser_class'} = $args->{'parser_class'} || $parser_class;
|
|
79
|
|
80 # TODO: undocumented
|
|
81 $self->{'connect_timeout'} = $args->{'connect_timeout'} || 0.25;
|
|
82 $self->{'select_timeout'} = $args->{'select_timeout'} || 1.0;
|
|
83 $self->{namespace} = $args->{namespace} || '';
|
|
84 $self->{namespace_len} = length $self->{namespace};
|
|
85
|
|
86 return $self;
|
|
87 }
|
|
88
|
|
89 sub set_pref_ip {
|
|
90 my Cache::Memcached $self = shift;
|
|
91 $self->{'pref_ip'} = shift;
|
|
92 }
|
|
93
|
|
94 sub set_servers {
|
|
95 my Cache::Memcached $self = shift;
|
|
96 my ($list) = @_;
|
|
97 $self->{'servers'} = $list || [];
|
|
98 $self->{'active'} = scalar @{$self->{'servers'}};
|
|
99 $self->{'buckets'} = undef;
|
|
100 $self->{'bucketcount'} = 0;
|
|
101 $self->init_buckets;
|
|
102 @buck2sock = ();
|
|
103
|
|
104 $self->{'_single_sock'} = undef;
|
|
105 if (@{$self->{'servers'}} == 1) {
|
|
106 $self->{'_single_sock'} = $self->{'servers'}[0];
|
|
107 }
|
|
108
|
|
109 return $self;
|
|
110 }
|
|
111
|
|
112 sub set_cb_connect_fail {
|
|
113 my Cache::Memcached $self = shift;
|
|
114 $self->{'cb_connect_fail'} = shift;
|
|
115 }
|
|
116
|
|
117 sub set_connect_timeout {
|
|
118 my Cache::Memcached $self = shift;
|
|
119 $self->{'connect_timeout'} = shift;
|
|
120 }
|
|
121
|
|
122 sub set_debug {
|
|
123 my Cache::Memcached $self = shift;
|
|
124 my ($dbg) = @_;
|
|
125 $self->{'debug'} = $dbg || 0;
|
|
126 }
|
|
127
|
|
128 sub set_readonly {
|
|
129 my Cache::Memcached $self = shift;
|
|
130 my ($ro) = @_;
|
|
131 $self->{'readonly'} = $ro;
|
|
132 }
|
|
133
|
|
134 sub set_norehash {
|
|
135 my Cache::Memcached $self = shift;
|
|
136 my ($val) = @_;
|
|
137 $self->{'no_rehash'} = $val;
|
|
138 }
|
|
139
|
|
140 sub set_compress_threshold {
|
|
141 my Cache::Memcached $self = shift;
|
|
142 my ($thresh) = @_;
|
|
143 $self->{'compress_threshold'} = $thresh;
|
|
144 }
|
|
145
|
|
146 sub enable_compress {
|
|
147 my Cache::Memcached $self = shift;
|
|
148 my ($enable) = @_;
|
|
149 $self->{'compress_enable'} = $enable;
|
|
150 }
|
|
151
|
|
152 sub forget_dead_hosts {
|
|
153 %host_dead = ();
|
|
154 @buck2sock = ();
|
|
155 }
|
|
156
|
|
157 sub set_stat_callback {
|
|
158 my Cache::Memcached $self = shift;
|
|
159 my ($stat_callback) = @_;
|
|
160 $self->{'stat_callback'} = $stat_callback;
|
|
161 }
|
|
162
|
|
163 my %sock_map; # stringified-$sock -> "$ip:$port"
|
|
164
|
|
165 sub _dead_sock {
|
|
166 my ($sock, $ret, $dead_for) = @_;
|
|
167 if (my $ipport = $sock_map{$sock}) {
|
|
168 my $now = time();
|
|
169 $host_dead{$ipport} = $now + $dead_for
|
|
170 if $dead_for;
|
|
171 delete $cache_sock{$ipport};
|
|
172 delete $sock_map{$sock};
|
|
173 }
|
|
174 @buck2sock = ();
|
|
175 return $ret; # 0 or undef, probably, depending on what caller wants
|
|
176 }
|
|
177
|
|
178 sub _close_sock {
|
|
179 my ($sock) = @_;
|
|
180 if (my $ipport = $sock_map{$sock}) {
|
|
181 close $sock;
|
|
182 delete $cache_sock{$ipport};
|
|
183 delete $sock_map{$sock};
|
|
184 }
|
|
185 @buck2sock = ();
|
|
186 }
|
|
187
|
|
188 sub _connect_sock { # sock, sin, timeout
|
|
189 my ($sock, $sin, $timeout) = @_;
|
|
190 $timeout = 0.25 if not defined $timeout;
|
|
191
|
|
192 # make the socket non-blocking from now on,
|
|
193 # except if someone wants 0 timeout, meaning
|
|
194 # a blocking connect, but even then turn it
|
|
195 # non-blocking at the end of this function
|
|
196
|
|
197 if ($timeout) {
|
|
198 IO::Handle::blocking($sock, 0);
|
|
199 } else {
|
|
200 IO::Handle::blocking($sock, 1);
|
|
201 }
|
|
202
|
|
203 my $ret = connect($sock, $sin);
|
|
204
|
|
205 if (!$ret && $timeout && $!==EINPROGRESS) {
|
|
206
|
|
207 my $win='';
|
|
208 vec($win, fileno($sock), 1) = 1;
|
|
209
|
|
210 if (select(undef, $win, undef, $timeout) > 0) {
|
|
211 $ret = connect($sock, $sin);
|
|
212 # EISCONN means connected & won't re-connect, so success
|
|
213 $ret = 1 if !$ret && $!==EISCONN;
|
|
214 }
|
|
215 }
|
|
216
|
|
217 unless ($timeout) { # socket was temporarily blocking, now revert
|
|
218 IO::Handle::blocking($sock, 0);
|
|
219 }
|
|
220
|
|
221 # from here on, we use non-blocking (async) IO for the duration
|
|
222 # of the socket's life
|
|
223
|
|
224 return $ret;
|
|
225 }
|
|
226
|
|
227 sub sock_to_host { # (host)
|
|
228 my Cache::Memcached $self = ref $_[0] ? shift : undef;
|
|
229 my $host = $_[0];
|
|
230 return $cache_sock{$host} if $cache_sock{$host};
|
|
231
|
|
232 my $now = time();
|
|
233 my ($ip, $port) = $host =~ /(.*):(\d+)/;
|
|
234 return undef if
|
|
235 $host_dead{$host} && $host_dead{$host} > $now;
|
|
236 my $sock;
|
|
237
|
|
238 my $connected = 0;
|
|
239 my $sin;
|
|
240 my $proto = $PROTO_TCP ||= getprotobyname('tcp');
|
|
241
|
|
242 if ( index($host, '/') != 0 )
|
|
243 {
|
|
244 # if a preferred IP is known, try that first.
|
|
245 if ($self && $self->{pref_ip}{$ip}) {
|
|
246 socket($sock, PF_INET, SOCK_STREAM, $proto);
|
|
247 my $prefip = $self->{pref_ip}{$ip};
|
|
248 $sin = Socket::sockaddr_in($port,Socket::inet_aton($prefip));
|
|
249 if (_connect_sock($sock,$sin,$self->{connect_timeout})) {
|
|
250 $connected = 1;
|
|
251 } else {
|
|
252 if (my $cb = $self->{cb_connect_fail}) {
|
|
253 $cb->($prefip);
|
|
254 }
|
|
255 close $sock;
|
|
256 }
|
|
257 }
|
|
258
|
|
259 # normal path, or fallback path if preferred IP failed
|
|
260 unless ($connected) {
|
|
261 socket($sock, PF_INET, SOCK_STREAM, $proto);
|
|
262 $sin = Socket::sockaddr_in($port,Socket::inet_aton($ip));
|
|
263 my $timeout = $self ? $self->{connect_timeout} : 0.25;
|
|
264 unless (_connect_sock($sock,$sin,$timeout)) {
|
|
265 my $cb = $self ? $self->{cb_connect_fail} : undef;
|
|
266 $cb->($ip) if $cb;
|
|
267 return _dead_sock($sock, undef, 20 + int(rand(10)));
|
|
268 }
|
|
269 }
|
|
270 } else { # it's a unix domain/local socket
|
|
271 socket($sock, PF_UNIX, SOCK_STREAM, 0);
|
|
272 $sin = Socket::sockaddr_un($host);
|
|
273 my $timeout = $self ? $self->{connect_timeout} : 0.25;
|
|
274 unless (_connect_sock($sock,$sin,$timeout)) {
|
|
275 my $cb = $self ? $self->{cb_connect_fail} : undef;
|
|
276 $cb->($host) if $cb;
|
|
277 return _dead_sock($sock, undef, 20 + int(rand(10)));
|
|
278 }
|
|
279 }
|
|
280
|
|
281 # make the new socket not buffer writes.
|
|
282 my $old = select($sock);
|
|
283 $| = 1;
|
|
284 select($old);
|
|
285
|
|
286 $sock_map{$sock} = $host;
|
|
287 $cache_sock{$host} = $sock;
|
|
288
|
|
289 return $sock;
|
|
290 }
|
|
291
|
|
292 sub get_sock { # (key)
|
|
293 my Cache::Memcached $self = $_[0];
|
|
294 my $key = $_[1];
|
|
295 return $self->sock_to_host($self->{'_single_sock'}) if $self->{'_single_sock'};
|
|
296 return undef unless $self->{'active'};
|
|
297 my $hv = ref $key ? int($key->[0]) : _hashfunc($key);
|
|
298
|
|
299 my $real_key = ref $key ? $key->[1] : $key;
|
|
300 my $tries = 0;
|
|
301 while ($tries++ < 20) {
|
|
302 my $host = $self->{'buckets'}->[$hv % $self->{'bucketcount'}];
|
|
303 my $sock = $self->sock_to_host($host);
|
|
304 return $sock if $sock;
|
|
305 return undef if $self->{'no_rehash'};
|
|
306 $hv += _hashfunc($tries . $real_key); # stupid, but works
|
|
307 }
|
|
308 return undef;
|
|
309 }
|
|
310
|
|
311 sub init_buckets {
|
|
312 my Cache::Memcached $self = shift;
|
|
313 return if $self->{'buckets'};
|
|
314 my $bu = $self->{'buckets'} = [];
|
|
315 foreach my $v (@{$self->{'servers'}}) {
|
|
316 if (ref $v eq "ARRAY") {
|
|
317 for (1..$v->[1]) { push @$bu, $v->[0]; }
|
|
318 } else {
|
|
319 push @$bu, $v;
|
|
320 }
|
|
321 }
|
|
322 $self->{'bucketcount'} = scalar @{$self->{'buckets'}};
|
|
323 }
|
|
324
|
|
325 sub disconnect_all {
|
|
326 my $sock;
|
|
327 foreach $sock (values %cache_sock) {
|
|
328 close $sock;
|
|
329 }
|
|
330 %cache_sock = ();
|
|
331 }
|
|
332
|
|
333 # writes a line, then reads result. by default stops reading after a
|
|
334 # single line, but caller can override the $check_complete subref,
|
|
335 # which gets passed a scalarref of buffer read thus far.
|
|
336 sub _write_and_read {
|
|
337 my Cache::Memcached $self = shift;
|
|
338 my ($sock, $line, $check_complete) = @_;
|
|
339 my $res;
|
|
340 my ($ret, $offset) = (undef, 0);
|
|
341
|
|
342 $check_complete ||= sub {
|
|
343 return (rindex($ret, "\r\n") + 2 == length($ret));
|
|
344 };
|
|
345
|
|
346 # state: 0 - writing, 1 - reading, 2 - done
|
|
347 my $state = 0;
|
|
348
|
|
349 # the bitsets for select
|
|
350 my ($rin, $rout, $win, $wout);
|
|
351 my $nfound;
|
|
352
|
|
353 my $copy_state = -1;
|
|
354 local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
|
|
355
|
|
356 # the select loop
|
|
357 while(1) {
|
|
358 if ($copy_state!=$state) {
|
|
359 last if $state==2;
|
|
360 ($rin, $win) = ('', '');
|
|
361 vec($rin, fileno($sock), 1) = 1 if $state==1;
|
|
362 vec($win, fileno($sock), 1) = 1 if $state==0;
|
|
363 $copy_state = $state;
|
|
364 }
|
|
365 $nfound = select($rout=$rin, $wout=$win, undef,
|
|
366 $self->{'select_timeout'});
|
|
367 last unless $nfound;
|
|
368
|
|
369 if (vec($wout, fileno($sock), 1)) {
|
|
370 $res = send($sock, $line, $FLAG_NOSIGNAL);
|
|
371 next
|
|
372 if not defined $res and $!==EWOULDBLOCK;
|
|
373 unless ($res > 0) {
|
|
374 _close_sock($sock);
|
|
375 return undef;
|
|
376 }
|
|
377 if ($res == length($line)) { # all sent
|
|
378 $state = 1;
|
|
379 } else { # we only succeeded in sending some of it
|
|
380 substr($line, 0, $res, ''); # delete the part we sent
|
|
381 }
|
|
382 }
|
|
383
|
|
384 if (vec($rout, fileno($sock), 1)) {
|
|
385 $res = sysread($sock, $ret, 255, $offset);
|
|
386 next
|
|
387 if !defined($res) and $!==EWOULDBLOCK;
|
|
388 if ($res == 0) { # catches 0=conn closed or undef=error
|
|
389 _close_sock($sock);
|
|
390 return undef;
|
|
391 }
|
|
392 $offset += $res;
|
|
393 $state = 2 if $check_complete->(\$ret);
|
|
394 }
|
|
395 }
|
|
396
|
|
397 unless ($state == 2) {
|
|
398 _dead_sock($sock); # improperly finished
|
|
399 return undef;
|
|
400 }
|
|
401
|
|
402 return $ret;
|
|
403 }
|
|
404
|
|
405 sub delete {
|
|
406 my Cache::Memcached $self = shift;
|
|
407 my ($key, $time) = @_;
|
|
408 return 0 if ! $self->{'active'} || $self->{'readonly'};
|
|
409 my $stime = Time::HiRes::time() if $self->{'stat_callback'};
|
|
410 my $sock = $self->get_sock($key);
|
|
411 return 0 unless $sock;
|
|
412
|
|
413 $self->{'stats'}->{"delete"}++;
|
|
414 $key = ref $key ? $key->[1] : $key;
|
|
415 $time = $time ? " $time" : "";
|
|
416 my $cmd = "delete $self->{namespace}$key$time\r\n";
|
|
417 my $res = _write_and_read($self, $sock, $cmd);
|
|
418
|
|
419 if ($self->{'stat_callback'}) {
|
|
420 my $etime = Time::HiRes::time();
|
|
421 $self->{'stat_callback'}->($stime, $etime, $sock, 'delete');
|
|
422 }
|
|
423
|
|
424 return $res eq "DELETED\r\n";
|
|
425 }
|
|
426 *remove = \&delete;
|
|
427
|
|
428 sub add {
|
|
429 _set("add", @_);
|
|
430 }
|
|
431
|
|
432 sub replace {
|
|
433 _set("replace", @_);
|
|
434 }
|
|
435
|
|
436 sub set {
|
|
437 _set("set", @_);
|
|
438 }
|
|
439
|
2
|
440 sub append {
|
|
441 _set("append", @_);
|
|
442 }
|
|
443
|
|
444 sub prepend {
|
|
445 _set("prepend", @_);
|
|
446 }
|
|
447
|
0
|
448 sub _set {
|
|
449 my $cmdname = shift;
|
|
450 my Cache::Memcached $self = shift;
|
|
451 my ($key, $val, $exptime) = @_;
|
|
452 return 0 if ! $self->{'active'} || $self->{'readonly'};
|
|
453 my $stime = Time::HiRes::time() if $self->{'stat_callback'};
|
|
454 my $sock = $self->get_sock($key);
|
|
455 return 0 unless $sock;
|
|
456
|
|
457 use bytes; # return bytes from length()
|
|
458
|
|
459 $self->{'stats'}->{$cmdname}++;
|
|
460 my $flags = 0;
|
|
461 $key = ref $key ? $key->[1] : $key;
|
|
462
|
|
463 if (ref $val) {
|
|
464 local $Carp::CarpLevel = 2;
|
|
465 $val = Storable::nfreeze($val);
|
|
466 $flags |= F_STORABLE;
|
|
467 }
|
|
468
|
|
469 my $len = length($val);
|
|
470
|
|
471 if ($self->{'compress_threshold'} && $HAVE_ZLIB && $self->{'compress_enable'} &&
|
|
472 $len >= $self->{'compress_threshold'}) {
|
|
473
|
|
474 my $c_val = Compress::Zlib::memGzip($val);
|
|
475 my $c_len = length($c_val);
|
|
476
|
|
477 # do we want to keep it?
|
|
478 if ($c_len < $len*(1 - COMPRESS_SAVINGS)) {
|
|
479 $val = $c_val;
|
|
480 $len = $c_len;
|
|
481 $flags |= F_COMPRESS;
|
|
482 }
|
|
483 }
|
|
484
|
|
485 $exptime = int($exptime || 0);
|
|
486
|
|
487 local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
|
|
488 my $line = "$cmdname $self->{namespace}$key $flags $exptime $len\r\n$val\r\n";
|
|
489
|
|
490 my $res = _write_and_read($self, $sock, $line);
|
|
491
|
|
492 if ($self->{'debug'} && $line) {
|
|
493 chop $line; chop $line;
|
|
494 print STDERR "Cache::Memcache: $cmdname $self->{namespace}$key = $val ($line)\n";
|
|
495 }
|
|
496
|
|
497 if ($self->{'stat_callback'}) {
|
|
498 my $etime = Time::HiRes::time();
|
|
499 $self->{'stat_callback'}->($stime, $etime, $sock, $cmdname);
|
|
500 }
|
|
501
|
|
502 return $res eq "STORED\r\n";
|
|
503 }
|
|
504
|
|
505 sub incr {
|
|
506 _incrdecr("incr", @_);
|
|
507 }
|
|
508
|
|
509 sub decr {
|
|
510 _incrdecr("decr", @_);
|
|
511 }
|
|
512
|
|
513 sub _incrdecr {
|
|
514 my $cmdname = shift;
|
|
515 my Cache::Memcached $self = shift;
|
|
516 my ($key, $value) = @_;
|
|
517 return undef if ! $self->{'active'} || $self->{'readonly'};
|
|
518 my $stime = Time::HiRes::time() if $self->{'stat_callback'};
|
|
519 my $sock = $self->get_sock($key);
|
|
520 return undef unless $sock;
|
|
521 $key = $key->[1] if ref $key;
|
|
522 $self->{'stats'}->{$cmdname}++;
|
|
523 $value = 1 unless defined $value;
|
|
524
|
|
525 my $line = "$cmdname $self->{namespace}$key $value\r\n";
|
|
526 my $res = _write_and_read($self, $sock, $line);
|
|
527
|
|
528 if ($self->{'stat_callback'}) {
|
|
529 my $etime = Time::HiRes::time();
|
|
530 $self->{'stat_callback'}->($stime, $etime, $sock, $cmdname);
|
|
531 }
|
|
532
|
|
533 return undef unless $res =~ /^(\d+)/;
|
|
534 return $1;
|
|
535 }
|
|
536
|
|
537 sub get {
|
|
538 my Cache::Memcached $self = $_[0];
|
|
539 my $key = $_[1];
|
|
540
|
|
541 # TODO: make a fast path for this? or just keep using get_multi?
|
|
542 my $r = $self->get_multi($key);
|
|
543 my $kval = ref $key ? $key->[1] : $key;
|
|
544 return $r->{$kval};
|
|
545 }
|
|
546
|
|
547 sub get_multi {
|
|
548 my Cache::Memcached $self = shift;
|
|
549 return {} unless $self->{'active'};
|
|
550 $self->{'_stime'} = Time::HiRes::time() if $self->{'stat_callback'};
|
|
551 $self->{'stats'}->{"get_multi"}++;
|
|
552
|
|
553 my %val; # what we'll be returning a reference to (realkey -> value)
|
|
554 my %sock_keys; # sockref_as_scalar -> [ realkeys ]
|
|
555 my $sock;
|
|
556
|
|
557 if ($self->{'_single_sock'}) {
|
|
558 $sock = $self->sock_to_host($self->{'_single_sock'});
|
|
559 unless ($sock) {
|
|
560 return {};
|
|
561 }
|
|
562 foreach my $key (@_) {
|
|
563 my $kval = ref $key ? $key->[1] : $key;
|
|
564 push @{$sock_keys{$sock}}, $kval;
|
|
565 }
|
|
566 } else {
|
|
567 my $bcount = $self->{'bucketcount'};
|
|
568 my $sock;
|
|
569 KEY:
|
|
570 foreach my $key (@_) {
|
|
571 my ($hv, $real_key) = ref $key ?
|
|
572 (int($key->[0]), $key->[1]) :
|
|
573 ((crc32($key) >> 16) & 0x7fff, $key);
|
|
574
|
|
575 my $tries;
|
|
576 while (1) {
|
|
577 my $bucket = $hv % $bcount;
|
|
578
|
|
579 # this segfaults perl 5.8.4 (and others?) if sock_to_host returns undef... wtf?
|
|
580 #$sock = $buck2sock[$bucket] ||= $self->sock_to_host($self->{buckets}[ $bucket ])
|
|
581 # and last;
|
|
582
|
|
583 # but this variant doesn't crash:
|
|
584 $sock = $buck2sock[$bucket] || $self->sock_to_host($self->{buckets}[ $bucket ]);
|
|
585 if ($sock) {
|
|
586 $buck2sock[$bucket] = $sock;
|
|
587 last;
|
|
588 }
|
|
589
|
|
590 next KEY if $tries++ >= 20;
|
|
591 $hv += _hashfunc($tries . $real_key);
|
|
592 }
|
|
593
|
|
594 push @{$sock_keys{$sock}}, $real_key;
|
|
595 }
|
|
596 }
|
|
597
|
|
598 $self->{'stats'}->{"get_keys"} += @_;
|
|
599 $self->{'stats'}->{"get_socks"} += keys %sock_keys;
|
|
600
|
|
601 local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
|
|
602
|
|
603 _load_multi($self, \%sock_keys, \%val);
|
|
604
|
|
605 if ($self->{'debug'}) {
|
|
606 while (my ($k, $v) = each %val) {
|
|
607 print STDERR "MemCache: got $k = $v\n";
|
|
608 }
|
|
609 }
|
|
610 return \%val;
|
|
611 }
|
|
612
|
|
613 sub _load_multi {
|
|
614 use bytes; # return bytes from length()
|
|
615 my Cache::Memcached $self;
|
|
616 my ($sock_keys, $ret);
|
|
617
|
|
618 ($self, $sock_keys, $ret) = @_;
|
|
619
|
|
620 # all keyed by $sockstr:
|
|
621 my %reading; # $sockstr -> $sock. bool, whether we're reading from this socket
|
|
622 my %writing; # $sockstr -> $sock. bool, whether we're writing to this socket
|
|
623 my %buf; # buffers, for writing
|
|
624
|
|
625 my %parser; # $sockstr -> Cache::Memcached::GetParser
|
|
626
|
|
627 my $active_changed = 1; # force rebuilding of select sets
|
|
628
|
|
629 my $dead = sub {
|
|
630 my $sock = shift;
|
|
631 print STDERR "killing socket $sock\n" if $self->{'debug'} >= 2;
|
|
632 delete $reading{$sock};
|
|
633 delete $writing{$sock};
|
|
634
|
|
635 if (my $p = $parser{$sock}) {
|
|
636 my $key = $p->current_key;
|
|
637 delete $ret->{$key} if $key;
|
|
638 }
|
|
639
|
|
640 if ($self->{'stat_callback'}) {
|
|
641 my $etime = Time::HiRes::time();
|
|
642 $self->{'stat_callback'}->($self->{'_stime'}, $etime, $sock, 'get_multi');
|
|
643 }
|
|
644
|
|
645 close $sock;
|
|
646 _dead_sock($sock);
|
|
647 };
|
|
648
|
|
649 # $finalize->($key, $flags)
|
|
650 # $finalize->({ $key => $flags, $key => $flags });
|
|
651 my $finalize = sub {
|
|
652 my $map = $_[0];
|
|
653 $map = {@_} unless ref $map;
|
|
654
|
|
655 while (my ($k, $flags) = each %$map) {
|
|
656
|
|
657 # remove trailing \r\n
|
|
658 chop $ret->{$k}; chop $ret->{$k};
|
|
659
|
|
660 $ret->{$k} = Compress::Zlib::memGunzip($ret->{$k})
|
|
661 if $HAVE_ZLIB && $flags & F_COMPRESS;
|
|
662 if ($flags & F_STORABLE) {
|
|
663 # wrapped in eval in case a perl 5.6 Storable tries to
|
|
664 # unthaw data from a perl 5.8 Storable. (5.6 is stupid
|
|
665 # and dies if the version number changes at all. in 5.8
|
|
666 # they made it only die if it unencounters a new feature)
|
|
667 eval {
|
|
668 $ret->{$k} = Storable::thaw($ret->{$k});
|
|
669 };
|
|
670 # so if there was a problem, just treat it as a cache miss.
|
|
671 if ($@) {
|
|
672 delete $ret->{$k};
|
|
673 }
|
|
674 }
|
|
675 }
|
|
676 };
|
|
677
|
|
678 foreach (keys %$sock_keys) {
|
|
679 my $ipport = $sock_map{$_} or die "No map found matching for $_";
|
|
680 my $sock = $cache_sock{$ipport} or die "No sock found for $ipport";
|
|
681 print STDERR "processing socket $_\n" if $self->{'debug'} >= 2;
|
|
682 $writing{$_} = $sock;
|
|
683 if ($self->{namespace}) {
|
|
684 $buf{$_} = join(" ", 'get', (map { "$self->{namespace}$_" } @{$sock_keys->{$_}}), "\r\n");
|
|
685 } else {
|
|
686 $buf{$_} = join(" ", 'get', @{$sock_keys->{$_}}, "\r\n");
|
|
687 }
|
|
688
|
|
689 $parser{$_} = $self->{parser_class}->new($ret, $self->{namespace_len}, $finalize);
|
|
690 }
|
|
691
|
|
692 my $read = sub {
|
|
693 my $sockstr = "$_[0]"; # $sock is $_[0];
|
|
694 my $p = $parser{$sockstr} or die;
|
|
695 my $rv = $p->parse_from_sock($_[0]);
|
|
696 if ($rv > 0) {
|
|
697 # okay, finished with this socket
|
|
698 delete $reading{$sockstr};
|
|
699 } elsif ($rv < 0) {
|
|
700 $dead->($_[0]);
|
|
701 }
|
|
702 return $rv;
|
|
703 };
|
|
704
|
|
705 # returns 1 when it's done, for success or error. 0 if still working.
|
|
706 my $write = sub {
|
|
707 my ($sock, $sockstr) = ($_[0], "$_[0]");
|
|
708 my $res;
|
|
709
|
|
710 $res = send($sock, $buf{$sockstr}, $FLAG_NOSIGNAL);
|
|
711
|
|
712 return 0
|
|
713 if not defined $res and $!==EWOULDBLOCK;
|
|
714 unless ($res > 0) {
|
|
715 $dead->($sock);
|
|
716 return 1;
|
|
717 }
|
|
718 if ($res == length($buf{$sockstr})) { # all sent
|
|
719 $buf{$sockstr} = "";
|
|
720
|
|
721 # switch the socket from writing to reading
|
|
722 delete $writing{$sockstr};
|
|
723 $reading{$sockstr} = $sock;
|
|
724 return 1;
|
|
725 } else { # we only succeeded in sending some of it
|
|
726 substr($buf{$sockstr}, 0, $res, ''); # delete the part we sent
|
|
727 }
|
|
728 return 0;
|
|
729 };
|
|
730
|
|
731 # the bitsets for select
|
|
732 my ($rin, $rout, $win, $wout);
|
|
733 my $nfound;
|
|
734
|
|
735 # the big select loop
|
|
736 while(1) {
|
|
737 if ($active_changed) {
|
|
738 last unless %reading or %writing; # no sockets left?
|
|
739 ($rin, $win) = ('', '');
|
|
740 foreach (values %reading) {
|
|
741 vec($rin, fileno($_), 1) = 1;
|
|
742 }
|
|
743 foreach (values %writing) {
|
|
744 vec($win, fileno($_), 1) = 1;
|
|
745 }
|
|
746 $active_changed = 0;
|
|
747 }
|
|
748 # TODO: more intelligent cumulative timeout?
|
|
749 # TODO: select is interruptible w/ ptrace attach, signal, etc. should note that.
|
|
750 $nfound = select($rout=$rin, $wout=$win, undef,
|
|
751 $self->{'select_timeout'});
|
|
752 last unless $nfound;
|
|
753
|
|
754 # TODO: possible robustness improvement: we could select
|
|
755 # writing sockets for reading also, and raise hell if they're
|
|
756 # ready (input unread from last time, etc.)
|
|
757 # maybe do that on the first loop only?
|
|
758 foreach (values %writing) {
|
|
759 if (vec($wout, fileno($_), 1)) {
|
|
760 $active_changed = 1 if $write->($_);
|
|
761 }
|
|
762 }
|
|
763 foreach (values %reading) {
|
|
764 if (vec($rout, fileno($_), 1)) {
|
|
765 $active_changed = 1 if $read->($_);
|
|
766 }
|
|
767 }
|
|
768 }
|
|
769
|
|
770 # if there're active sockets left, they need to die
|
|
771 foreach (values %writing) {
|
|
772 $dead->($_);
|
|
773 }
|
|
774 foreach (values %reading) {
|
|
775 $dead->($_);
|
|
776 }
|
|
777
|
|
778 return;
|
|
779 }
|
|
780
|
|
781 sub _hashfunc {
|
|
782 return (crc32($_[0]) >> 16) & 0x7fff;
|
|
783 }
|
|
784
|
|
785 sub flush_all {
|
|
786 my Cache::Memcached $self = shift;
|
|
787
|
|
788 my $success = 1;
|
|
789
|
|
790 my @hosts = @{$self->{'buckets'}};
|
|
791 foreach my $host (@hosts) {
|
|
792 my $sock = $self->sock_to_host($host);
|
|
793 my @res = $self->run_command($sock, "flush_all\r\n");
|
|
794 $success = 0 unless (@res);
|
|
795 }
|
|
796
|
|
797 return $success;
|
|
798 }
|
|
799
|
|
800 # returns array of lines, or () on failure.
|
|
801 sub run_command {
|
|
802 my Cache::Memcached $self = shift;
|
|
803 my ($sock, $cmd) = @_;
|
|
804 return () unless $sock;
|
|
805 my $ret;
|
|
806 my $line = $cmd;
|
|
807 while (my $res = _write_and_read($self, $sock, $line)) {
|
|
808 undef $line;
|
|
809 $ret .= $res;
|
|
810 last if $ret =~ /(?:OK|END|ERROR)\r\n$/;
|
|
811 }
|
|
812 chop $ret; chop $ret;
|
|
813 return map { "$_\r\n" } split(/\r\n/, $ret);
|
|
814 }
|
|
815
|
|
816 sub stats {
|
|
817 my Cache::Memcached $self = shift;
|
|
818 my ($types) = @_;
|
|
819 return 0 unless $self->{'active'};
|
|
820 return 0 unless !ref($types) || ref($types) eq 'ARRAY';
|
|
821 if (!ref($types)) {
|
|
822 if (!$types) {
|
|
823 # I don't much care what the default is, it should just
|
|
824 # be something reasonable. Obviously "reset" should not
|
|
825 # be on the list :) but other types that might go in here
|
|
826 # include maps, cachedump, slabs, or items.
|
|
827 $types = [ qw( misc malloc sizes self ) ];
|
|
828 } else {
|
|
829 $types = [ $types ];
|
|
830 }
|
|
831 }
|
|
832
|
|
833 my $stats_hr = { };
|
|
834
|
|
835 # The "self" stat type is special, it only applies to this very
|
|
836 # object.
|
|
837 if (grep /^self$/, @$types) {
|
|
838 $stats_hr->{'self'} = \%{ $self->{'stats'} };
|
|
839 }
|
|
840
|
|
841 my %misc_keys = map { $_ => 1 }
|
|
842 qw/ bytes bytes_read bytes_written
|
|
843 cmd_get cmd_set connection_structures curr_items
|
|
844 get_hits get_misses
|
|
845 total_connections total_items
|
|
846 /;
|
|
847
|
|
848 # Now handle the other types, passing each type to each host server.
|
|
849 my @hosts = @{$self->{'buckets'}};
|
|
850 HOST: foreach my $host (@hosts) {
|
|
851 my $sock = $self->sock_to_host($host);
|
|
852 TYPE: foreach my $typename (grep !/^self$/, @$types) {
|
|
853 my $type = $typename eq 'misc' ? "" : " $typename";
|
|
854 my $lines = _write_and_read($self, $sock, "stats$type\r\n", sub {
|
|
855 my $bref = shift;
|
|
856 return $$bref =~ /^(?:END|ERROR)\r?\n/m;
|
|
857 });
|
|
858 unless ($lines) {
|
|
859 _dead_sock($sock);
|
|
860 next HOST;
|
|
861 }
|
|
862
|
|
863 $lines =~ s/\0//g; # 'stats sizes' starts with NULL?
|
|
864
|
|
865 # And, most lines end in \r\n but 'stats maps' (as of
|
|
866 # July 2003 at least) ends in \n. ??
|
|
867 my @lines = split(/\r?\n/, $lines);
|
|
868
|
|
869 # Some stats are key-value, some are not. malloc,
|
|
870 # sizes, and the empty string are key-value.
|
|
871 # ("self" was handled separately above.)
|
|
872 if ($typename =~ /^(malloc|sizes|misc)$/) {
|
|
873 # This stat is key-value.
|
|
874 foreach my $line (@lines) {
|
|
875 my ($key, $value) = $line =~ /^(?:STAT )?(\w+)\s(.*)/;
|
|
876 if ($key) {
|
|
877 $stats_hr->{'hosts'}{$host}{$typename}{$key} = $value;
|
|
878 }
|
|
879 $stats_hr->{'total'}{$key} += $value
|
|
880 if $typename eq 'misc' && $key && $misc_keys{$key};
|
|
881 $stats_hr->{'total'}{"malloc_$key"} += $value
|
|
882 if $typename eq 'malloc' && $key;
|
|
883 }
|
|
884 } else {
|
|
885 # This stat is not key-value so just pull it
|
|
886 # all out in one blob.
|
|
887 $lines =~ s/^END\r?\n//m;
|
|
888 $stats_hr->{'hosts'}{$host}{$typename} ||= "";
|
|
889 $stats_hr->{'hosts'}{$host}{$typename} .= "$lines";
|
|
890 }
|
|
891 }
|
|
892 }
|
|
893
|
|
894 return $stats_hr;
|
|
895 }
|
|
896
|
|
897 sub stats_reset {
|
|
898 my Cache::Memcached $self = shift;
|
|
899 my ($types) = @_;
|
|
900 return 0 unless $self->{'active'};
|
|
901
|
|
902 HOST: foreach my $host (@{$self->{'buckets'}}) {
|
|
903 my $sock = $self->sock_to_host($host);
|
|
904 my $ok = _write_and_read($self, $sock, "stats reset");
|
|
905 unless ($ok eq "RESET\r\n") {
|
|
906 _dead_sock($sock);
|
|
907 }
|
|
908 }
|
|
909 return 1;
|
|
910 }
|
|
911
|
|
912 1;
|
|
913 __END__
|
|
914
|
|
915 =head1 NAME
|
|
916
|
|
917 Cache::Memcached - client library for memcached (memory cache daemon)
|
|
918
|
|
919 =head1 SYNOPSIS
|
|
920
|
|
921 use Cache::Memcached;
|
|
922
|
|
923 $memd = new Cache::Memcached {
|
|
924 'servers' => [ "10.0.0.15:11211", "10.0.0.15:11212", "/var/sock/memcached",
|
|
925 "10.0.0.17:11211", [ "10.0.0.17:11211", 3 ] ],
|
|
926 'debug' => 0,
|
|
927 'compress_threshold' => 10_000,
|
|
928 };
|
|
929 $memd->set_servers($array_ref);
|
|
930 $memd->set_compress_threshold(10_000);
|
|
931 $memd->enable_compress(0);
|
|
932
|
|
933 $memd->set("my_key", "Some value");
|
|
934 $memd->set("object_key", { 'complex' => [ "object", 2, 4 ]});
|
|
935
|
|
936 $val = $memd->get("my_key");
|
|
937 $val = $memd->get("object_key");
|
|
938 if ($val) { print $val->{'complex'}->[2]; }
|
|
939
|
|
940 $memd->incr("key");
|
|
941 $memd->decr("key");
|
|
942 $memd->incr("key", 2);
|
|
943
|
|
944 =head1 DESCRIPTION
|
|
945
|
|
946 This is the Perl API for memcached, a distributed memory cache daemon.
|
|
947 More information is available at:
|
|
948
|
|
949 http://www.danga.com/memcached/
|
|
950
|
|
951 =head1 CONSTRUCTOR
|
|
952
|
|
953 =over 4
|
|
954
|
|
955 =item C<new>
|
|
956
|
|
957 Takes one parameter, a hashref of options. The most important key is
|
|
958 C<servers>, but that can also be set later with the C<set_servers>
|
|
959 method. The servers must be an arrayref of hosts, each of which is
|
|
960 either a scalar of the form C<10.0.0.10:11211> or an arrayref of the
|
|
961 former and an integer weight value. (The default weight if
|
|
962 unspecified is 1.) It's recommended that weight values be kept as low
|
|
963 as possible, as this module currently allocates memory for bucket
|
|
964 distribution proportional to the total host weights.
|
|
965
|
|
966 Use C<compress_threshold> to set a compression threshold, in bytes.
|
|
967 Values larger than this threshold will be compressed by C<set> and
|
|
968 decompressed by C<get>.
|
|
969
|
|
970 Use C<no_rehash> to disable finding a new memcached server when one
|
|
971 goes down. Your application may or may not need this, depending on
|
|
972 your expirations and key usage.
|
|
973
|
|
974 Use C<readonly> to disable writes to backend memcached servers. Only
|
|
975 get and get_multi will work. This is useful in bizarre debug and
|
|
976 profiling cases only.
|
|
977
|
|
978 Use C<namespace> to prefix all keys with the provided namespace value.
|
|
979 That is, if you set namespace to "app1:" and later do a set of "foo"
|
|
980 to "bar", memcached is actually seeing you set "app1:foo" to "bar".
|
|
981
|
|
982 The other useful key is C<debug>, which when set to true will produce
|
|
983 diagnostics on STDERR.
|
|
984
|
|
985 =back
|
|
986
|
|
987 =head1 METHODS
|
|
988
|
|
989 =over 4
|
|
990
|
|
991 =item C<set_servers>
|
|
992
|
|
993 Sets the server list this module distributes key gets and sets between.
|
|
994 The format is an arrayref of identical form as described in the C<new>
|
|
995 constructor.
|
|
996
|
|
997 =item C<set_debug>
|
|
998
|
|
999 Sets the C<debug> flag. See C<new> constructor for more information.
|
|
1000
|
|
1001 =item C<set_readonly>
|
|
1002
|
|
1003 Sets the C<readonly> flag. See C<new> constructor for more information.
|
|
1004
|
|
1005 =item C<set_norehash>
|
|
1006
|
|
1007 Sets the C<no_rehash> flag. See C<new> constructor for more information.
|
|
1008
|
|
1009 =item C<set_compress_threshold>
|
|
1010
|
|
1011 Sets the compression threshold. See C<new> constructor for more information.
|
|
1012
|
|
1013 =item C<enable_compress>
|
|
1014
|
|
1015 Temporarily enable or disable compression. Has no effect if C<compress_threshold>
|
|
1016 isn't set, but has an overriding effect if it is.
|
|
1017
|
|
1018 =item C<get>
|
|
1019
|
|
1020 my $val = $memd->get($key);
|
|
1021
|
|
1022 Retrieves a key from the memcache. Returns the value (automatically
|
|
1023 thawed with Storable, if necessary) or undef.
|
|
1024
|
|
1025 The $key can optionally be an arrayref, with the first element being the
|
|
1026 hash value, if you want to avoid making this module calculate a hash
|
|
1027 value. You may prefer, for example, to keep all of a given user's
|
|
1028 objects on the same memcache server, so you could use the user's
|
|
1029 unique id as the hash value.
|
|
1030
|
|
1031 =item C<get_multi>
|
|
1032
|
|
1033 my $hashref = $memd->get_multi(@keys);
|
|
1034
|
|
1035 Retrieves multiple keys from the memcache doing just one query.
|
|
1036 Returns a hashref of key/value pairs that were available.
|
|
1037
|
|
1038 This method is recommended over regular 'get' as it lowers the number
|
|
1039 of total packets flying around your network, reducing total latency,
|
|
1040 since your app doesn't have to wait for each round-trip of 'get'
|
|
1041 before sending the next one.
|
|
1042
|
|
1043 =item C<set>
|
|
1044
|
|
1045 $memd->set($key, $value[, $exptime]);
|
|
1046
|
|
1047 Unconditionally sets a key to a given value in the memcache. Returns true
|
|
1048 if it was stored successfully.
|
|
1049
|
|
1050 The $key can optionally be an arrayref, with the first element being the
|
|
1051 hash value, as described above.
|
|
1052
|
|
1053 The $exptime (expiration time) defaults to "never" if unspecified. If
|
|
1054 you want the key to expire in memcached, pass an integer $exptime. If
|
|
1055 value is less than 60*60*24*30 (30 days), time is assumed to be relative
|
|
1056 from the present. If larger, it's considered an absolute Unix time.
|
|
1057
|
|
1058 =item C<add>
|
|
1059
|
|
1060 $memd->add($key, $value[, $exptime]);
|
|
1061
|
|
1062 Like C<set>, but only stores in memcache if the key doesn't already exist.
|
|
1063
|
|
1064 =item C<replace>
|
|
1065
|
|
1066 $memd->replace($key, $value[, $exptime]);
|
|
1067
|
|
1068 Like C<set>, but only stores in memcache if the key already exists. The
|
|
1069 opposite of C<add>.
|
|
1070
|
|
1071 =item C<delete>
|
|
1072
|
|
1073 $memd->delete($key[, $time]);
|
|
1074
|
|
1075 Deletes a key. You may optionally provide an integer time value (in seconds) to
|
|
1076 tell the memcached server to block new writes to this key for that many seconds.
|
|
1077 (Sometimes useful as a hacky means to prevent races.) Returns true if key
|
|
1078 was found and deleted, and false otherwise.
|
|
1079
|
|
1080 You may also use the alternate method name B<remove>, so
|
|
1081 Cache::Memcached looks like the L<Cache::Cache> API.
|
|
1082
|
|
1083 =item C<incr>
|
|
1084
|
|
1085 $memd->incr($key[, $value]);
|
|
1086
|
|
1087 Sends a command to the server to atomically increment the value for
|
|
1088 $key by $value, or by 1 if $value is undefined. Returns undef if $key
|
|
1089 doesn't exist on server, otherwise it returns the new value after
|
|
1090 incrementing. Value should be zero or greater. Overflow on server
|
|
1091 is not checked. Be aware of values approaching 2**32. See decr.
|
|
1092
|
|
1093 =item C<decr>
|
|
1094
|
|
1095 $memd->decr($key[, $value]);
|
|
1096
|
|
1097 Like incr, but decrements. Unlike incr, underflow is checked and new
|
|
1098 values are capped at 0. If server value is 1, a decrement of 2
|
|
1099 returns 0, not -1.
|
|
1100
|
|
1101 =item C<stats>
|
|
1102
|
|
1103 $memd->stats([$keys]);
|
|
1104
|
|
1105 Returns a hashref of statistical data regarding the memcache server(s),
|
|
1106 the $memd object, or both. $keys can be an arrayref of keys wanted, a
|
|
1107 single key wanted, or absent (in which case the default value is malloc,
|
|
1108 sizes, self, and the empty string). These keys are the values passed
|
|
1109 to the 'stats' command issued to the memcached server(s), except for
|
|
1110 'self' which is internal to the $memd object. Allowed values are:
|
|
1111
|
|
1112 =over 4
|
|
1113
|
|
1114 =item C<misc>
|
|
1115
|
|
1116 The stats returned by a 'stats' command: pid, uptime, version,
|
|
1117 bytes, get_hits, etc.
|
|
1118
|
|
1119 =item C<malloc>
|
|
1120
|
|
1121 The stats returned by a 'stats malloc': total_alloc, arena_size, etc.
|
|
1122
|
|
1123 =item C<sizes>
|
|
1124
|
|
1125 The stats returned by a 'stats sizes'.
|
|
1126
|
|
1127 =item C<self>
|
|
1128
|
|
1129 The stats for the $memd object itself (a copy of $memd->{'stats'}).
|
|
1130
|
|
1131 =item C<maps>
|
|
1132
|
|
1133 The stats returned by a 'stats maps'.
|
|
1134
|
|
1135 =item C<cachedump>
|
|
1136
|
|
1137 The stats returned by a 'stats cachedump'.
|
|
1138
|
|
1139 =item C<slabs>
|
|
1140
|
|
1141 The stats returned by a 'stats slabs'.
|
|
1142
|
|
1143 =item C<items>
|
|
1144
|
|
1145 The stats returned by a 'stats items'.
|
|
1146
|
|
1147 =back
|
|
1148
|
|
1149 =item C<disconnect_all>
|
|
1150
|
|
1151 $memd->disconnect_all;
|
|
1152
|
|
1153 Closes all cached sockets to all memcached servers. You must do this
|
|
1154 if your program forks and the parent has used this module at all.
|
|
1155 Otherwise the children will try to use cached sockets and they'll fight
|
|
1156 (as children do) and garble the client/server protocol.
|
|
1157
|
|
1158 =item C<flush_all>
|
|
1159
|
|
1160 $memd->flush_all;
|
|
1161
|
|
1162 Runs the memcached "flush_all" command on all configured hosts,
|
|
1163 emptying all their caches. (or rather, invalidating all items
|
|
1164 in the caches in an O(1) operation...) Running stats will still
|
|
1165 show the item existing, they're just be non-existent and lazily
|
|
1166 destroyed next time you try to detch any of them.
|
|
1167
|
|
1168 =back
|
|
1169
|
|
1170 =head1 BUGS
|
|
1171
|
|
1172 When a server goes down, this module does detect it, and re-hashes the
|
|
1173 request to the remaining servers, but the way it does it isn't very
|
|
1174 clean. The result may be that it gives up during its rehashing and
|
|
1175 refuses to get/set something it could've, had it been done right.
|
|
1176
|
|
1177 =head1 COPYRIGHT
|
|
1178
|
|
1179 This module is Copyright (c) 2003 Brad Fitzpatrick.
|
|
1180 All rights reserved.
|
|
1181
|
|
1182 You may distribute under the terms of either the GNU General Public
|
|
1183 License or the Artistic License, as specified in the Perl README file.
|
|
1184
|
|
1185 =head1 WARRANTY
|
|
1186
|
|
1187 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
|
|
1188
|
|
1189 =head1 FAQ
|
|
1190
|
|
1191 See the memcached website:
|
|
1192 http://www.danga.com/memcached/
|
|
1193
|
|
1194 =head1 AUTHORS
|
|
1195
|
|
1196 Brad Fitzpatrick <brad@danga.com>
|
|
1197
|
|
1198 Anatoly Vorobey <mellon@pobox.com>
|
|
1199
|
|
1200 Brad Whitaker <whitaker@danga.com>
|
|
1201
|
|
1202 Jamie McCarthy <jamie@mccarthy.vg>
|