Added support for transparent template caching.
This commit is contained in:
parent
6bb244472f
commit
e0792ca8b4
15
Template.pm
15
Template.pm
@ -183,6 +183,10 @@ BEGIN {
|
|||||||
# to mark the location of an ordinal specifier. %o is ignored if it
|
# to mark the location of an ordinal specifier. %o is ignored if it
|
||||||
# does not immediately follow a digit field. Defaults to "%a, %d %b %Y %H:%M:%S"
|
# does not immediately follow a digit field. Defaults to "%a, %d %b %Y %H:%M:%S"
|
||||||
# - `blockname` If set, allow blocks to be specified by name rather than id.
|
# - `blockname` If set, allow blocks to be specified by name rather than id.
|
||||||
|
# - `usecache` If true, template files are cached as they are loaded. This will increase memory use, but
|
||||||
|
# can make template operations faster in situations where the same template needs to be used
|
||||||
|
# repeatedly, but explicity preloading can't be performed. Template files are reloaded if the
|
||||||
|
# file mtime has changed since the last load to avoid stale cache entries. This defaults to true.
|
||||||
sub new {
|
sub new {
|
||||||
my $invocant = shift;
|
my $invocant = shift;
|
||||||
my $class = ref($invocant) || $invocant;
|
my $class = ref($invocant) || $invocant;
|
||||||
@ -199,6 +203,7 @@ sub new {
|
|||||||
"entities" => $entities,
|
"entities" => $entities,
|
||||||
"utfentities" => $utfentities,
|
"utfentities" => $utfentities,
|
||||||
"blockname" => 0,
|
"blockname" => 0,
|
||||||
|
"usecache" => 1,
|
||||||
@_,
|
@_,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -482,6 +487,12 @@ sub load_template {
|
|||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my $filemtime = (stat($filename))[9];
|
||||||
|
|
||||||
|
# If caching is enabled, and the times match, the file has been loaded before
|
||||||
|
return $self -> process_template($self -> {"cache"} -> {$name} -> {"template"}, $varmap, $nocharfix)
|
||||||
|
if($self -> {"usecache"} && $self -> {"cache"} -> {$name} -> {"mtime"} == $filemtime);
|
||||||
|
|
||||||
# Try the load and process the template...
|
# Try the load and process the template...
|
||||||
if(open(TEMPLATE, "<:utf8", $filename)) {
|
if(open(TEMPLATE, "<:utf8", $filename)) {
|
||||||
undef $/;
|
undef $/;
|
||||||
@ -489,6 +500,10 @@ sub load_template {
|
|||||||
$/ = "\n";
|
$/ = "\n";
|
||||||
close(TEMPLATE);
|
close(TEMPLATE);
|
||||||
|
|
||||||
|
# Cache the template if needed.
|
||||||
|
$self -> {"cache"} -> {$name} = { "template" => $lines, "mtime" => $filemtime }
|
||||||
|
if($self -> {"usecache"});
|
||||||
|
|
||||||
# Do variable substitution
|
# Do variable substitution
|
||||||
$self -> process_template(\$lines, $varmap, $nocharfix);
|
$self -> process_template(\$lines, $varmap, $nocharfix);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user