use Template::TT3::Cache; my $cache = Template::TT3::Cache->new( size => 32 ); $cache->set( foo => $foo ); # ...later... $foo = $cache->get('foo') || warn "foo has expired from cache\n";
The Template::TT3::Cache
module implements a simple in-memory cache for
compiled template documents.
The most time-consuming part of processing a template is the initial phase in which we read in the template source, parse it, and compile it into Perl code. The Perl code is then evaluated and should result in an object being created which implements the functionality of the original template. Fortunately, we only need to compile the template once and can then re-use the generated object as many times as we like.
Template::TT3::Cache
provides a simple mechanism for limiting the number of
templates that are cached and automatically discards the least-recently-used
component when the limit is reached.
It also defines a simple API and can act as a base class for modules that
implement different caching mechanisms. The API is deliberately compatible
with the
Cache::Cache
modules, allowing you to use any of them as a direct
replacement for Template::TT3::Cache
.
The following methods are implemented in addition to those inherited from the Template::TT3::Base and Badger::Base base classes.
Constructor method which creates a new Template::TT3::Cache
object.
use Template::TT3::Cache; my $cache = Template::TT3::Cache->new();
The size
parameter (or cache_size
if you prefer your parameters a little
more long-winded) can be specified to define a limit to the number of items
that the cache will store at any one time.
my $cache = Template::TT3::Cache->new( size => 32 ); # either my $cache = Template::TT3::Cache->new( cache_size => 32 ); # or
Set size
to 0
to explicitly disable any caching.
my $cache = Template::TT3::Cache->new( size => 0 );
The default size
value is -1
which indicates an unlimited size for the
cache. The
Template::TT3::Constants
module defines the CACHE_ALL
constant for this value.
use Template::TT3::Constants 'CACHE_ALL'; my $cache = Template::TT3::Cache->new( size => CACHE_ALL );
The CACHE_NONE
(0) constant is also provided for completeness.
use Template::TT3::Constants 'CACHE_NONE'; my $cache = Template::TT3::Cache->new( size => CACHE_NONE );
You can import both CACHE_NONE
and CACHE_ALL
by specifying the :cache
constant group.
use Template::TT3::Constants ':cache';
Add an item to the cache. The first argument provides a name for the
component passed as the second argument, by which it can subsequently
be fetched via the get()
method.
$cache->set( foo => $foo_component );
Fetch an item from the cache previously stored by calling set()
.
If the item is not in the cache, either because it was never been
put in the cache or because it was, but has subsequently expired,
then the method returns undef
.
This method deletes all items from the cache and frees the memory associated with the cache slots. It is called automatically by the DESTROY method when the cache object goes out of scope.
For the technically minded, the Least-Recently-Used algorithm implements a
doubly linked list of slots. Perl cannot free this data structure
automatically due to the circular references between the forward (NEXT
) and
backward (PREV
) references. This method walks the list explciitly deleting
all the NEXT/PREV
references, allowing the proper cleanup to occur and
memory to be repooled.
This is an alias to the clear()
method in keeping with other TT3 modules
that follow that convention. It is called automatically by the DESTROY
method when the cache object goes out of scope and is garbage collected.
You can manually call the destroy()
method if you want to, but you probably
shouldn't ever need to.
Internal method to insert a cache slot at the head of the linked list. New slots are always inserted at the head of the list. Each time an entry is fetched from the cache, we remove the slot from its current position in the list and re-insert it at the head. Thus, the list remain sorted in most-recently-used to least-recently-used order.
# first and last items in slot are prev/next references which the # _insert_slot() method will fill in $self->_insert_slot([undef, $name, $data, undef]);
Internal method to remove a slot from the linked list.
$self->_remove_slot($slot);
Perl calls this method automatically when the cache object goes out of scope. In turn it calls the destroy() method which is a simple alias to the clear() method which releases the memory retained by the cache slots.
Subclasses may want to define the destroy() method to implement different or additional behaviour.
Andy Wardley http://wardley.org
Copyright (C) 1996-2009 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Template::TT3::Base , Badger::Base .
See
Cache::Cache
for various different caching modules that implement
different caching strategies and can be used in place of
Template::TT3::Cache
.