[Koha-patches] [memcached 1/2] Use Memcached When Available

John Beppu john.beppu at liblime.com
Wed Feb 4 10:35:31 CET 2009


- Currently using memcached for:
  * C4::Context->preference($var)
  * C4::Language::getAllLanguages()

- Added memcached config options to etc/koha-conf.xml.
  New options include:
  * <memcachedserver> - comma-separated list of hostname:port pairs
  * <memcached${x}namespace> - prefix to use for memcached namespace, $x;
    (This makes it easier to let many koha installations on a single server
    share memcached servers.)

- Note that neither memcached nor Cache::Memcached::Fast is a
  hard requirement.  If either of these are not available, the code
  will fall back to using the database to get the data.
---
 C4/Context.pm     |   74 +++++++++++++++++++++++++++++++++++++++++++++++-----
 C4/Languages.pm   |    8 +++++
 etc/koha-conf.xml |    5 +++
 3 files changed, 79 insertions(+), 8 deletions(-)

diff --git a/C4/Context.pm b/C4/Context.pm
index 2f2bc96..1f321e0 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);
+use vars qw($VERSION $AUTOLOAD $context @context_stack %sysprefs %memd);
 
 BEGIN {
 	if ($ENV{'HTTP_USER_AGENT'})	{
@@ -87,6 +87,7 @@ use XML::Simple;
 use C4::Boolean;
 use C4::Debug;
 use POSIX ();
+eval "use Cache::Memcached::Fast";
 
 =head1 NAME
 
@@ -173,7 +174,7 @@ environment variable to the pathname of a configuration file to use.
 
 use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml";
                 # Default config file, if none is specified
-                
+
 my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
                 # path to config file set by installer
                 # __KOHA_CONF_DIR__ is set by rewrite-confg.PL
@@ -182,7 +183,7 @@ my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml';
                 # __KOHA_CONF_DIR__ is *not* rewritten; instead
                 # developers should set the KOHA_CONF environment variable 
 
-$context = undef;        # Initially, no context is set
+$context = undef;           # Initially, no context is set
 @context_stack = ();        # Initially, no saved contexts
 
 
@@ -444,6 +445,47 @@ sub ModZebrations {
 	return _common_config($_[1],'serverinfo');
 }
 
+=item memcached
+
+  $memd = C4::Context->memcached('preference');
+
+This method returns an instance of Cache::Memcached::Fast for the given
+memcached namespace.  The memcached servers and namespaces should be defined in
+F<koha-conf.xml> like this:
+
+  <config>
+    <memcachedserver>192.168.0.10:11211,192.168.0.11:11211</memcachedserver>
+    <memcachedpreferencenamespace>library-preference:</memcachedpreferencenamespace>
+    <memcachedmiscnamespace>library-misc:</memcachedmiscnamespace>
+  </config>
+
+B<memcachedserver> should be a comma separated list of memcached servers to
+use.
+
+Each namespace you intend to use should have a tag named like
+B<memcached${name}namespace>.  In the example above, the C<preference> and
+C<misc> namespaces have been defined.
+
+B<IMPORTANT>:  Each koha installation on a server should define its own set of
+namespaces in order to prevent the data for one koha installation from
+interfering with another koha installation.
+
+=cut
+
+sub memcached {
+    my ($class, $name) = @_;
+    return undef unless (%Cache::Memcached::Fast::);
+    return $memd{$name} if exists $memd{$name};
+    my $namespace_key = "memcached${name}namespace";
+    my $md = Cache::Memcached::Fast->new({
+        servers   => [ split("," => C4::Context->config('memcachedserver')) ],
+        namespace => C4::Context->config($namespace_key),
+        utf8      => 1,
+    });
+    $memd{$name} = $md;
+    return $md;
+}
+
 =item preference
 
   $sys_preference = C4::Context->preference('some_variable');
@@ -460,19 +502,25 @@ with this method.
 
 =cut
 
-# FIXME: running this under mod_perl will require a means of
-# flushing the caching mechanism.
-
-my %sysprefs;
-
 sub preference {
     my $self = shift;
     my $var  = shift;                          # The system preference to return
 
     if (exists $sysprefs{$var}) {
+        #warn "memory $var";
         return $sysprefs{$var};
     }
 
+    my $memd;
+    if ($memd = C4::Context->memcached('preference')) {
+        my $value = $memd->get($var);
+        if (defined $value) {
+            #warn "memcached $var";
+            return $sysprefs{$var} = $value;
+        }
+    }
+
+    #warn "db $var";
     my $dbh  = C4::Context->dbh or return 0;
 
     # Look up systempreferences.variable==$var
@@ -483,6 +531,10 @@ sub preference {
         LIMIT    1
 END_SQL
     $sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var );
+    if (defined $memd) {
+        #warn $var if not defined $sysprefs{$var};
+        $memd->set($var => $sysprefs{$var});
+    }
     return $sysprefs{$var};
 }
 
@@ -493,6 +545,10 @@ sub boolean_preference ($) {
     return defined($it)? C4::Boolean::true_p($it): undef;
 }
 
+sub set_preference {
+    my ($class, $var, $value) = @_;
+}
+
 =item clear_syspref_cache
 
   C4::Context->clear_syspref_cache();
@@ -505,6 +561,8 @@ sub boolean_preference ($) {
 
 sub clear_syspref_cache {
     %sysprefs = ();
+    my $memd = C4::Context->memcached('preference');
+    $memd->flush_all if $memd;
 }
 
 # AUTOLOAD
diff --git a/C4/Languages.pm b/C4/Languages.pm
index 7c3503c..c2a96c9 100644
--- a/C4/Languages.pm
+++ b/C4/Languages.pm
@@ -172,6 +172,11 @@ Returns a reference to an array of hashes:
 
 sub getAllLanguages {
     my @languages_loop;
+    my $memd = C4::Context->memcached('misc');
+    if ($memd) {
+        my @all_languages = $memd->get('all_languages');
+        return @all_languages if @all_languages;
+    }
     my $dbh=C4::Context->dbh;
     my $current_language = shift || 'en';
     my $sth = $dbh->prepare('SELECT * FROM language_subtag_registry WHERE type=\'language\'');
@@ -205,6 +210,9 @@ sub getAllLanguages {
         }
         push @languages_loop, $language_subtag_registry;
     }
+    if ($memd) {
+        $memd->set('all_languages', \@languages_loop);
+    }
     return \@languages_loop;
 }
 
diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml
index 3c4da9c..32422d1 100644
--- a/etc/koha-conf.xml
+++ b/etc/koha-conf.xml
@@ -190,5 +190,10 @@ __PAZPAR2_TOGGLE_XML_POST__
  <logdir>__LOG_DIR__</logdir>
  <pazpar2url>http://__PAZPAR2_HOST__:__PAZPAR2_PORT__/search.pz2</pazpar2url>
  <install_log>__MISC_DIR__/koha-install-log</install_log>
+ <memcachedserver>localhost:11211</memcachedserver>
+ <!-- If you run more than one koha installation on a server -->
+ <!-- you should change the memcached namespaces for each installation. -->
+ <memcachedpreferencenamespace>preference:</memcachedpreferencenamespace>
+ <memcachedmiscnamespace>misc:</memcachedmiscnamespace>
 </config>
 </yazgfs>
-- 
1.6.0.3



More information about the Koha-patches mailing list