View | Details | Raw Unified | Return to bug 7248
Collapse All | Expand All

(-)a/Koha/Cache.pm (-1 / +6 lines)
Lines 56-68 __PACKAGE__->mk_ro_accessors( qw( cache ) ); Link Here
56
sub new {
56
sub new {
57
    my $class = shift;
57
    my $class = shift;
58
    my $param = shift;
58
    my $param = shift;
59
    my $cache_type = $param->{cache_type} || 'memcached';
59
    my $cache_type = $ENV{CACHING_SYSTEM} || $param->{cache_type} || 'memcached';
60
    my $subclass = __PACKAGE__."::".ucfirst($cache_type);
60
    my $subclass = __PACKAGE__."::".ucfirst($cache_type);
61
    my $cache    = $subclass->_cache_handle($param)
61
    my $cache    = $subclass->_cache_handle($param)
62
      or croak "Cannot create cache handle for '$cache_type'";
62
      or croak "Cannot create cache handle for '$cache_type'";
63
    return bless $class->SUPER::new({cache => $cache}), $subclass;
63
    return bless $class->SUPER::new({cache => $cache}), $subclass;
64
}
64
}
65
65
66
sub is_cache_active {
67
    return $ENV{CACHING_SYSTEM} ? '1' : '' ;
68
}
69
66
=head2 EXPORT
70
=head2 EXPORT
67
71
68
None by default.
72
None by default.
Lines 74-79 Koha::Cache::Memcached Link Here
74
=head1 AUTHOR
78
=head1 AUTHOR
75
79
76
Chris Cormack, E<lt>chris@bigballofwax.co.nzE<gt>
80
Chris Cormack, E<lt>chris@bigballofwax.co.nzE<gt>
81
Paul Poulain, E<lt>paul.poulain@biblibre.comE<gt>
77
82
78
=cut
83
=cut
79
84
(-)a/Koha/Cache/Memcached.pm (-3 / +9 lines)
Lines 28-37 use base qw(Koha::Cache); Link Here
28
sub _cache_handle {
28
sub _cache_handle {
29
    my $class  = shift;
29
    my $class  = shift;
30
    my $params = shift;
30
    my $params = shift;
31
    my @servers = split /,/, $params->{'cache_servers'};
31
    my @servers = split /,/, $params->{'cache_servers'}?$params->{'cache_servers'}:$ENV{MEMCACHED_SERVERS};
32
    $ENV{DEBUG} && warn "Caching server settings: ".join(', ',@servers)." with ".($ENV{MEMCACHED_NAMESPACE} || $params->{'namespace'} || 'koha');
32
    return Cache::Memcached->new(
33
    return Cache::Memcached->new(
33
        servers   => \@servers,
34
        servers   => \@servers,
34
        namespace => $params->{'namespace'} || 'KOHA',
35
        debug   => 0,
36
        compress_threshold => 10_000,
37
        expire_time => 600,
38
        namespace => $ENV{MEMCACHED_NAMESPACE} || $params->{'namespace'} || 'koha',
35
    );
39
    );
36
}
40
}
37
41
Lines 39-45 sub set_in_cache { Link Here
39
    my ( $self, $key, $value, $expiry ) = @_;
43
    my ( $self, $key, $value, $expiry ) = @_;
40
    croak "No key" unless $key;
44
    croak "No key" unless $key;
41
    $self->cache->set_debug;
45
    $self->cache->set_debug;
42
46
    $ENV{DEBUG} && warn "set_in_cache for Memcache $key";
47
     
43
    if ( defined $expiry ) {
48
    if ( defined $expiry ) {
44
        return $self->cache->set( $key, $value, $expiry );
49
        return $self->cache->set( $key, $value, $expiry );
45
    }
50
    }
Lines 51-56 sub set_in_cache { Link Here
51
sub get_from_cache {
56
sub get_from_cache {
52
    my ( $self, $key ) = @_;
57
    my ( $self, $key ) = @_;
53
    croak "No key" unless $key;
58
    croak "No key" unless $key;
59
    $ENV{DEBUG} && warn "get_from_cache for Memcache $key";
54
    return $self->cache->get($key);
60
    return $self->cache->get($key);
55
}
61
}
56
62
(-)a/opac/svc/report (-14 / +6 lines)
Lines 25-51 use C4::Reports::Guided; Link Here
25
use JSON;
25
use JSON;
26
use CGI;
26
use CGI;
27
27
28
use Koha::Cache;
29
28
my $query  = CGI->new();
30
my $query  = CGI->new();
29
my $report = $query->param('id');
31
my $report = $query->param('id');
30
32
31
my $cache;
33
my $cache;
32
my $usecache = C4::Context->ismemcached;
33
34
34
my ( $sql, $type, $name, $notes, $cache_expiry, $public ) =
35
my ( $sql, $type, $name, $notes, $cache_expiry, $public ) =
35
  get_saved_report($report);
36
  get_saved_report($report);
36
die "Sorry this report is not public\n" unless $public;
37
die "Sorry this report is not public\n" unless $public;
37
38
38
if ($usecache) {
39
if (Koha::Cache->is_cache_active) {
39
    require Koha::Cache;
40
    Koha::Cache->import();
41
    $cache = Koha::Cache->new(
40
    $cache = Koha::Cache->new(
42
        {
43
            'cache_type'    => 'memcached',
44
            'cache_servers' => $ENV{'MEMCACHED_SERVERS'}
45
        }
46
    );
41
    );
47
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
42
    my $page = $cache->get_from_cache("opac:report:$report");
48
    my $page = $cache->get_from_cache("$namespace:opac:report:$report");
49
    if ($page) {
43
    if ($page) {
50
        print $query->header;
44
        print $query->header;
51
        print $page;
45
        print $page;
Lines 61-68 my $lines = $sth->fetchall_arrayref; Link Here
61
my $json_text = to_json($lines);
55
my $json_text = to_json($lines);
62
print $json_text;
56
print $json_text;
63
57
64
if ($usecache) {
58
if (Koha::Cache->is_cache_active) {
65
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
59
    $cache->set_in_cache( "opac:report:$report", $json_text, $cache_expiry );
66
    $cache->set_in_cache( "$namespace:opac:report:$report",
67
        $json_text, $cache_expiry );
68
}
60
}
(-)a/svc/report (-17 / +8 lines)
Lines 26-36 use C4::Reports::Guided; Link Here
26
use JSON;
26
use JSON;
27
use CGI;
27
use CGI;
28
28
29
use Koha::Cache;
30
31
29
my $query  = CGI->new();
32
my $query  = CGI->new();
30
my $report = $query->param('id');
33
my $report = $query->param('id');
31
34
32
my $cache;
35
my $cache;
33
my $usecache = C4::Context->ismemcached;
34
36
35
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36
    {
38
    {
Lines 42-58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
42
    }
44
    }
43
);
45
);
44
46
45
if ($usecache) {
47
if (Koha::Cache->is_cache_active) {
46
    require Koha::Cache;
48
    $cache = Koha::Cache->new();
47
    Koha::Cache->import();
49
    my $page = $cache->get_from_cache("intranet:report:$report");
48
    $cache = Koha::Cache->new(
49
        {
50
            'cache_type'    => 'memcached',
51
            'cache_servers' => $ENV{'MEMCACHED_SERVERS'}
52
        }
53
    );
54
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
55
    my $page = $cache->get_from_cache("$namespace:intranet:report:$report");
56
    if ($page) {
50
    if ($page) {
57
        print $query->header;
51
        print $query->header;
58
        print $page;
52
        print $page;
Lines 72-79 my $lines = $sth->fetchall_arrayref; Link Here
72
my $json_text = to_json($lines);
66
my $json_text = to_json($lines);
73
print $json_text;
67
print $json_text;
74
68
75
if ($usecache) {
69
if (Koha::Cache->is_cache_active) {
76
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
70
    $cache->set_in_cache( "intranet:report:$report", $json_text, $cache_expiry );
77
    $cache->set_in_cache( "$namespace:intranet:report:$report",
78
        $json_text, $cache_expiry );
79
}
71
}
80
- 

Return to bug 7248