Bug 7248 follow-up (alternative)
[koha.git] / Koha / Cache / Memcached.pm
1 package Koha::Cache::Memcached;
2
3 # Copyright 2009 Chris Cormack and The Koha Dev Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use Carp;
23
24 use Cache::Memcached;
25
26 use base qw(Koha::Cache);
27
28 sub _cache_handle {
29     my $class  = shift;
30     my $params = shift;
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');
33     return Cache::Memcached->new(
34         servers   => \@servers,
35         debug   => 0,
36         compress_threshold => 10_000,
37         expire_time => 600,
38         namespace => $ENV{MEMCACHED_NAMESPACE} || $params->{'namespace'} || 'koha',
39     );
40 }
41
42 sub set_in_cache {
43     my ( $self, $key, $value, $expiry ) = @_;
44     croak "No key" unless $key;
45     $self->cache->set_debug;
46     $ENV{DEBUG} && warn "set_in_cache for Memcache $key";
47
48     if ( defined $expiry ) {
49         return $self->cache->set( $key, $value, $expiry );
50     }
51     else {
52         return $self->cache->set( $key, $value );
53     }
54 }
55
56 sub get_from_cache {
57     my ( $self, $key ) = @_;
58     croak "No key" unless $key;
59     $ENV{DEBUG} && warn "get_from_cache for Memcache $key";
60     return $self->cache->get($key);
61 }
62
63 sub clear_from_cache {
64     my ( $self, $key ) = @_;
65     croak "No key" unless $key;
66     return $self->cache->delete($key);
67 }
68
69 sub flush_all {
70     my $self = shift;
71     return $self->cache->flush_all;
72 }
73
74 1;
75 __END__
76
77 =head1 NAME
78
79 Koha::Cache::Memcached - memcached subclass of Koha::Cache
80
81 =cut