@@ -, +, @@ $value, $cache_expiry ); * 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= => confirmation we will use memcache => the cache is still empty, we run the SQL => confirmation we will use memcache => confirmation we are retrieving the result from the cache --- Koha/Cache.pm | 25 +++++++++++++++++++------ opac/svc/report | 25 +++++++++---------------- svc/report | 23 +++++++---------------- 3 files changed, 35 insertions(+), 38 deletions(-) --- a/Koha/Cache.pm +++ a/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 --- a/opac/svc/report +++ a/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 ); } --- a/svc/report +++ a/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 ); } --