Bug 18309: Add UNIMARC field 214 and its subfields
[koha.git] / Koha / Template / Plugin / Cache.pm
blob5e3f8f7d77a87fb92b9986a9cf4404df00d3949e
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
10 # version.
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.
20 use strict;
21 use warnings;
22 use base qw( Template::Plugin );
23 use Template::Plugin;
24 use C4::Context;
26 #------------------------------------------------------------------------
27 # new(\%options)
28 #------------------------------------------------------------------------
30 sub new {
31 my ( $class, $context, $params ) = @_;
32 my $cache;
33 if ( $params->{cache} ) {
34 $cache = delete $params->{cache};
36 else {
37 require Koha::Cache;
38 $cache = Koha::Caches->get_instance();
40 my $self = bless {
41 CACHE => $cache,
42 CONFIG => $params,
43 CONTEXT => $context,
44 }, $class;
45 return $self;
48 #------------------------------------------------------------------------
49 # $cache->include({
50 # template => 'foo.html',
51 # keys => {'user.name', user.name},
52 # ttl => 60, #seconds
53 # });
54 #------------------------------------------------------------------------
56 sub inc {
57 my ( $self, $params ) = @_;
58 $self->_cached_action( 'include', $params );
61 sub proc {
62 my ( $self, $params ) = @_;
63 $self->_cached_action( 'process', $params );
66 sub _cached_action {
67 my ( $self, $action, $params ) = @_;
68 my $key;
69 if ( $params->{key} ) {
70 $key = delete $params->{key};
72 else {
73 my $cache_keys = $params->{keys};
74 $key = join(
75 ':',
77 $params->{template},
78 map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
82 my $result = $self->{CACHE}->get_from_cache($key);
83 if ( !$result ) {
84 $result = $self->{CONTEXT}->$action( $params->{template} );
85 $self->{CACHE}
86 ->set_in_cache( $key, $result, { expiry => $params->{ttl} } );
88 return $result;
93 =head1 NAME
95 Koha::Template::Plugin::Cache - cache output of templates
97 =head1 SYNOPSIS
99 [% USE cache = Cache %]
102 [% cache.inc(
103 'template' => 'slow.html',
104 'keys' => {'user.name' => user.name},
105 'ttl' => 360
106 ) %]
108 # or with a pre-defined Cache::* object and key
110 [% USE cache = Cache( cache => mycache ) %]
112 [% cache.inc(
113 'template' => 'slow.html',
114 'key' => mykey,
115 'ttl' => 360
116 ) %]
118 __END__