Author image Daisuke Maki
and 1 contributors


Test::Memcached - Memcached Runner For Tests


    use Test::Memcached;

    my $memd = Test::Memcached->new(
        options => {
            user => 'memcached-user',


    my $port = $memd->option( 'tcp_port' );

    my $client = Cache::Memcached->new({
        servers => [ "$port" ]



Test::Memcached automatically sets up a memcached instance, and destroys it when the perl script exists.

HACKING Makefile

This is not for the faint of heart, but you can actually hack your CPAN style Makefile to start your memcached server once per "make test". Do something like this in your Makefile.PL:

    # After you generated your Makefile (that's after your "WriteMakeffile()"
    # or "WriteAll()" statements):

    if (-f 'Makefile') {
        open (my $fh, '<', 'Makefile') or die "Could not open Makefile: $!";
        my $makefile = do { local $/; <$fh> };
        close $fh or die $!;

        $makefile =~ s/"-e" "(test_harness\(\$\(TEST_VERBOSE\), )/"-I\$(INST_LIB)" "-I\$(INST_ARCHLIB)" "-It\/lib" "-MTest::Memcached" "-e" "\\\$\$SIG{INT} = sub { CORE::exit }; my \\\$\$memd; if ( ! \\\$\$ENV{TEST_MEMCACHED_SERVERS}) { \\\$\$memd = Test::Memcached->new(); if (\\\$\$memd) { \\\$\$memd->start(); \\\$\$ENV{TEST_MEMCACHED_SERVERS} = '' . \\\$\$memd->option('tcp_port'); } } $1/;

        open (my $fh, '>', 'Makefile') or die "Could not open Makefile: $!";
        print $fh $makefile;
        close $fh or die $!;

Then you can just rely on TEST_MEMCACHED_SERVERS in your .t files. When make test ends, then the memcached instance will automatically stop.

It's ugly, but it works



Creates a new instance. you can set the location of memcached by explicitly setting it, or it will attempt to find it.

You can speficy a set of options to pass to memcached. Below table shows the values that you can use, and the option name that will be mapped to:

    tcp_port                  : 'p'
    udp_port                  : 'U'
    unix_socket               : 's'
    unix_socket_mask          : 'a'
    bind                      : 'l'
    max_core_limit            : 'r'
    user                      : 'u'
    max_memory                : 'm'
    error_on_exhausted_memory : 'M'
    max_connections           : 'c'
    lock_down                 : 'k'
    verbose                   : 'v'
    pidfile                   : 'P'
    chunk_size_factor         : 'f'
    minimum_space             : 'n'
    use_large_memory_pages    : 'L'
    delimiter                 : 'D'
    threads                   : 't'
    requests_per_event        : 'R'
    disable_cas               : 'C'
    backlog_limit             : 'b'
    bind_protocol             : 'B'
    item_size                 : 'I'


Gets the current value of the named option

    my $port = $memd->option('tcp_port');


If no unix_socket, udp_port is set, automatically looks for an empty port to listen on, and starts memcached.


stops memcached. by sending TERM signal


When the object goes out of scope, stop gets called.


Kazuho Oku wrote Test::mysqld, which I shamelessly stole from.

Tokuhiro Matsuno wrote Test::TCP, which I also shamelessly stole from

Daisuke Maki <>


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.