From c4a6fe2eea567a8e467068f1dd01a97400fdb467 Mon Sep 17 00:00:00 2001 From: Paul Poulain Date: Mon, 14 May 2012 17:45:33 +0200 Subject: [PATCH] Bug 7248 follow-up (alternative) Content-Type: text/plain; charset="UTF-8" This patch introduces some new features for caching system: * the type of caching is retrieved from ENV variable in httpd configuration if not forced when the ->new() is called. * if $ENV{DEBUG} is ON, you'll have feedback when something is set or read from cache * the Koha::Cache->is_cache_active is now available and will return 1 if there is a caching system available. It's a replacement for C4::Context->ismemcached * fixes the mistake for debug/compress_threshold and expire_time parameters The 2 report web service modules have been updated to use this new API Signed-off-by: Jared Camins-Esakov --- Koha/Cache.pm | 7 ++++++- Koha/Cache/Memcached.pm | 10 ++++++++-- opac/svc/report | 20 ++++++-------------- svc/report | 24 ++++++++---------------- 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/Koha/Cache.pm b/Koha/Cache.pm index 25aad93..67064d4 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -56,13 +56,17 @@ __PACKAGE__->mk_ro_accessors( qw( cache ) ); sub new { my $class = shift; my $param = shift; - my $cache_type = $param->{cache_type} || 'memcached'; + my $cache_type = $ENV{CACHING_SYSTEM} || $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; } +sub is_cache_active { + return $ENV{CACHING_SYSTEM} ? '1' : '' ; +} + =head2 EXPORT None by default. @@ -74,6 +78,7 @@ Koha::Cache::Memcached =head1 AUTHOR Chris Cormack, Echris@bigballofwax.co.nzE +Paul Poulain, Epaul.poulain@biblibre.comE =cut diff --git a/Koha/Cache/Memcached.pm b/Koha/Cache/Memcached.pm index 87fe3c7..226fa7c 100644 --- a/Koha/Cache/Memcached.pm +++ b/Koha/Cache/Memcached.pm @@ -28,10 +28,14 @@ use base qw(Koha::Cache); sub _cache_handle { my $class = shift; my $params = shift; - my @servers = split /,/, $params->{'cache_servers'}; + my @servers = split /,/, $params->{'cache_servers'}?$params->{'cache_servers'}:$ENV{MEMCACHED_SERVERS}; + $ENV{DEBUG} && warn "Caching server settings: ".join(', ',@servers)." with ".($ENV{MEMCACHED_NAMESPACE} || $params->{'namespace'} || 'koha'); return Cache::Memcached->new( servers => \@servers, - namespace => $params->{'namespace'} || 'KOHA', + debug => 0, + compress_threshold => 10_000, + expire_time => 600, + namespace => $ENV{MEMCACHED_NAMESPACE} || $params->{'namespace'} || 'koha', ); } @@ -39,6 +43,7 @@ sub set_in_cache { my ( $self, $key, $value, $expiry ) = @_; croak "No key" unless $key; $self->cache->set_debug; + $ENV{DEBUG} && warn "set_in_cache for Memcache $key"; if ( defined $expiry ) { return $self->cache->set( $key, $value, $expiry ); @@ -51,6 +56,7 @@ sub set_in_cache { sub get_from_cache { my ( $self, $key ) = @_; croak "No key" unless $key; + $ENV{DEBUG} && warn "get_from_cache for Memcache $key"; return $self->cache->get($key); } diff --git a/opac/svc/report b/opac/svc/report index 4fd8183..8781369 100755 --- a/opac/svc/report +++ b/opac/svc/report @@ -25,27 +25,21 @@ use C4::Reports::Guided; use JSON; use CGI; +use Koha::Cache; + my $query = CGI->new(); my $report = $query->param('id'); my $cache; -my $usecache = C4::Context->ismemcached; 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(); +if (Koha::Cache->is_cache_active) { $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"); + my $page = $cache->get_from_cache("opac:report:$report"); if ($page) { print $query->header; print $page; @@ -61,8 +55,6 @@ 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", - $json_text, $cache_expiry ); +if (Koha::Cache->is_cache_active) { + $cache->set_in_cache( "opac:report:$report", $json_text, $cache_expiry ); } diff --git a/svc/report b/svc/report index 476475c..18e1986 100755 --- a/svc/report +++ b/svc/report @@ -26,11 +26,13 @@ use C4::Reports::Guided; use JSON; use CGI; +use Koha::Cache; + + my $query = CGI->new(); my $report = $query->param('id'); my $cache; -my $usecache = C4::Context->ismemcached; my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { @@ -42,17 +44,9 @@ 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 (Koha::Cache->is_cache_active) { + $cache = Koha::Cache->new(); + my $page = $cache->get_from_cache("intranet:report:$report"); if ($page) { print $query->header; print $page; @@ -72,8 +66,6 @@ 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", - $json_text, $cache_expiry ); +if (Koha::Cache->is_cache_active) { + $cache->set_in_cache( "intranet:report:$report", $json_text, $cache_expiry ); } -- 1.7.2.5