From 3b7a479a498dcd56216c24b6f55dfdc703fa4074 Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Tue, 25 Jun 2013 08:08:24 -0700 Subject: [PATCH] bug 10503: fix various issues with use of Memoize::Memcached Memoize::Memcached expects to be passed a hashref of connection parameters, which it in turn uses to initialize a Cache::Memcached object. However, commit 52afe06ddd started passing a Cache::Memcached object directly, not the required hashref. This happened to work by coincidence, but no longer works with recent versions of Memoize::Memcached packaged for Debian. This patch introduces a new C4::Context function, memcached_params(), that returns a hashref of connection parameters suitable for passing to the Cache::Memcached constructor. It also introduces a UNITCHECK block to C4::Context so that memcached_params() can be used during intialization of the module. This patch also corrects SQLHelper so that it no longer tries to use the deprecated (and removed) memcached_servers setting in koha-conf.xml; it now gets the memcached connection parameters from C4::Context->memcached_params(). To test: [1] This patch is most easily tested on Debian Wheezy, as version 0.03-2 of the libmemoize-memcached-perl package includes the fix for Debian bug 614868 that causes Koha's use of Memoize::Memcached to break. If you don't want to go so far as to install/upgrade to Wheezy, you can apply the following Debian patch to your copy of Memoize::Memcached to get the effect: http://patch-tracker.debian.org/patch/series/view/libmemoize-memcached-perl/0.03-2/universal.patch [2] Set up your Koha Apache configuration to point to a memcached server. [3] Restart memcached to start with a clear cache. [4] Do the following steps: - Create a new bib record. Note that it is sufficient to just open the editor; you don't need to save the bib. - Search for a patron record. [5] Use the memcdump command-line to get a list of keys in your memcached cache. You should *not* see keys that contain the strings GetMarcStructure, getTranslatedLanguages, or _get_columns. [6] Apply the patch, then go through step 4 again. [7] Use memcdump again. This time, you *should* see the keys mentioned in step 5. [8] Set the SessionStorage system preference to memcached. After logging in again, verify that your staff interface and OPAC sessions work without requiring that you log in for each and every page load. Signed-off-by: Galen Charlton --- C4/Biblio.pm | 2 +- C4/Context.pm | 40 +++++++++++++++++++++++++++------------- C4/Languages.pm | 6 +++--- C4/SQLHelper.pm | 13 +++---------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 7af8619..6f924bd 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -146,7 +146,7 @@ eval { import Memoize::Memcached qw(memoize_memcached); memoize_memcached( 'GetMarcStructure', - memcached => C4::Context->memcached); + memcached => C4::Context->memcached_params); } }; diff --git a/C4/Context.pm b/C4/Context.pm index 7709f7e..baffc5c 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -18,7 +18,7 @@ package C4::Context; use strict; use warnings; -use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached); +use vars qw($VERSION $AUTOLOAD $context @context_stack $memcached $ismemcached); BEGIN { if ($ENV{'HTTP_USER_AGENT'}) { @@ -79,23 +79,18 @@ BEGIN { } } # else there is no browser to send fatals to! + $VERSION = '3.07.00.049'; +} + +UNITCHECK { # Check if there are memcached servers set - $servers = $ENV{'MEMCACHED_SERVERS'}; - if ($servers) { + if ($ENV{'MEMCACHED_SERVERS'}) { # Load required libraries and create the memcached object require Cache::Memcached; - $memcached = Cache::Memcached->new({ - servers => [ $servers ], - debug => 0, - compress_threshold => 10_000, - expire_time => 600, - namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha' - }); + $memcached = Cache::Memcached->new(memcached_params()); # Verify memcached available (set a variable and test the output) - $ismemcached = $memcached->set('ismemcached','1'); + $ismemcached = $memcached->set('ismemcached','1'); } - - $VERSION = '3.07.00.049'; } use DBI; @@ -293,6 +288,25 @@ sub memcached { } } +=head2 memcached_params + +Return a hashref containing the memcached parameters in a +form suitable for initializing a new C +object. Caller is responsible for verifying that connection +parameters work. + +=cut + +sub memcached_params { + return { + servers => [ $ENV{'MEMCACHED_SERVERS'} ], + namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha', + debug => 0, + expire_time => 600, + compress_threshold => 10_000, + }; +} + # db_scheme2dbi # Translates the full text name of a database into de appropiate dbi name # diff --git a/C4/Languages.pm b/C4/Languages.pm index 3a5ee2a..ca683e4 100644 --- a/C4/Languages.pm +++ b/C4/Languages.pm @@ -30,9 +30,9 @@ eval { require Memoize::Memcached; import Memoize::Memcached qw(memoize_memcached); - memoize_memcached('getTranslatedLanguages', memcached => C4::Context->memcached); - memoize_memcached('getFrameworkLanguages' , memcached => C4::Context->memcached); - memoize_memcached('getAllLanguages', memcached => C4::Context->memcached); + memoize_memcached('getTranslatedLanguages', memcached => C4::Context->memcached_params); + memoize_memcached('getFrameworkLanguages' , memcached => C4::Context->memcached_params); + memoize_memcached('getAllLanguages', memcached => C4::Context->memcached_params); } }; diff --git a/C4/SQLHelper.pm b/C4/SQLHelper.pm index 21f5fa1..c107370 100644 --- a/C4/SQLHelper.pm +++ b/C4/SQLHelper.pm @@ -28,19 +28,12 @@ require Exporter; use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); eval { - my $servers = C4::Context->config('memcached_servers'); - if ($servers) { + if (C4::Context->ismemcached) { require Memoize::Memcached; import Memoize::Memcached qw(memoize_memcached); - my $memcached = { - servers => [$servers], - key_prefix => C4::Context->config('memcached_namespace') || 'koha', - expire_time => 600 - }; # cache for 10 mins - - memoize_memcached( '_get_columns', memcached => $memcached ); - memoize_memcached( 'GetPrimaryKeys', memcached => $memcached ); + memoize_memcached( '_get_columns', memcached => C4::Context->memcached_params() ); + memoize_memcached( 'GetPrimaryKeys', memcached => C4::Context->memcached_params() ); } }; -- 1.7.10.4