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

(-)a/Koha/Cache.pm (-6 / +19 lines)
Lines 55-66 __PACKAGE__->mk_ro_accessors( qw( cache ) ); Link Here
55
55
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;
60
    my $subclass = __PACKAGE__."::".ucfirst($cache_type);
60
    my $subclass;
61
    my $cache    = $subclass->_cache_handle($param)
61
    # try to find which cache type we use
62
      or croak "Cannot create cache handle for '$cache_type'";
62
    if ($ENV{'MEMCACHED_SERVERS'}) {
63
    return bless $class->SUPER::new({cache => $cache}), $subclass;
63
        # we use MEMCACHE
64
        $subclass = __PACKAGE__."::Memcached";
65
        $cache    = $subclass->_cache_handle(
66
            {'cache_type'    => 'memcached',
67
            'cache_servers' => $ENV{'MEMCACHED_SERVERS'},
68
            'namespace'     => $ENV{'MEMCACHED_NAMESPACE'},
69
            }
70
        )
71
            or croak "Cannot create cache handle for memcache";
72
        return bless $class->SUPER::new({cache => $cache}), $subclass;
73
    } else {
74
        $ENV{DEBUG} && warn "No caching system";
75
        return;
76
    }
64
}
77
}
65
78
66
=head2 EXPORT
79
=head2 EXPORT
(-)a/Koha/Cache/Memcached.pm (-1 / +5 lines)
Lines 31-36 sub _cache_handle { Link Here
31
    my @servers = split /,/, $params->{'cache_servers'};
31
    my @servers = split /,/, $params->{'cache_servers'};
32
    return Cache::Memcached->new(
32
    return Cache::Memcached->new(
33
        servers   => \@servers,
33
        servers   => \@servers,
34
        debug   => 0,
35
        compress_threshold => 10_000,
36
        expire_time => 600,
34
        namespace => $params->{'namespace'} || 'KOHA',
37
        namespace => $params->{'namespace'} || 'KOHA',
35
    );
38
    );
36
}
39
}
Lines 39-45 sub set_in_cache { Link Here
39
    my ( $self, $key, $value, $expiry ) = @_;
42
    my ( $self, $key, $value, $expiry ) = @_;
40
    croak "No key" unless $key;
43
    croak "No key" unless $key;
41
    $self->cache->set_debug;
44
    $self->cache->set_debug;
42
45
    $ENV{DEBUG} && warn "Setting in memcache $key";
43
    if ( defined $expiry ) {
46
    if ( defined $expiry ) {
44
        return $self->cache->set( $key, $value, $expiry );
47
        return $self->cache->set( $key, $value, $expiry );
45
    }
48
    }
Lines 51-56 sub set_in_cache { Link Here
51
sub get_from_cache {
54
sub get_from_cache {
52
    my ( $self, $key ) = @_;
55
    my ( $self, $key ) = @_;
53
    croak "No key" unless $key;
56
    croak "No key" unless $key;
57
    $ENV{DEBUG} && warn "Getting from memcache $key"; 
54
    return $self->cache->get($key);
58
    return $self->cache->get($key);
55
}
59
}
56
60
(-)a/opac/svc/report (-16 / +9 lines)
Lines 28-52 use CGI; Link Here
28
my $query  = CGI->new();
28
my $query  = CGI->new();
29
my $report = $query->param('id');
29
my $report = $query->param('id');
30
30
31
my $cache;
31
# load caching. If we have one, then we will try to retrieve the report from the cache for better performances
32
my $usecache = C4::Context->ismemcached;
32
use Koha::Cache;
33
my $cache = Koha::Cache->new();
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 ($cache) {
39
    require Koha::Cache;
40
    $ENV{DEBUG} && warn "We have and will use a cache";
40
    Koha::Cache->import();
41
    my $page = $cache->get_from_cache("opac:report:$report");
41
    $cache = Koha::Cache->new(
42
        {
43
            'cache_type'    => 'memcached',
44
            'cache_servers' => $ENV{'MEMCACHED_SERVERS'}
45
        }
46
    );
47
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
48
    my $page = $cache->get_from_cache("$namespace:opac:report:$report");
49
    if ($page) {
42
    if ($page) {
43
        $ENV{DEBUG} && warn "Report $report retrieved from cache";
50
        print $query->header;
44
        print $query->header;
51
        print $page;
45
        print $page;
52
        exit;
46
        exit;
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 ($cache) {
65
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
59
    $cache->set_in_cache( "opac:report:$report",
66
    $cache->set_in_cache( "$namespace:opac:report:$report",
67
        $json_text, $cache_expiry );
60
        $json_text, $cache_expiry );
68
}
61
}
(-)a/svc/report (-16 / +7 lines)
Lines 29-36 use CGI; Link Here
29
my $query  = CGI->new();
29
my $query  = CGI->new();
30
my $report = $query->param('id');
30
my $report = $query->param('id');
31
31
32
my $cache;
32
# load caching. If we have one, then we will try to retrieve the report from the cache for better performances
33
my $usecache = C4::Context->ismemcached;
33
use Koha::Cache;
34
my $cache = Koha::Cache->new();
34
35
35
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36
    {
37
    {
Lines 42-58 my ( $template, $loggedinuser, $cookie ) = get_template_and_user( Link Here
42
    }
43
    }
43
);
44
);
44
45
45
if ($usecache) {
46
if ($cache) {
46
    require Koha::Cache;
47
    my $page = $cache->get_from_cache("intranet:report:$report");
47
    Koha::Cache->import();
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) {
48
    if ($page) {
57
        print $query->header;
49
        print $query->header;
58
        print $page;
50
        print $page;
Lines 72-79 my $lines = $sth->fetchall_arrayref; Link Here
72
my $json_text = to_json($lines);
64
my $json_text = to_json($lines);
73
print $json_text;
65
print $json_text;
74
66
75
if ($usecache) {
67
if ($cache) {
76
    my $namespace = $ENV{'MEMCACHED_NAMESPACE'} || 'koha';
68
    $cache->set_in_cache( "intranet:report:$report",
77
    $cache->set_in_cache( "$namespace:intranet:report:$report",
78
        $json_text, $cache_expiry );
69
        $json_text, $cache_expiry );
79
}
70
}
(-)a/t/Cache.t (-4 / +2 lines)
Lines 13-21 BEGIN { Link Here
13
}
13
}
14
14
15
SKIP: {
15
SKIP: {
16
    skip "Memcached not enabled", 7 unless C4::Context->ismemcached;
16
    my $cache = Koha::Cache->new();
17
17
    skip "Caching disabled", 7 unless $cache;
18
    my $cache = Koha::Cache->new ( { 'cache_servers' => $ENV{'MEMCACHED_SERVERS'} } );
19
18
20
    # test fetching an item that isnt in the cache
19
    # test fetching an item that isnt in the cache
21
    is( $cache->get_from_cache("not in here"), undef, "fetching item NOT in cache");
20
    is( $cache->get_from_cache("not in here"), undef, "fetching item NOT in cache");
22
- 

Return to bug 7248