Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Template Toolkit Cache

by avo (Pilgrim)
on Nov 15, 2008 at 08:14 UTC ( [id://723781]=perlquestion: print w/replies, xml ) Need Help??

avo has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using some templates that I keep in my program instead of a file (using process with a ref to my scalar). Is there a way I can catch a compiled cache version of the template and store it for further use instead of processing every time. My processing time is quite expensive. I know TT2 does that in a file with extension provided by me (COMPILE_EXT) when I am using file instead. Thanks for your help.

Replies are listed 'Best First'.
Re: Template Toolkit Cache
by LanX (Saint) on Nov 15, 2008 at 11:33 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Template Toolkit Cache
by ikegami (Patriarch) on Nov 16, 2008 at 09:31 UTC

    I just had a peek at the source. The relevant code is in Template::Provider. The compiled template cache code is highly dependent on the presence of a file name.

    Your best bet might be to subclass Template::Provider, overriding _template_modified($path) and _template_content($path) to fetch the template from memory instead of fetching the template from disk. That will allow you to use a file name to fetch the template (albeit to a virtual file), which will allow TT2 to cache the compiled template.

    Update: Subclassing would look something like:

    use strict; use warnings; BEGIN { package Template::Provider::Memory; use Template::Provider qw( ); BEGIN { our @ISA = 'Template::Provider'; } sub _init { my ($self, $params) = @_; $self->{ MEM_DRIVE } = $params->{ MEM_DRIVE }; $self->SUPER::_init($params); } sub _template_modified { my ($self, $path) = @_; if (!exists( $self->{ MEM_DRIVE }{ $path } )) { return undef; } return $self->{ MEM_DRIVE }{ $path }[0]; } sub _template_content { my ($self, $path) = @_; my $data; my $mod_date; my $error; if (exists( $self->{ MEM_DRIVE }{ $path } )) { $mod_date = $self->{ MEM_DRIVE }{ $path }[0]; $data = $self->{ MEM_DRIVE }{ $path }[1]; } else { $error = "$path: Not found"; } return (wantarray ? ( $data, $error, $mod_date ) : $data ); } $INC{'Template/Provider/Memory.pm'} = 1; } use Template qw( ); use Template::Provider::Memory qw( ); my %mem_drive = ( 'index.tt2' => [ time(), 'Hello World!' ], ); my $tt = Template->new({ LOAD_TEMPLATES => [ Template::Provider::Memory->new({ MEM_DRIVE => \%mem_drive }), Template::Provider->new(), ], COMPILE_DIR => '/tmp/ttc', COMPILE_EXT => '.ttc', }); $tt->process('index.tt2') or die($tt->error(), "\n");

    That was tough to decipher! Runs, but it doesn't create the compiled file.

Re: Template Toolkit Cache
by Anonymous Monk on Nov 15, 2008 at 08:29 UTC
    Is there a way I can catch a compiled cache version of the template and store it for further use instead of processing every time.
    Simples solution is create the files if they don't exist, so you only have to do it once.

    Or you could try Template::Provider::MD5.

Re: Template Toolkit Cache
by wrinkles (Pilgrim) on Nov 15, 2008 at 19:47 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://723781]
Approved by wfsp
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found