Perl API stats problem (includes fix)

Andrew Barrow andrew at myinternet.com.au
Wed Jun 8 16:15:51 PDT 2005


We've recently changed over our caching libraries to use Memcached, and the 
Perl API for Memcached. One of the cache stats we like to keep track of, is 
the number of items in the cache. Thankfully, Memcached provides these stats, 
however the Perl API doesn't correctly parse them. When I try to get the 
'misc' stats, it only returns the 'pid'. It should return a large number of 
generic statistics, including the number of items in the cache.

The problem appears to be in the '_oneline' method of 'Cache::Memcached'. The 
way the method is being used in this library, it appears that it is supposed 
to return one line from the socket at a time. However, instead it reads all 
of the information from the socket, and returns it in a single line (the line 
does contain line breaks).

My fix changes this behavior slightly. The '_oneline' method still reads all 
of the information from the socket. However, instead of returning it all in 
one hit, it stores this locally, and returns one line at a time. This fixes 
the statistics, so that they are correctly parsed. And according to my 
testing, doesn't appear to break any other part of the code.

For the record, we are running the library on a Linux server, running Debian 
Woody. And the version of the library we are using is '1.14'.

I've attached the diff of my changes below. I'd appreciate any feedback on the 
fix, especially if I have misinterpretted the way that '_oneline' is expected 
to function.

Andrew


--- Memcached.pm.old	2005-06-08 08:28:23.000000000 +1000
+++ Memcached.pm	2005-06-08 15:58:43.000000000 +1000
@@ -21,6 +21,7 @@
     readonly select_timeout namespace namespace_len servers active buckets
     pref_ip
     bucketcount _single_sock _stime
+	 read_lines
 };
 
 # flag definitions
@@ -287,6 +288,10 @@
     my $res;
     my ($ret, $offset) = (undef, 0);
 
+    if ($self->{read_lines} && @{$self->{read_lines}}) {
+        return shift @{$self->{read_lines}};
+    }
+
     # state: 0 - writing, 1 - reading, 2 - done
     my $state = defined $line ? 0 : 1;
 
@@ -344,8 +349,9 @@
         _dead_sock($sock); # improperly finished
         return undef;
     }
+    $self->{read_lines} = [ split(/[\r\n]+/, $ret) ];
 
-    return $ret;
+    return shift @{$self->{read_lines}};
 }
 
 


More information about the memcached mailing list