Permalink
Browse files

Bug 7248 follow-up (alternative)

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>
  • Loading branch information...
1 parent 03fee7a commit 66dbe7dc662f11ae67d1db721a451e9ac77968a9 @PaulPoulain PaulPoulain committed May 14, 2012
Showing with 28 additions and 33 deletions.
  1. +6 −1 Koha/Cache.pm
  2. +8 −2 Koha/Cache/Memcached.pm
  3. +6 −14 opac/svc/report
  4. +8 −16 svc/report
View
7 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, E<lt>chris@bigballofwax.co.nzE<gt>
+Paul Poulain, E<lt>paul.poulain@biblibre.comE<gt>
=cut
View
10 Koha/Cache/Memcached.pm
@@ -28,17 +28,22 @@ 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',
);
}
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);
}
View
20 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 );
}
View
24 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 );
}

0 comments on commit 66dbe7d

Please sign in to comment.