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

(-)a/Koha/Template/Plugin/Cache.pm (-1 / +120 lines)
Line 0 Link Here
0
- 
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
	warn "here in not in cache";
87
	$result = $self->{CONTEXT}->$action( $params->{template} );
88
	$self->{CACHE}->set_in_cache( $key, $result, $params->{ttl} );
89
    }
90
    return $result;
91
}
92
93
1;
94
95
=head1 NAME
96
97
Koha::Template::Plugin::Cache - cache output of templates
98
99
=head1 SYNOPSIS
100
101
  [% USE cache = Cache %]
102
103
104
  [% cache.inc(
105
                   'template' => 'slow.html',
106
                   'keys' => {'user.name' => user.name},
107
                   'ttl' => 360
108
                   ) %]
109
110
  # or with a pre-defined Cache::* object and key
111
112
  [% USE cache = Cache( cache => mycache ) %]
113
114
  [% cache.inc(
115
                       'template' => 'slow.html',
116
                       'key'      => mykey,
117
                       'ttl'      => 360
118
                       )  %]
119
120
__END__

Return to bug 7387