Anyone miss Mojolicious' memorize helper?

Mojolicious’s 4.0 release came with lots of shiny features but it also came with a lot of housecleaning. One of the old things swept away was the memorize helper, which would cache a part of a template and prevent its repeated evaluation. Do you miss it, as some users undoubtedly do, or else does this helper sound useful to you? Then read on, because its back and better than ever!

Galileo, my CPAN-friendly minimal CMS, used the memorize for caching the menu part of its templates. It did so until I needed a certain feature added, namely the ability to expire a template immediately rather than after a certain time. After asking sri, he informed me that he didn’t think that memorize was a useful core functionality, and he didn’t think it would remain, I forked the module and it has happily existed as part of the Galileo distribution with that one additional feature.

I should note that I agreed with the removal of the memorize helper from the Mojolicious core. One of the guiding ideas of Mojolicious development is that core functionality should be useful to the core itself or else be of essential value to the user. The memorize helper was only really included as an example of helpers (of which there are now a multitude), and it was also difficult to effectively test without causing some slowdown. Focusing on a lean core helps to achieve a secondary goal of keeping Mojolicious fast and easy to install.

Since 4.0 sent memorize off to the land of forgotten code, I have meant to get around to spinning my version out from Galileo and make it available for others to use. Finally this last weekend I have gotten around to doing so, as Mojolicious::Plugin::Memorize. The release adds some other cute features, like access to the underlying cache and using a steady clock to be resilient against time jumps, in addition to being a drop-in replacement for the old plugin. I don’t know how many users were scared away from upgrading to 4.0 because of the removal of memorize, but if you happened to be one, here is your chance!

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

Leave a comment

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.