1 package Koha::Template::Plugin::Cache;
3 # Copyright Catalyst IT 2011
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 3 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 use vars qw( $VERSION );
23 use base qw( Template::Plugin );
28 #------------------------------------------------------------------------
30 #------------------------------------------------------------------------
33 my ( $class, $context, $params ) = @_;
35 if ( $params->{cache} ) {
36 $cache = delete $params->{cache};
40 $cache = Koha::Cache->new( { 'cache_type' => 'memcached', 'cache_servers' => C4::Context->config('memcached_servers') });
50 #------------------------------------------------------------------------
52 # template => 'foo.html',
53 # keys => {'user.name', user.name},
56 #------------------------------------------------------------------------
59 my ( $self, $params ) = @_;
60 $self->_cached_action( 'include', $params );
64 my ( $self, $params ) = @_;
65 $self->_cached_action( 'process', $params );
69 my ( $self, $action, $params ) = @_;
71 if ( $params->{key} ) {
72 $key = delete $params->{key};
75 my $cache_keys = $params->{keys};
80 map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
84 my $result = $self->{CACHE}->get_from_cache($key);
86 $result = $self->{CONTEXT}->$action( $params->{template} );
87 $self->{CACHE}->set_in_cache( $key, $result, $params->{ttl} );
96 Koha::Template::Plugin::Cache - cache output of templates
100 [% USE cache = Cache %]
104 'template' => 'slow.html',
105 'keys' => {'user.name' => user.name},
109 # or with a pre-defined Cache::* object and key
111 [% USE cache = Cache( cache => mycache ) %]
114 'template' => 'slow.html',