View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Cindy Wang (CindyLinz) > Memcached-Server-0.04 > Memcached::Server



Annotate this POD


Open  0
Stalled  1
View/Report Bugs
Module Version: 0.04   Source  


Memcached::Server - A pure perl Memcached server helper, that help you create a server speaking Memcached protocol


Version 0.04


    # running as a stand alone server
    use Memcached::Server;
    my $server = Memcached::Server->new(
        no_extra => 0 / 1, # if set to true, then the server will skip cas, expire, flag;
                           #  thus, cas always success, never expire, flag remains 0 forever.
                           # with this option on, one can get a entry that hasn't been set,
                           #  as long as your 'get' and '_find' say yes.
        open => [[0, 8888], ['', 8889], ['', 8889], [$host, $port], ...],
        cmd => { # customizable handlers
            _find => sub {
                my($cb, $key, $client) = @_;
                $cb->(0); # not found
                ... or ...
                $cb->(1); # found
            get => sub {
                my($cb, $key, $client) = @_;
                $cb->(0); # not found
                ... or ...
                $cb->(1, $data); # found
            set => sub {
                my($cb, $key, $flag, $expire, $value, $client) = @_;
                $cb->(1); # success
                ... or ...
                $cb->(-1, $error_message); # error occured, but keep the connection to accept next commands.
                ... or ...
                $cb->(-2, $error_message); # error occured, and close the connection immediately.
            delete => sub {
                my($cb, $key, $client) = @_;
                $cb->(0); # not found
                ... or ...
                $cb->(1); # success
            flush_all => sub {
                my($cb, $client) = @_;
            _begin => sub { # called when a client is accepted or assigned by 'serve' method (optional)
                my($cb, $client) = @_;
            _end => sub { # called when a client disconnects (optional)
                my($cb, $client) = @_;
            # NOTE: the $client, a AnyEvent::Handle object, is presented for keeping per connection information by using it as a hash key.
            #  it's not recommended to read or write to this object directly, that might break the protocol consistency.
    $server->open($host, $port); # open one more listening address
    $server->close($host, $port); # close a listening address
    $server->close_all; # close all the listening addresses
    $server->serve($file_handle); # assign an accepted client socket to the server manually


This module help us to create a pure perl Memcached server. It take care some protocol stuff, so that we can only focus on primary functions.

Take a look on the source of Memcached::Server::Default, a compelete example that works as a standard Memcached server, except it's pure perl implemented.


$server = Memcached::Server->new( cmd => ..., open => ... );

Create a Memcached::Server with parameters 'cmd' (required) and 'open' (optional).

The parameter 'cmd' is provided to control the behaviors, that should be prepared and assigned at the initial time.

The parameter 'open' is provided to assign a list of listening hosts/ports. Each of the list is passed to AnyEvent::Socket::tcp_server directly, so you can use IPv4, IPv6, and also unix sockets.

If you don't provide 'open' here, you can provide it later by member method 'open'.


Assign an accepted client socket to the server. Instead of accepting and serving clients on centain listening port automatically, you can also serve clients manually by this method.

$server->open( host, port )

Functions like the 'open' parameter of the 'new' method. The 'new' method will put each element of the 'open' parameter to this method indeed.

$server->close( host, port )

Close and stop listening to certain host-port.


Close all the listening host-port.

$server->_set, $server->_find, $server->_get, $server->_delete, $server->_flush_all, $server->_begin, $server->_end

These methods are the main function methods used by the server. They should be used or overrided when you implementing your own server and you want to do something SPECIAL. Please read the source for better understanding.


Memcached::Server::Default, AnyEvent, AnyEvent::Socket


Cindy Wang (CindyLinz)


Please report any bugs or feature requests to bug-memcached-server at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc Memcached::Server

You can also look for information at:


Copyright 2010 Cindy Wang (CindyLinz).

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See for more information.

syntax highlighting: