3 # Copyright 2009 Chris Cormack and The Koha Dev Team
5 # This file is part of Koha.
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
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.
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.
22 Koha::Cache - Handling caching of html and Objects for Koha
26 use Koha::Cache (cache_type => $cache_type, %params );
30 Base class for Koha::Cache::X. Subclasses must provide the following methods
32 B<_cache_handle ($params_hr)> - cache handle creator
34 Subclasses may override the following methods if they are not using a
37 B<set_in_cache ($key, $value, $expiry)>
39 B<get_from_cache ($key)>
41 B<clear_from_cache ($key)>
52 use Module::Load::Conditional qw(can_load);
58 if ( can_load( modules => { CHI => undef } ) ) {
63 use base qw(Class::Accessor);
65 __PACKAGE__->mk_ro_accessors(qw( cache ));
72 || $param->{cache_type}
74 my $subclass = __PACKAGE__ . "::" . ucfirst($cache_type);
75 $param->{have_chi} = $have_chi;
76 unless ( can_load( modules => { $subclass => undef } ) ) {
77 $subclass = __PACKAGE__ . "::" . ucfirst('Null');
80 my $cache = $subclass->_cache_handle($param);
82 bless $class->SUPER::new( { cache => $cache, have_chi => $have_chi } ),
87 return $ENV{CACHING_SYSTEM} ? '1' : '';
91 my ( $self, $key, $value, $expiry ) = @_;
92 croak "No key" unless $key;
93 $ENV{DEBUG} && warn "set_in_cache for $key";
95 return unless $self->{cache};
96 return unless $self->{have_chi};
98 if ( defined $expiry ) {
99 return $self->{cache}->set( $key, $value, $expiry );
102 return $self->{cache}->set( $key, $value );
107 my ( $self, $key ) = @_;
108 croak "No key" unless $key;
109 $ENV{DEBUG} && warn "get_from_cache for $key";
110 return unless $self->{cache};
111 return unless $self->{have_chi};
112 return $self->{cache}->get($key);
115 sub clear_from_cache {
116 my ( $self, $key ) = @_;
117 croak "No key" unless $key;
118 return unless $self->{cache};
119 return unless $self->{have_chi};
120 return $self->{cache}->remove($key);
125 return unless $self->{cache};
126 return unless $self->{have_chi};
127 return $self->{cache}->clear();
136 Koha::Cache::Memcached
140 Chris Cormack, E<lt>chris@bigballofwax.co.nzE<gt>
141 Paul Poulain, E<lt>paul.poulain@biblibre.comE<gt>
142 Jared Camins-Esakov, E<lt>jcamins@cpbibliography.comE<gt>