e7d26300aac21324eac6ce5478c81059a1f24d87
[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( );
23 use base qw( Template::Plugin );
24 use Template::Plugin;
25 use C4::Context;
26
27 #------------------------------------------------------------------------
28 # new(\%options)
29 #------------------------------------------------------------------------
30
31 sub new {
32     my ( $class, $context, $params ) = @_;
33     my $cache;
34     if ( $params->{cache} ) {
35         $cache = delete $params->{cache};
36     }
37     else {
38         require Koha::Cache;
39         $cache = Koha::Cache->get_instance();
40     }
41     my $self = bless {
42         CACHE   => $cache,
43         CONFIG  => $params,
44         CONTEXT => $context,
45     }, $class;
46     return $self;
47 }
48
49 #------------------------------------------------------------------------
50 # $cache->include({
51 #                 template => 'foo.html',
52 #                 keys     => {'user.name', user.name},
53 #                 ttl      => 60, #seconds
54 #                });
55 #------------------------------------------------------------------------
56
57 sub inc {
58     my ( $self, $params ) = @_;
59     $self->_cached_action( 'include', $params );
60 }
61
62 sub proc {
63     my ( $self, $params ) = @_;
64     $self->_cached_action( 'process', $params );
65 }
66
67 sub _cached_action {
68     my ( $self, $action, $params ) = @_;
69     my $key;
70     if ( $params->{key} ) {
71         $key = delete $params->{key};
72     }
73     else {
74         my $cache_keys = $params->{keys};
75         $key = join(
76             ':',
77             (
78                 $params->{template},
79                 map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
80             )
81         );
82     }
83     my $result = $self->{CACHE}->get_from_cache($key);
84     if ( !$result ) {
85         $result = $self->{CONTEXT}->$action( $params->{template} );
86         $self->{CACHE}
87           ->set_in_cache( $key, $result, { expiry => $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__