Friday, October 8, 2010

HTML::Template CACHE LOAD, HIT and MISS

If you are sure you have mod_perl running and are using cache_debug to diagnose issues with repeated cache LOAD and cache MISS with HTML::Template then the following information might be useful.

Digging through the HTML::Template source I discovered the following:

my @key = ($options->{filepath});
push(@key, @{$options->{path}});
push(@key, $options->{search_path_on_include} || 0);
push(@key, $options->{loop_context_vars} || 0);
push(@key, $options->{global_vars} || 0);


The key under which the cached template is stored is a md5_hex of the previous values passed to the HTML::Template->new() method.  If you are pre-loading your templates in your startup with one set of any of these parameters but are later loading the same template with another set then you will see another CACHE LOAD.

For example if you pre-cache in your startup with:

HTML::Template->new( filename => '/my/path/templates/t.html', cache => 1);

and later in a Registry cgi you create a template object with:

my $tmpl = HTML::Template->new( filename => '/my/path/templates/t.html',  cache => 1, global_vars => 1);

you will see a CACHE LOAD rather than a CACHE HIT when /my/path/templates/t.html is loaded.  The end result is that you maybe loading and caching multiple copies of the same template unnecessarily and wasting memory.