Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(386)

Side by Side Diff: lib/Cache/Memcached.pm

Issue 22052: Added method to obtain most recent result message (NOT_FOUND etc). Base URL: http://code.sixapart.com/svn/memcached/trunk/api/perl/
Patch Set: Created 16 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # $Id$ 1 # $Id$
2 # 2 #
3 # Copyright (c) 2003, 2004 Brad Fitzpatrick <brad@danga.com> 3 # Copyright (c) 2003, 2004 Brad Fitzpatrick <brad@danga.com>
4 # 4 #
5 # See COPYRIGHT section in pod text below for usage and distribution rights. 5 # See COPYRIGHT section in pod text below for usage and distribution rights.
6 # 6 #
7 7
8 package Cache::Memcached; 8 package Cache::Memcached;
9 9
10 use strict; 10 use strict;
11 use warnings; 11 use warnings;
12 12
13 no strict 'refs'; 13 no strict 'refs';
14 use Storable (); 14 use Storable ();
15 use Socket qw( MSG_NOSIGNAL PF_INET PF_UNIX IPPROTO_TCP SOCK_STREAM ); 15 use Socket qw( MSG_NOSIGNAL PF_INET PF_UNIX IPPROTO_TCP SOCK_STREAM );
16 use IO::Handle (); 16 use IO::Handle ();
17 use Time::HiRes (); 17 use Time::HiRes ();
18 use String::CRC32; 18 use String::CRC32;
19 use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN ); 19 use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN );
20 use Cache::Memcached::GetParser; 20 use Cache::Memcached::GetParser;
21 use fields qw{ 21 use fields qw{
22 debug no_rehash stats compress_threshold compress_enable stat_callback 22 debug no_rehash stats compress_threshold compress_enable stat_callback
23 readonly select_timeout namespace namespace_len servers active buckets 23 readonly select_timeout namespace namespace_len servers active buckets
24 pref_ip 24 pref_ip
25 bucketcount _single_sock _stime 25 bucketcount _single_sock _stime
26 connect_timeout cb_connect_fail 26 connect_timeout cb_connect_fail
27 parser_class 27 parser_class
28 last_res
28 }; 29 };
29 30
30 # flag definitions 31 # flag definitions
31 use constant F_STORABLE => 1; 32 use constant F_STORABLE => 1;
32 use constant F_COMPRESS => 2; 33 use constant F_COMPRESS => 2;
33 34
34 # size savings required before saving compressed value 35 # size savings required before saving compressed value
35 use constant COMPRESS_SAVINGS => 0.20; # percent 36 use constant COMPRESS_SAVINGS => 0.20; # percent
36 37
37 use vars qw($VERSION $HAVE_ZLIB $FLAG_NOSIGNAL); 38 use vars qw($VERSION $HAVE_ZLIB $FLAG_NOSIGNAL);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 80
80 # TODO: undocumented 81 # TODO: undocumented
81 $self->{'connect_timeout'} = $args->{'connect_timeout'} || 0.25; 82 $self->{'connect_timeout'} = $args->{'connect_timeout'} || 0.25;
82 $self->{'select_timeout'} = $args->{'select_timeout'} || 1.0; 83 $self->{'select_timeout'} = $args->{'select_timeout'} || 1.0;
83 $self->{namespace} = $args->{namespace} || ''; 84 $self->{namespace} = $args->{namespace} || '';
84 $self->{namespace_len} = length $self->{namespace}; 85 $self->{namespace_len} = length $self->{namespace};
85 86
86 return $self; 87 return $self;
87 } 88 }
88 89
90 sub last_res {
brad_danga_com 2009/02/28 15:58:41 "res" is a great internal abbreviations but it's a
markaufflick 2009/03/02 00:04:12 Fair point - i'll change that. On 2009/02/28 15:5
91 my Cache::Memcached $self = shift;
92
93 return $self->{last_res};
94 }
95
89 sub set_pref_ip { 96 sub set_pref_ip {
90 my Cache::Memcached $self = shift; 97 my Cache::Memcached $self = shift;
91 $self->{'pref_ip'} = shift; 98 $self->{'pref_ip'} = shift;
92 } 99 }
93 100
94 sub set_servers { 101 sub set_servers {
95 my Cache::Memcached $self = shift; 102 my Cache::Memcached $self = shift;
96 my ($list) = @_; 103 my ($list) = @_;
97 $self->{'servers'} = $list || []; 104 $self->{'servers'} = $list || [];
98 $self->{'active'} = scalar @{$self->{'servers'}}; 105 $self->{'active'} = scalar @{$self->{'servers'}};
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 341
335 # writes a line, then reads result. by default stops reading after a 342 # writes a line, then reads result. by default stops reading after a
336 # single line, but caller can override the $check_complete subref, 343 # single line, but caller can override the $check_complete subref,
337 # which gets passed a scalarref of buffer read thus far. 344 # which gets passed a scalarref of buffer read thus far.
338 sub _write_and_read { 345 sub _write_and_read {
339 my Cache::Memcached $self = shift; 346 my Cache::Memcached $self = shift;
340 my ($sock, $line, $check_complete) = @_; 347 my ($sock, $line, $check_complete) = @_;
341 my $res; 348 my $res;
342 my ($ret, $offset) = (undef, 0); 349 my ($ret, $offset) = (undef, 0);
343 350
351 $self->{last_res} = undef;
352
344 $check_complete ||= sub { 353 $check_complete ||= sub {
345 return (rindex($ret, "\r\n") + 2 == length($ret)); 354 return (rindex($ret, "\r\n") + 2 == length($ret));
346 }; 355 };
347 356
348 # state: 0 - writing, 1 - reading, 2 - done 357 # state: 0 - writing, 1 - reading, 2 - done
349 my $state = 0; 358 my $state = 0;
350 359
351 # the bitsets for select 360 # the bitsets for select
352 my ($rin, $rout, $win, $wout); 361 my ($rin, $rout, $win, $wout);
353 my $nfound; 362 my $nfound;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 $offset += $res; 403 $offset += $res;
395 $state = 2 if $check_complete->(\$ret); 404 $state = 2 if $check_complete->(\$ret);
396 } 405 }
397 } 406 }
398 407
399 unless ($state == 2) { 408 unless ($state == 2) {
400 _dead_sock($sock); # improperly finished 409 _dead_sock($sock); # improperly finished
401 return undef; 410 return undef;
402 } 411 }
403 412
413 $self->{last_res} = $ret;
414 $self->{last_res} =~ s/\r\n$//;
brad_danga_com 2009/02/28 15:58:41 Performance suggestion: It might make more sense
markaufflick 2009/03/02 00:04:12 Also a good suggestion. On 2009/02/28 15:58:41, b
415
404 return $ret; 416 return $ret;
405 } 417 }
406 418
407 sub delete { 419 sub delete {
408 my Cache::Memcached $self = shift; 420 my Cache::Memcached $self = shift;
409 my ($key, $time) = @_; 421 my ($key, $time) = @_;
410 return 0 if ! $self->{'active'} || $self->{'readonly'}; 422 return 0 if ! $self->{'active'} || $self->{'readonly'};
411 my $stime = Time::HiRes::time() if $self->{'stat_callback'}; 423 my $stime = Time::HiRes::time() if $self->{'stat_callback'};
412 my $sock = $self->get_sock($key); 424 my $sock = $self->get_sock($key);
413 return 0 unless $sock; 425 return 0 unless $sock;
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
1086 is not checked. Be aware of values approaching 2**32. See decr. 1098 is not checked. Be aware of values approaching 2**32. See decr.
1087 1099
1088 =item C<decr> 1100 =item C<decr>
1089 1101
1090 $memd->decr($key[, $value]); 1102 $memd->decr($key[, $value]);
1091 1103
1092 Like incr, but decrements. Unlike incr, underflow is checked and new 1104 Like incr, but decrements. Unlike incr, underflow is checked and new
1093 values are capped at 0. If server value is 1, a decrement of 2 1105 values are capped at 0. If server value is 1, a decrement of 2
1094 returns 0, not -1. 1106 returns 0, not -1.
1095 1107
1108 =item C<last_res>
1109
1110 $memd->last_res
1111
1112 This method returns the status string of the last operation except for get(s).
brad_danga_com 2009/02/28 15:58:41 So this only works with the ASCII protocol? You s
markaufflick 2009/03/02 00:04:12 The whole module has no support or mention of the
1113 This allows you, for instance, to differentiate between C<NOT_FOUND> and a
1114 protocol error. The trailing \r\n has already been removed. See the
1115 memcached protocol docs for possible return values for each command:
1116 L<http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt>
1117
1096 =item C<stats> 1118 =item C<stats>
1097 1119
1098 $memd->stats([$keys]); 1120 $memd->stats([$keys]);
1099 1121
1100 Returns a hashref of statistical data regarding the memcache server(s), 1122 Returns a hashref of statistical data regarding the memcache server(s),
1101 the $memd object, or both. $keys can be an arrayref of keys wanted, a 1123 the $memd object, or both. $keys can be an arrayref of keys wanted, a
1102 single key wanted, or absent (in which case the default value is malloc, 1124 single key wanted, or absent (in which case the default value is malloc,
1103 sizes, self, and the empty string). These keys are the values passed 1125 sizes, self, and the empty string). These keys are the values passed
1104 to the 'stats' command issued to the memcached server(s), except for 1126 to the 'stats' command issued to the memcached server(s), except for
1105 'self' which is internal to the $memd object. Allowed values are: 1127 'self' which is internal to the $memd object. Allowed values are:
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1188 1210
1189 =head1 AUTHORS 1211 =head1 AUTHORS
1190 1212
1191 Brad Fitzpatrick <brad@danga.com> 1213 Brad Fitzpatrick <brad@danga.com>
1192 1214
1193 Anatoly Vorobey <mellon@pobox.com> 1215 Anatoly Vorobey <mellon@pobox.com>
1194 1216
1195 Brad Whitaker <whitaker@danga.com> 1217 Brad Whitaker <whitaker@danga.com>
1196 1218
1197 Jamie McCarthy <jamie@mccarthy.vg> 1219 Jamie McCarthy <jamie@mccarthy.vg>
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b