NAME
Mojolicious::Plugin::Memorize - Memorize part of your Mojolicious template
SYNOPSIS
use Mojolicious::Lite;
plugin 'Memorize';
any '/' => 'index';
any '/reset' => sub {
my $self = shift;
$self->memorize->expire('access');
$self->redirect_to('/');
};
app->start;
__DATA__
@@ index.html.ep
%= memorize access => { expires => 0 } => begin
This page was memorized on
%= scalar localtime
% end
DESCRIPTION
This plugin provides the functionality to easily memorize a portion of a template, to prevent re-evaluation. This may be useful when a portion of your response is expensive to generate but changes rarely (a menu for example).
The memorize
helper derives from the helper that was removed from Mojolicious
at version 4.0, with one tiny addition, the underlying plugin object is returned when no arguments are passed. This makes more flexible interaction possible, including, as an example, the expire
method.
HELPERS
memorize( [$name,] [$args,] [$template_block] )
-
When called with arguments, the helper behaves as the old helper did. It takes as many as three arguments, the final of which must be a template block (begin/end, see "Blocks" in Mojolicious::Lite) to be memorized. The first argument may be a string which is the name (key) of the memorized template (used for later access), if this is not provided one will be generated. A hashref may also be passed in which is used for additional arguments; as of this writing, the only available argument is
expires
. Ifexpires
is greater than zero and less than the currenttime
then the template block is re-evaluated. In this case the return value is the memorized template result.When called without arguments, the plugin object is returned, allowing the use of other plugin methods or access to the plugin's cache.
ATTRIBUTES
cache
-
A hash reference containing the memorized template content and other data.
METHODS
expire( $name )
-
This method allows for manually expiring a memorized template block. This may useful if the template is set to never expire or when the underlying content is known to have changed.
This is an example of the utility of having access to the underlying hash. In the original implementation of the core helper, this access was not available.
register
-
This method is called upon loading the plugin and probably is not useful for other purposes.
SEE ALSO
SOURCE REPOSITORY
http://github.com/jberger/Mojolicious-Plugin-Memorize
AUTHORS
COPYRIGHT AND LICENSE
Copyright (C) 2013 by Joel Berger and Sebastian Riedel
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.