View | Details | Raw Unified | Return to bug 7387
Collapse All | Expand All

(-)a/Koha/Template/Plugin/Cache.pm (+119 lines)
Line 0 Link Here
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 2 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 vars qw( $VERSION );
22
use base qw( Template::Plugin );
23
use Template::Plugin;
24
use C4::Context;
25
$VERSION = '0.01';
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->new( { 'cache_servers' => C4::Context->config('memcached_servers') });
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
	warn "here in not in cache";
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__
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt (-2 / +7 lines)
Lines 1-4 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
1
[% USE cache = Cache %]
2
[% cache.inc(
3
    'template' => 'doc-head-open.inc',
4
    'keys' => {'dochead' => 'dochead'},
5
    'ttl' => 360
6
    )
7
%]
2
<title>Koha staff client</title>
8
<title>Koha staff client</title>
3
[% INCLUDE 'doc-head-close.inc' %]
9
[% INCLUDE 'doc-head-close.inc' %]
4
<style type="text/css"> #koha-news { border: 1px solid #EEEEEE; margin: .2em;} #koha-news h3 {background-color: #EEEEEE; padding : .3em; margin: 0;} .newsitem {margin: .3em; border-bottom: 1px solid #EEE;} .newsitem:last-child {border-bottom : 0;} .newsfooter {font-size: 80%; color: #CCCCCC;} </style>
10
<style type="text/css"> #koha-news { border: 1px solid #EEEEEE; margin: .2em;} #koha-news h3 {background-color: #EEEEEE; padding : .3em; margin: 0;} .newsitem {margin: .3em; border-bottom: 1px solid #EEE;} .newsitem:last-child {border-bottom : 0;} .newsfooter {font-size: 80%; color: #CCCCCC;} </style>
5
- 

Return to bug 7387