[Templates] stonehenge.com data/partial HTML caching
Randal L. Schwartz
merlyn@stonehenge.com
27 Sep 2002 07:15:18 -0700
OK, that was easier than I thought. I added
package Stonehenge::Template::Plugin::CacheFileCache;
use Cache::FileCache;
use base qw(Template::Plugin);
sub new {
my $class = shift;
my $context = shift;
Cache::FileCache->new(@_);
}
1;
to my webserver's mod_perl search path, and
TT2PluginBase Stonehenge::Template::Plugin
to my config file, and now I can say:
USE mycache = CacheFileCache({
namespace => 'stonehenge-nav_stacked_bars',
default_expires_in => 1800,
auto_purge_interval => 1800,
});
mykey = [class, instance, global.navbardata.modtime];
mykey = mykey.join(":");
result = mycache.get(mykey);
UNLESS result;
result = BLOCK;
[[ stuff to compute result deleted ]]
END;
mycache.set(mykey, result);
FILTER stderr; "cache miss on "; mykey; "\n"; END;
END;
result;
to my navbar generation template, and I've just cached the creation of
my navbars (function of class, instance, and the timestamp on the
navbar data). Next to add the caching of the analysis of a picture
directory index, and global.navbardata as well.
TT2 rocks. Cache::Cache rocks. 'nuff said.
[Yes, this was based on an idea from Perrin, but I wanted to be able to
cache values, and not just text.]
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!