Marco Fontani > Dancer-Plugin-MemcachedFast > Dancer::Plugin::MemcachedFast

Download:
Dancer-Plugin-MemcachedFast-0.130500.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.130500   Source  

NAME ^

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

SYNOPSIS ^

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:

    plugins:
        MemcachedFast:
            servers:
                - "127.0.0.1:11211"
                - "127.0.0.1:22122"
            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};
        memcached_delete(
            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 };
    };

TIMEOUTS ^

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).

KEYWORDS ^

memcached_get_or_set

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);

memcached_get

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']);

memcached_gets

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.

memcached_set

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:
    memcached_set(['meaningoflife',42],['test',1234,300]);
    memcached_set(['test',sub{42},123],['abc','def']);

memcached_add

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

memcached_replace

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

memcached_delete

Forcibly expunges one or more keys from the cache.

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

memcached_append

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');
    memcached_append(['meaningoflife',42],['answer','whoknows']);

memcached_prepend

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

memcached_incr

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

memcached_decr

Same as memcached_incr but decrements.

memcached_flush_all

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

memcached_nowait_push

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

memcached_server_versions

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

memcached_disconnect_all

Just in case your program forks and needs to disconnect them

memcached_compress

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

memcached_namespace

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:'
syntax highlighting: