From dcb584f427b78bfe47a2bef83f510de3e8049386 Mon Sep 17 00:00:00 2001 From: Paul Poulain Date: Mon, 14 May 2012 13:24:44 +0200 Subject: [PATCH] Bug 7248/7249 follow-up: more generic cache handling This patch update the cache handling to Koha::Cache. So, whatever the cache mechanism is, Koha caching will work this way: use Koha::Cache; my $cache = Koha::Cache->new(); if ($cache) { $cache->get_from_cache($identifier) } if ($cache) { $cache->set_in_cache( $identifier, $value, $cache_expiry ); } Test plan: * setup a Koha with MEMCACHE activated (MEMCACHED_NAMESPACE is defined in koha-httpd.conf) * setup Koha with DEBUG on (SetEnv DEBUG "1" in koha-httpd.conf) * restart Apache * load a report $KOHA/cgi-bin/koha/svc/report?id= In the Apache logs you will get: * report: We have and will use a cache at /home/paul/koha.dev/koha-community/opac/svc/report line 40. => confirmation we will use memcache * execute_query() => the cache is still empty, we run the SQL load the report again, you'll get: * report: We have and will use a cache at /home/paul/koha.dev/koha-community/opac/svc/report line 40. => confirmation we will use memcache * report: Report retrieved from cache at /home/paul/koha.dev/koha-community/opac/svc/report line 43. => confirmation we are retrieving the result from the cache Remove the MEMCACHE env variable, restart Apache, reload the page, you'll get: * report: No caching system at /home/paul/koha.dev/koha-community/Koha/Cache.pm line 75. => confirmation you have no caching system active --- Koha/Cache.pm | 25 +++++++++++++++++++------ Koha/Cache/Memcached.pm | 6 +++++- opac/svc/report | 25 +++++++++---------------- svc/report | 23 +++++++---------------- t/Cache.t | 5 ++--- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Koha/Cache.pm b/Koha/Cache.pm index 25aad93..ca409b7 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -55,12 +55,25 @@ __PACKAGE__->mk_ro_accessors( qw( cache ) ); sub new { my $class = shift; - my $param = shift; - my $cache_type = $param->{cache_type} || 'memcached'; - my $subclass = __PACKAGE__."::".ucfirst($cache_type); - my $cache = $subclass->_cache_handle($param) - or croak "Cannot create cache handle for '$cache_type'"; - return bless $class->SUPER::new({cache => $cache}), $subclass; +# my $param = shift; + my $cache; + my $subclass; + # try to find which cache type we use + if ($ENV{'MEMCACHED_SERVERS'}) { + # we use MEMCACHE + $subclass = __PACKAGE__."::Memcached"; + $cache = $subclass->_cache_handle( + {'cache_type' => 'memcached', + 'cache_servers' => $ENV{'MEMCACHED_SERVERS'}, + 'namespace' => $ENV{'MEMCACHED_NAMESPACE'}, + } + ) + or croak "Cannot create cache handle for memcache"; + return bless $class->SUPER::new({cache => $cache}), $subclass; + } else { + $ENV{DEBUG} && warn "No caching system"; + return; + } } =head2 EXPORT diff --git a/Koha/Cache/Memcached.pm b/Koha/Cache/Memcached.pm index 87fe3c7..96f890c 100644 --- a/Koha/Cache/Memcached.pm +++ b/Koha/Cache/Memcached.pm @@ -31,6 +31,9 @@ sub _cache_handle { my @servers = split /,/, $params->{'cache_servers'}; return Cache::Memcached->new( servers => \@servers, + debug => 0, + compress_threshold => 10_000, + expire_time => 600, namespace => $params->{'namespace'} || 'KOHA', ); } @@ -39,7 +42,7 @@ sub set_in_cache { my ( $self, $key, $value, $expiry ) = @_; croak "No key" unless $key; $self->cache->set_debug; - + $ENV{DEBUG} && warn "Setting in memcache $key"; if ( defined $expiry ) { return $self->cache->set( $key, $value, $expiry ); } @@ -51,6 +54,7 @@ sub set_in_cache { sub get_from_cache { my ( $self, $key ) = @_; croak "No key" unless $key; + $ENV{DEBUG} && warn "Getting from memcache $key"; return $self->cache->get($key); } diff --git a/opac/svc/report b/opac/svc/report index 4fd8183..2df20bc 100755 --- a/opac/svc/report +++ b/opac/svc/report @@ -28,25 +28,19 @@ use CGI; my $query = CGI->new(); my $report = $query->param('id'); -my $cache; -my $usecache = C4::Context->ismemcached; +# load caching. If we have one, then we will try to retrieve the report from the cache for better performances +use Koha::Cache; +my $cache = Koha::Cache->new(); my ( $sql, $type, $name, $notes, $cache_expiry, $public ) = get_saved_report($report); die "Sorry this report is not public\n" unless $public; -if ($usecache) { - require Koha::Cache; - Koha::Cache->import(); - $cache = Koha::Cache->new( - { - 'cache_type' => 'memcached', - 'cache_servers' => $ENV{'MEMCACHED_SERVERS'} - } - ); - my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha'; - my $page = $cache->get_from_cache("$namespace:opac:report:$report"); +if ($cache) { + $ENV{DEBUG} && warn "We have and will use a cache"; + my $page = $cache->get_from_cache("opac:report:$report"); if ($page) { + $ENV{DEBUG} && warn "Report $report retrieved from cache"; print $query->header; print $page; exit; @@ -61,8 +55,7 @@ my $lines = $sth->fetchall_arrayref; my $json_text = to_json($lines); print $json_text; -if ($usecache) { - my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha'; - $cache->set_in_cache( "$namespace:opac:report:$report", +if ($cache) { + $cache->set_in_cache( "opac:report:$report", $json_text, $cache_expiry ); } diff --git a/svc/report b/svc/report index 476475c..0764e2d 100755 --- a/svc/report +++ b/svc/report @@ -29,8 +29,9 @@ use CGI; my $query = CGI->new(); my $report = $query->param('id'); -my $cache; -my $usecache = C4::Context->ismemcached; +# load caching. If we have one, then we will try to retrieve the report from the cache for better performances +use Koha::Cache; +my $cache = Koha::Cache->new(); my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { @@ -42,17 +43,8 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( } ); -if ($usecache) { - require Koha::Cache; - Koha::Cache->import(); - $cache = Koha::Cache->new( - { - 'cache_type' => 'memcached', - 'cache_servers' => $ENV{'MEMCACHED_SERVERS'} - } - ); - my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha'; - my $page = $cache->get_from_cache("$namespace:intranet:report:$report"); +if ($cache) { + my $page = $cache->get_from_cache("intranet:report:$report"); if ($page) { print $query->header; print $page; @@ -72,8 +64,7 @@ my $lines = $sth->fetchall_arrayref; my $json_text = to_json($lines); print $json_text; -if ($usecache) { - my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha'; - $cache->set_in_cache( "$namespace:intranet:report:$report", +if ($cache) { + $cache->set_in_cache( "intranet:report:$report", $json_text, $cache_expiry ); } diff --git a/t/Cache.t b/t/Cache.t index 286c0c9..424dc75 100644 --- a/t/Cache.t +++ b/t/Cache.t @@ -13,9 +13,8 @@ BEGIN { } SKIP: { - skip "Memcached not enabled", 7 unless C4::Context->ismemcached; - - my $cache = Koha::Cache->new ( { 'cache_servers' => $ENV{'MEMCACHED_SERVERS'} } ); + my $cache = Koha::Cache->new(); + skip "Caching disabled", 7 unless $cache; # test fetching an item that isnt in the cache is( $cache->get_from_cache("not in here"), undef, "fetching item NOT in cache"); -- 1.7.9.5