Protocol::Memcached - memcached binary protocol implementation
version 0.004
package Subclass::Of::Protocol::Memcached; use parent qw(Protocol::Memcached); sub write { $_[0]->{socket}->write($_[1]) } package main; my $mc = Subclass::Of::Protocol::Memcached->new; my ($k, $v) = ('hello' => 'world'); $mc->set( $k => $v, on_complete => sub { $mc->get( 'key', on_complete => sub { my $v = shift; print "Had $v\n" }, on_error => sub { die "Failed because of @_\n" } ); } );
Bare minimum protocol support for memcached. This class is transport-agnostic and as such is not a working implementation - you need to subclass and provide your own ->write method.
If you're using this class, you're most likely doing it wrong - head over to the "SEE ALSO" section to rectify this.
Protocol::Memcached::Client is probably the module you want if you are going to subclass this.
Provide the following method:
This will be called with the data to be written, and zero or more named parameters:
on_flush - coderef to execute when the data has left the building, if this is not supported by the transport layer then the subclass should call the coderef before returning
and when you have data, call "on_read".
Bare minimum constructor - subclass may need to inherit from something with a non-trivial constructor, so we put all our init code in "init".
Helper method for weak callbacks.
Retrieves a value from memcached.
Takes a key and zero or more optional named parameters:
on_write - called when we've sent the request to the server
Sets things up.
Currently just does some internal housekeeping, takes no parameters, and returns $self.
This should be called when there is data to be processed. It takes a single parameter: a reference to a buffer containing the incoming data. If a packet is processed successfully then it will be removed from this buffer (via substr or s// ).
substr
s//
Returns true if a packet was found, false if not. It is recommended (but not required) that this method be called repeatedly until it returns false.
Returns the status message corresponding to the given code.
Generic packet construction.
Returns a hashed version of the given key using md5.
Provided for backward compatibility only. See "hash_key".
Generates a Ketama hash map from the given list of servers.
Returns an arrayref of points.
Calculates an integer hash value from the given key.
Given a key value, calculates the closest point on the Ketama map.
Three main reasons:
Transport-agnostic - purposefully does not get involved in the details of sending or receiving data, when it wants to write something it'll call write, and when you have data to process you call on_read
Nonblocking - since this just operates on data and callbacks, rather than getting involved in transporting data, all operations should return quickly (in Perl terms)
Debugging support - strap this over a memcached transport layer and see human-readable versions of the binary packets
If you're looking for good performance, stability, an extensive set of tests, support, and a pony, then you're reading the wrong module:
Cache::Memcached - official implementation
AnyEvent::Memcached - text protocol support for AnyEvent
Cache::Memcached::AnyEvent - provides binary protocol support for AnyEvent
Memcached::Client - another AnyEvent implementation, again with the transport layer too highly coupled for my purposes
Cache::Memcached::GetParserXS - XS implementation for parsing memcached binary data, apparently "possibly twice as fast as the original perl version".
Tom Molesworth <cpan@entitymodel.com>
Copyright Tom Molesworth 2011-2012. Licensed under the same terms as Perl itself.
To install Protocol::Memcached, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Protocol::Memcached
CPAN shell
perl -MCPAN -e shell install Protocol::Memcached
For more information on module installation, please visit the detailed CPAN module installation guide.