The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Dancer::Plugin::MemcachedFast - Cache things using Cache::Memcached::Fast


This plugin allows Dancer to use Cache::Memcached::Fast to get and store content on a number of memcached servers.

Add the following to your configuration file to enable the plugin:

                - ""
                - ""
            default_timeout: 3600
            namespace: "myapp:"

The options that can be used are the same as the parameters to the new constructor of Cache::Memcached::Fast, apart from default_timeout.

The default_timeout parameter defaults to 3600 seconds. It allows you to specify a time, in seconds, after which you would like keys to expire from the cache.

It accepts the same options that Cache::Memcached::Fast accepts.

In your app:

    package MyApp;
    use Dancer;
    use Dancer::Plugin::MemcachedFast;

    # Simply fetch the index from memcached if found,
    # or set the cache to the parsed index template
    get '/' => sub {
        memcached_get_or_set('index', sub {
            template 'index';

    # The login page can be shown from the cache,
    # whereas if a user is logged in the cache is
    # user-specific
    get '/admin' => sub {
        unless (session->{username}) {
            return memcached_get_or_set('admin_login', sub {
                template 'admin_login';
        memcached_get_or_set('admin-home-' . session->{username}, sub {
            template 'admin_home';

    # Use memcache to cache bits of information only,
    # for a specific amount of seconds
    get '/articles' => sub {
        my $articles = memcached_get_or_set('articles-list', sub {
            # use DBI to fetch last 10 articles
            my @last_10_articles = ...;
            return \@last_10_articles
        }, 300);
        template 'articles_list' => { articles => $articles };

    # forcibly delete a number of keys from the cache
    get '/admin/PANIC' => {
        return redirect '/admin'
            unless session->{username};
            qw/articles_list admin_login/,
            'admin-home-' . session->{username},
        redirect '/admin';

    # set a cache to a specified value unconditionally
    get '/admin/meaningoflife' => sub {
        return redirect '/admin'
            unless session->{username};
        memcached_set('meaning', 42);
        redirect '/admin';

    # get a value from the cache, who cares if it isn't there?!
    get '/admin/maybe' => sub {
        my $meaning = memcached_get('meaningoflife');
        template 'meaning' => { meaning => $meaning };


This plugin allows Dancer to use Cache::Memcached::Fast to get and store content on a number of memcached servers.


You can set the default timeout via the configuration, as explained in the SYNOPSIS section. The default is 3600 seconds.

If you want to cache a value for as long as there is memory available to Memcache, you can achieve that by passing 0 as the expiration parameter (not undef, as that just uses the default_timeout).



Will check whether the specified key is found in the cache and return it where available. Otherwise it will set it to either the value given, or will call the coderef given ans set it to the value returned by the coderef. It will be cached either for the default number of seconds, or the number of seconds given.

    # returns the value on memcached for 'keyname' if found, or
    # sets the 'keyname' to the value 42 and its expiration to 300 seconds
    memcached_get_or_set('key name', sub { 42 }, 300);
    memcached_get_or_set('key name', 42, 300);
    memcached_get_or_set('key name', [42], 300);


Attempts to get a value from memcached, and returns it. Uses get_multi if given more than one value to get. Expands arrayrefs where needed.

    my $maybe  = memcached_get('key');
    my $values = memcached_get(qw/keya keyb keyc/);
    $values = memcached_get([qw/keya keyb keyc/], 'keyd', ['keye','keyf']);


Retries the value and its CAS for the key given. Uses gets_multi if given more than one value to get. Expands arrayrefs where needed.


Sets unconditionally a key to a given value. A coderef may be used also, and its return value will be used. Alternatively, provide an arrayref for it to use set_multi on it. Takes an optional third value indicating the number of seconds the key will be cached for, or the default number of seconds. You can use an expiration value of 0 to mean that the value should never expire from the cache.

    memcached_set('meaningoflife', 42);
    memcached_set('meaningoflife', sub { 21 * 2 });
    # "meaningoflife" will be cached for the default number of seconds:


Similar to memcached_set, but only operates if the key is not already stored in memcached.


Similar to memcached_set, but only operates if the key is already stored in memcached.


Forcibly expunges one or more keys from the cache.

    memcached_delete( [ 'abc', 'def' ], [ 'xyz' ] );
    memcached_delete( [ 'abc', 'def' ], 'xyz' );


Appends the given string to the already stored scalar value. See Cache::Memcached::Fast's method append and append_multi. append is used if the two given parameters are scalars, append_multi will be used if they are arrayrefs.

    memcached_set( 'meaningoflife', '4' );
    memcached_append( 'meaningoflife', '2' );
    my $fourtytwo = memcached_get('meaningoflife');


Same as append_multi, but works at the beginning of the string.


Increments the key given by 1 or the optional value given. Can operate on arrayrefs, using incr_multi.


Same as memcached_incr but decrements.


Invalidates all caches the client knows about. See Cache::Memcached::Fast's flush_all method.


Pushes all the pending request to the server(s), see Cache::Memcached::Fast's nowait_push method.


Returns the versions of the servers the plugin is connected to.


Just in case your program forks and needs to disconnect them


Needs a boolean, to enable or disable the compression. In order to work, the configuration options compress_threshold and compress_methods need to be pgiven. See Cache::Memcached::Fast for the options.

    memcached_compress(42); # true, starts compressing
    memcached_compress(1);  # true, starts compressing
    memcached_compress(''); # false, stops compressing
    memcached_compress(0);  # false, stops compressing


Either returns the current namespace prefix, or sets it to the given value and returns the old value.

    my $current_prefix = memcached_namespace;
    my $old_prexix     = memcached_namespace('test:');
    # $current_prefix eq $old_prefix
    $current_prefix = memcached_namespace;
    # $current_prefix eq 'test:'


Nikolay Mishin <>


This software is copyright (c) 2011 by Marco FONTANI. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.