Bug 7248 follow-up (alternative)
authorPaul Poulain <paul.poulain@biblibre.com>
Mon, 14 May 2012 15:45:33 +0000 (17:45 +0200)
committerPaul Poulain <paul.poulain@biblibre.com>
Tue, 15 May 2012 16:05:58 +0000 (18:05 +0200)
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 <jcamins@cpbibliography.com>
Koha/Cache.pm
Koha/Cache/Memcached.pm
opac/svc/report
svc/report

index 25aad93..67064d4 100644 (file)
@@ -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, E<lt>chris@bigballofwax.co.nzE<gt>
+Paul Poulain, E<lt>paul.poulain@biblibre.comE<gt>
 
 =cut
 
index 87fe3c7..226fa7c 100644 (file)
@@ -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);
 }
 
index 4fd8183..8781369 100755 (executable)
@@ -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 );
 }
index 476475c..18e1986 100755 (executable)
@@ -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 );
 }