Merge branch 'bug_7387' into 3.12-master
[koha.git] / Koha / Template / Plugin / Cache.pm
1 package Koha::Template::Plugin::Cache;
2
3 # Copyright Catalyst IT 2011
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 3 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 vars qw( $VERSION );
23 use base qw( Template::Plugin );
24 use Template::Plugin;
25 use C4::Context;
26 $VERSION = '0.01';
27
28 #------------------------------------------------------------------------
29 # new(\%options)
30 #------------------------------------------------------------------------
31
32 sub new {
33     my ( $class, $context, $params ) = @_;
34     my $cache;
35     if ( $params->{cache} ) {
36         $cache = delete $params->{cache};
37     }
38     else {
39         require Koha::Cache;
40         $cache = Koha::Cache->new( { 'cache_type' => 'memcached', 'cache_servers' => C4::Context->config('memcached_servers') });
41     }
42     my $self = bless {
43         CACHE   => $cache,
44         CONFIG  => $params,
45         CONTEXT => $context,
46     }, $class;
47     return $self;
48 }
49
50 #------------------------------------------------------------------------
51 # $cache->include({
52 #                 template => 'foo.html',
53 #                 keys     => {'user.name', user.name},
54 #                 ttl      => 60, #seconds
55 #                });
56 #------------------------------------------------------------------------
57
58 sub inc {
59     my ( $self, $params ) = @_;
60     $self->_cached_action( 'include', $params );
61 }
62
63 sub proc {
64     my ( $self, $params ) = @_;
65     $self->_cached_action( 'process', $params );
66 }
67
68 sub _cached_action {
69     my ( $self, $action, $params ) = @_;
70     my $key;
71     if ( $params->{key} ) {
72         $key = delete $params->{key};
73     }
74     else {
75         my $cache_keys = $params->{keys};
76         $key = join(
77             ':',
78             (
79                 $params->{template},
80                 map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
81             )
82         );
83     }
84     my $result = $self->{CACHE}->get_from_cache($key);
85     if ( !$result ) {
86         $result = $self->{CONTEXT}->$action( $params->{template} );
87         $self->{CACHE}->set_in_cache( $key, $result, $params->{ttl} );
88     }
89     return $result;
90 }
91
92 1;
93
94 =head1 NAME
95
96 Koha::Template::Plugin::Cache - cache output of templates
97
98 =head1 SYNOPSIS
99
100   [% USE cache = Cache %]
101
102
103   [% cache.inc(
104                    'template' => 'slow.html',
105                    'keys' => {'user.name' => user.name},
106                    'ttl' => 360
107                    ) %]
108
109   # or with a pre-defined Cache::* object and key
110
111   [% USE cache = Cache( cache => mycache ) %]
112
113   [% cache.inc(
114                        'template' => 'slow.html',
115                        'key'      => mykey,
116                        'ttl'      => 360
117                        )  %]
118
119 __END__