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";
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
modules, allowing you to use any of them as a direct
Constructor method which creates a new
use Template::TT3::Cache; my $cache = Template::TT3::Cache->new();
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
0 to explicitly disable any caching.
my $cache = Template::TT3::Cache->new( size => 0 );
size value is
-1 which indicates an unlimited size for the
module defines the
constant for this value.
use Template::TT3::Constants 'CACHE_ALL'; my $cache = Template::TT3::Cache->new( size => CACHE_ALL );
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_ALL by specifying the
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
$cache->set( foo => $foo_component );
Fetch an item from the cache previously stored by calling
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
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 (
PREV) references. This method walks the list explciitly deleting
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
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.
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.
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.