Skip to content
Newer
Older
100644 254 lines (201 sloc) 6.73 KB
8c276c0 @abw moved todo files to new location
authored Feb 9, 2007
1 From: Perrin Harkins <perrin@elem.com>
2 X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.16-22 i686)
3 X-Accept-Language: en
4 MIME-Version: 1.0
5 To: templates@template-toolkit.org
6 Content-Type: multipart/mixed;
7 boundary="------------A76E8171DCE73B663B9DA377"
8 Subject: [Templates] RFC: Template::Plugin::Cache
9 Sender: templates-admin@template-toolkit.org
10 Errors-To: templates-admin@template-toolkit.org
11 X-BeenThere: templates@template-toolkit.org
12 X-Mailman-Version: 2.0rc1
13 Precedence: bulk
14 List-Help: <mailto:templates-request@template-toolkit.org?subject=help>
15 List-Post: <mailto:templates@template-toolkit.org>
16 List-Subscribe: <http://www.template-toolkit.org/mailman/listinfo/templates>,
17 <mailto:templates-request@template-toolkit.org?subject=subscribe>
18 List-Id: Template Toolkit mailing list <templates.template-toolkit.org>
19 List-Unsubscribe: <http://www.template-toolkit.org/mailman/listinfo/templates>,
20 <mailto:templates-request@template-toolkit.org?subject=unsubscribe>
21 List-Archive: <http://www.template-toolkit.org/pipermail/templates/>
22 Date: Wed, 01 Aug 2001 01:56:12 -0700
23 X-UIDL: 4b52ce5bbd5e4261cad4eafdc1bb408b
24 Status: RO
25 Content-Length: 5732
26 Lines: 224
27
28 This is a multi-part message in MIME format.
29 --------------A76E8171DCE73B663B9DA377
30 Content-Type: text/plain; charset=us-ascii
31 Content-Transfer-Encoding: 7bit
32
33 Here's my first draft. I'm including the module and a test script. Go
34 wild. If this turns into something legit, I'll put it on CPAN.
35 - Perrin
36 --------------A76E8171DCE73B663B9DA377
37 Content-Type: application/x-perl;
38 name="test_plugin.pl"
39 Content-Transfer-Encoding: 7bit
40 Content-Disposition: inline;
41 filename="test_plugin.pl"
42
43 #!/usr/bin/perl -w
44
45 use strict;
46 use Template;
47 $| = 1;
48
49 my $template = <<EOM;
50 [% USE cache = Cache %]
51 [% BLOCK cache_me %]
52 -----------------------
53 About to do a lot of work...
54 [% PERL %]
55 sleep 5;
56 [% END %]
57 And now to demonstrate that the keys matter:
58 Hello [% name %]
59 -----------------------
60 [% END %]
61 [% cache.inc(
62 'template' => 'cache_me',
63 'keys' => {'name' => name},
64 'ttl' => 15
65 ) %]
66 EOM
67
68 my $t = Template->new(
69 EVAL_PERL => 1,
70 ANYCASE => 1,
71 );
72
73 print "first run with name = Suzanne";
74 $t->process(\$template, { name => 'Suzanne' }) || die $t->error();
75
76 print "second run with name = Suzanne\n\n";
77 $t->process(\$template, { name => 'Suzanne' }) || die $t->error();
78
79 print "first run with name = World\n\n";
80 $t->process(\$template, { name => 'World' }) || die $t->error();
81
82 print "second run with name = World\n\n";
83 $t->process(\$template, { name => 'World' }) || die $t->error();
84
85 --------------A76E8171DCE73B663B9DA377
86 Content-Type: text/plain; charset=us-ascii;
87 name="Cache.pm"
88 Content-Transfer-Encoding: 7bit
89 Content-Disposition: inline;
90 filename="Cache.pm"
91
92 #============================================================= -*-Perl-*-
93 #
94 # Template::Plugin::Cache
95 #
96 # DESCRIPTION
97 #
98 # Plugin to cache template output
99 #
100 # AUTHORS
101 # Perrin Harkins <perrin@elem.com>
102 # (your name here)
103 #
104 # COPYRIGHT
105 # Copyright (C) 2000 Perrin Harkins.
106 #
107 # This module is free software; you can redistribute it and/or
108 # modify it under the same terms as Perl itself.
109 #
110 #----------------------------------------------------------------------------
111 #
112 # $Id$
113 #
114 #============================================================================
115
116 package Template::Plugin::Cache;
117
118 use strict;
119 use vars qw( $VERSION );
120 use base qw( Template::Plugin );
121 use Template::Plugin;
122
123 use File::Cache;
124
125 $VERSION = 0.1;
126
127 #------------------------------------------------------------------------
128 # new(\%options)
129 #------------------------------------------------------------------------
130
131 sub new {
132 my ($class, $context, $params) = @_;
133 my $cache = File::Cache->new($params);
134 my $self = bless {
135 CACHE => $cache,
136 CONFIG => $params,
137 CONTEXT => $context,
138 }, $class;
139 return $self;
140 }
141
142 #------------------------------------------------------------------------
143 # $cache->inc({
144 # template => 'foo.html',
145 # keys => {'user.name', user.name},
146 # ttl => 60, #seconds
147 # });
148 #------------------------------------------------------------------------
149
150 sub inc {
151 my ($self, $params) = @_;
152 my $cache_keys = $params->{keys};
153 #warn %$cache_keys;
154 my $key = join(
155 ':',
156 (
157 $params->{template},
158 map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
159 )
160 );
161 my $result = $self->{CACHE}->get($key);
162 if (!$result) {
163 #warn "processing template";
164 $result = $self->{CONTEXT}->include($params->{template});
165 $self->{CACHE}->set($key, $result, $params->{ttl});
166 }
167 return $result;
168 }
169
170 1;
171
172 __END__
173
174 =head1 NAME
175
176 Template::Plugin::Cache - cache output of templates
177
178 =head1 SYNOPSIS
179
180 [% USE cache = Cache%]
181
182
183 [% cache.inc(
184 'template' => 'slow.html',
185 'keys' => {'user.name' => user.name},
186 'ttl' => 360
187 ) %]
188
189 =head1 DESCRIPTION
190
191 The Cache plugin allows you to cache generated output from a template.
192 You load the plugin with the standard syntax:
193
194 [% USE cache = Cache %]
195
196 This creates a plugin object with the name 'cache'. You may also
197 specify parameters for the File::Cache module, which is used for
198 storage.
199
200 [% USE mycache = Cache(namespace => 'MyCache') %]
201
202 The only method currently available is an include, "cache.inc", which
203 can work on blocks or files the same as the standard INCLUDE
204 directive.
205
206 [% cache.inc(
207 'template' => 'slow.html',
208 'keys' => {'user.name' => user.name},
209 'ttl' => 360
210 ) %]
211
212 The template parameter names the file or block to include. The keys
213 are variables used to identify the correct cache file. Different
214 values for the specified keys will result in different cache files.
215 The ttl parameter specifies the "time to live" for this cache file, in
216 seconds.
217
218 Why the ugliness on the keys? Well, the TT dot notation can only be
219 resolved correctly by the TT parser at compile time. It's easy to
220 look up simple variable names in the stash, but compound names like
221 "user.name" are hard to resolve at runtime. I may attempt to fake
222 this in a future version, but it would be hacky and might cause
223 problems.
224
225 =head1 AUTHORS
226
227 Perrin Harkins (perrin@elem.com) wrote the first version of this
228 plugin, with help and suggestions from various parties.
229
230 =head1 COPYRIGHT
231
232 Copyright (C) 2001 Perrin Harkins.
233
234 This module is free software; you can redistribute it and/or
235 modify it under the same terms as Perl itself.
236
237 =head1 SEE ALSO
238
239 L<Template::Plugin|Template::Plugin>, L<File::Cache|File::Cache>
240
241 =cut
242
243
244 --------------A76E8171DCE73B663B9DA377--
245
246
247 _______________________________________________
248 templates mailing list
249 templates@template-toolkit.org
250 http://www.template-toolkit.org/mailman/listinfo/templates
251
252
253
Something went wrong with that request. Please try again.