Here's my first draft.  I'm including the module and a test script.  Go
wild.  If this turns into something legit, I'll put it on CPAN.
- Perrin

Attachment: test_plugin.pl
Description: Perl program

#============================================================= -*-Perl-*-
#
# Template::Plugin::Cache
#
# DESCRIPTION
#
#   Plugin to cache template output
#
# AUTHORS
#   Perrin Harkins           <[EMAIL PROTECTED]>
#   (your name here)
#
# COPYRIGHT
#   Copyright (C) 2000 Perrin Harkins.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
#----------------------------------------------------------------------------
#
# $Id$
#
#============================================================================

package Template::Plugin::Cache;

use strict;
use vars qw( $VERSION );
use base qw( Template::Plugin );
use Template::Plugin;

use File::Cache;

$VERSION = 0.1;

#------------------------------------------------------------------------
# new(\%options)
#------------------------------------------------------------------------

sub new {
    my ($class, $context, $params) = @_;
    my $cache = File::Cache->new($params);
    my $self = bless {
                      CACHE   => $cache,
                      CONFIG  => $params,
                      CONTEXT => $context,
                     }, $class;
    return $self;
}

#------------------------------------------------------------------------
# $cache->inc({
#              template => 'foo.html',
#              keys     => {'user.name', user.name},
#              ttl      => 60, #seconds
#             });
#------------------------------------------------------------------------

sub inc {
    my ($self, $params) = @_;
    my $cache_keys = $params->{keys};
    #warn %$cache_keys;
    my $key = join(
                   ':',
                   (
                    $params->{template},
                    map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
                   )
                  );
    my $result = $self->{CACHE}->get($key);
    if (!$result) {
      #warn "processing template";
      $result = $self->{CONTEXT}->include($params->{template});
      $self->{CACHE}->set($key, $result, $params->{ttl});
    }
    return $result;
}

1;

__END__

=head1 NAME

Template::Plugin::Cache - cache output of templates

=head1 SYNOPSIS

  [% USE cache = Cache%]
    

  [% cache.inc(
               'template' => 'slow.html',
               'keys' => {'user.name' => user.name},
               'ttl' => 360
               ) %]

=head1 DESCRIPTION

The Cache plugin allows you to cache generated output from a template.
You load the plugin with the standard syntax:

    [% USE cache = Cache %]

This creates a plugin object with the name 'cache'.  You may also
specify parameters for the File::Cache module, which is used for
storage.

    [% USE mycache = Cache(namespace => 'MyCache') %]

The only method currently available is an include, "cache.inc", which
can work on blocks or files the same as the standard INCLUDE
directive.

  [% cache.inc(
               'template' => 'slow.html',
               'keys' => {'user.name' => user.name},
               'ttl' => 360
               ) %]

The template parameter names the file or block to include.  The keys
are variables used to identify the correct cache file.  Different
values for the specified keys will result in different cache files.
The ttl parameter specifies the "time to live" for this cache file, in
seconds.

Why the ugliness on the keys?  Well, the TT dot notation can only be
resolved correctly by the TT parser at compile time.  It's easy to
look up simple variable names in the stash, but compound names like
"user.name" are hard to resolve at runtime.  I may attempt to fake
this in a future version, but it would be hacky and might cause
problems.

=head1 AUTHORS

Perrin Harkins ([EMAIL PROTECTED]) wrote the first version of this
plugin, with help and suggestions from various parties.

=head1 COPYRIGHT

Copyright (C) 2001 Perrin Harkins.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<Template::Plugin|Template::Plugin>, L<File::Cache|File::Cache>

=cut

Reply via email to