Modules for caching - C4::Cache is the base class, C4::Cache::Memcached and C4::Cache...
[koha.git] / C4 / Cache / FastMemcached.pm
1 package Koha::Cache::FastMemcached;
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 with                                                                 
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,                                                                  
18 # Suite 330, Boston, MA  02111-1307 USA    
19
20 use strict;
21 use warnings;
22 use Carp;
23
24 use Cache::Memcached::Fast;
25 use IO::Compress::Gzip;
26 use IO::Uncompress::Gunzip;
27 use Storable;
28
29 use base qw(C4::Cache);
30
31 sub _cache_handle {
32     my $class  = shift;
33     my $params = shift;
34
35     my @servers = split /,/, $params->{'cache_servers'};
36
37     return Cache::Memcached::Fast->new(
38         {
39             servers            => \@servers,
40             namespace          => $params->{'namespace'} || 'KOHA',
41             connect_timeout    => $params->{'connect_timeout'} || 2,
42             io_timeout         => $params->{'io_timeout'} || 2,
43             close_on_error     => 1,
44             compress_threshold => 100_000,
45             compress_ratio     => 0.9,
46             compress_methods =>
47               [ \&IO::Compress::Gzip::gzip, \&IO::Uncompress::Gunzip::gunzip ],
48             max_failures      => 3,
49             failure_timeout   => 2,
50             ketama_points     => 150,
51             nowait            => 1,
52             hash_namespace    => 1,
53             serialize_methods => [ \&Storable::freeze, \&Storable::thaw ],
54             utf8              => 1,
55         }
56     );
57 }
58
59 sub set_in_cache {
60     my ( $self, $key, $value, $expiry ) = @_;
61     croak "No key" unless $key;
62
63     if ( defined $expiry ) {
64         return $self->cache->set( $key, $value, $expiry );
65     }
66     else {
67         return $self->cache->set( $key, $value );
68     }
69 }
70
71 sub get_from_cache {
72     my ( $self, $key ) = @_;
73     croak "No key" unless $key;
74     return $self->cache->get($key);
75 }
76
77 sub clear_from_cache {
78     my ( $self, $key ) = @_;
79     croak "No key" unless $key;
80     return $self->cache->delete($key);
81 }
82
83 sub flush_all {
84     my $self = shift;
85     return $self->cache->flush_all;
86 }
87
88 1;
89 __END__                                                                         
90                                                                                   
91 =head1 NAME                                                                     
92                                                                                 
93   C4::Cache::FastMemcached - memcached::fast subclass of C4::Cache                
94                                                                                   
95 =cut