Memoize::Memcached(3) use a memcached cache to memoize functions

SYNOPSIS


use Memoize::Memcached
memcached => {
servers => [ '127.0.0.1:11211' ],
};
memoize_memcached('foo');
# Function 'foo' is now memoized using the memcached server
# running on 127.0.0.1:11211 as the cache.

WARNING

The way "flush_cache" works with memcached can be dangerous. Please read the documentation below on "flush_cache".

EXPORT

This module exports "memoize_memcached", "flush_cache", and "unmemoize". The "unmemoize" function is just the one from Memoize, and is made available for convenience.

FUNCTIONS

memoize_memcached

This is the memcached equivalent of "memoize". It works very similarly, except for some difference in options.

If the "LIST_CACHE" or "SCALAR_CACHE" options are passed in, "memoize_memcached" will complain and then pass the request along to "memoize". The result will be a memoized function, but using whatever cache you specified and NOT using memcached at all.

This function also accepts a "memcached" option, which expects a hashref. This is de-referenced and passed directly into an internal function which sets up the memcached configuration for that function. This contents of this hashref are mostly options passed to "Cache::Memcached", with a few exceptions.

The actual key used to look up memoize data in memcached is formed from the function name, the normalized arguments, and some additional prefixes which can be set via the "memcached" option. These prefixes are "key_prefix", "list_key_prefix", and "scalar_key_prefix".

The "key_prefix" defaults to ``memoize-'' if it's not passed in, or an undefined value is passed in.

The "list_key_prefix" and "scalar_key_prefix" options default to ``list-'' and ``scalar-'' respectively, by the same criteria.

So, the default way the key is generated is:

  "memoize-<function>-list-<normalized args>"

or

  "memoize-<function>-scalar-<normalized args>"

The function and normalized args portion of this key are set internally, but the ``memoize-'' prefix and the context portion can be configured with memcached options as follows:

  "<key_prefix>-function-<list_key_prefix|scalar_key_prefix>-args"

Examples:

  memoize_memcached('foo');
  # keys generated will look like this:
  #  list context:   memoize-foo-list-<argument signature>
  #  scalar context: memoize-foo-scalar-<argument signature>
  memoize_memcached('foo',
    memcached => {
      servers => [ ... ],
      key_prefix        => '_M-',
      list_key_prefix   => 'L-',
      scalar_key_prefix => 'S-',
    },
    ;
  # keys generated will look like this:
  #  list context:   _M-foo-L-<argument signature>
  #  scalar context: _M-foo-S-<argument signature>

flush_cache

The behavior documented in "Memoize" is sort of implemented. A call to "flush_cache('memoized_function')" will indeed clear the cache of all cached return values for that function, BUT it will also clear the entire memcached cache, including all other memoized functions using the same memcached cache, and even data unrelated to "Memoize::Memcached" in the same cache. It will flush the entire cache.

There are 2 new ways to call this function:

    flush_cache();

and

    flush_cache(memoized_function => qw( an argument signature ));

The call without arguments will flush the entire memcached cache, just like the 1 argument version. This includes unrelated data. Be careful.

The call with 2 or more arguments will flush only the cached return values (array and scalar contexts) for a call to the function named by the first argument with an argument signature matching the second argument to the end. Unlike the other 2 ways to call this function, when called this way only the specified part of the cache is flushed.

I would recommended that only the 2 or more argument version of "flush_cache" be called unless you are very sure of what you are doing.

GOTCHAS

The biggest gotcha is that you probably never want to call "flush_cache('memoized_function')". Because of the way "CLEAR" is implemented against memcached, this call will flush the entire memcached cache. Everything. Even stuff having nothing to do with "Memoize::Memcached". You are warned.

TO-DO

A more intuitive interface for handling different memcached server configurations would probably be useful.

AUTHOR

David Trischuk, "<trischuk at gmail.com>"

BUGS

Please report any bugs or feature requests to "bug-memoize-memcached at rt.cpan.org", or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Memoize-Memcached <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Memoize-Memcached>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGMENTS

The tied hash portion of this module is heavily based on "Cache::Memcached::Tie" by Andrew Kostenko.

COPYRIGHT & LICENSE

Copyright 2008 David Trischuk, all rights reserved.

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