[Koha-devel] On memcached/memoize

Tomas Cohen Arazi tomascohen at gmail.com
Tue Jan 17 17:26:22 CET 2012


We are using memcached in two different ways accross Koha code:


1) The traditional way, explicitly using memcached inside the
method/function code (just a mock):

sub get_key
{
  my $data;

  if (C4::Context->ismemcached) {
      $data = C4::Context->memcached->get('key');
  }

  $data = slow_routine_to_get_data_from_db &&
C4::Context->memcached->set('key',$data) unless defined $data;

  return $data;
}


2) Using memoize/dynamic programming:

eval {
    if (C4::Context->ismemcached) {
        require Memoize::Memcached;
        import Memoize::Memcached qw(memoize_memcached);

        memoize_memcached( 'get_key,
                            memcached => C4::Context->memcached,
                            expire_time => 600 );    #cache for 10 minutes
    }
};


sub get_key
{
  my $data slow_routine_to_get_data_from_db;

  return $data;
}


The second method is really handy, in the sense that we need to just
need a new call to memoize_memcached to make get_key use memcached.
The problem with this approach (and the reason of this writing) is
data invalidation, that is sometimes needed. Setting expire_time seems
not enough for several cases (for example preferences change).

With the first method, we can use the the method 'delete' to
invalidate data on the setter function.
Using memoize on data we should be able to invalidate means we have to
use flush_cache(get_key => qw( key )) on methods/functions that write
data. But then we need to hook our methods and I can't decide if we
should continue using memoize... as it is less transparent for us who
read other peoples code...

I wrote to the author of Memoize (who didn't actually answer me) to
ask him if we could add a parameter to memoize_memcached: a list of
references to functions that invalidate data for this memoized
function. This way we could just continue using this. Otherwise I'd
recommend just using memcache the classic way.

Hope we have a discussion on this as there are plenty of places we can
benefit from this.

To+





julian.maurice at biblibre.com


More information about the Koha-devel mailing list