Vetinari's $HOME


MojoX::Renderer::Cache - Cache rendered output in Mojo


Add the handler:

  use MojoX::Renderer::TT;
  use MojoX::Renderer::Cache;
  use Cache::File;
  sub startup {
     my $tt = MojoX::Renderer::TT->build(
            mojo => $self,
            template_options =>
             { PROCESS => 'tpl/wrapper',
               FILTERS => [ foo => [ \&filter_factory, 1]

    my $cache = MojoX::Renderer::Cache->build(
            renderer     => $tt,
            key          => [qw(template_path article)],
            cache        => Cache::File->new(
                                cache_root      => '/tmp',
                                default_expires => '5 minutes',
    $self->renderer->add_handler( tt => $cache );

And then in the handler call render which will call the MojoX::Renderer::Cache renderer. This will either call the given renderer or use a cached version of the output:

   $c->render(foo => 123, bar => [qw(giz mo)]);


uncached (1|0)

You can force rendering by setting the stash variable uncached to a true value (this will also not add the rendered page to the cache).

cache_expires VALUE

By setting the stash variable cache_expires, you can change the default expire time of the cache object. Setting this, will write to the cache, no matter if uncached (see above) was true or not. See Cache on what vaild values for VALUE are.



This method returns a handler for the Mojo renderer.

Supported parameters are


This must be a valid Mojo renderer, which will be used to render the output if a page is not in the cache. No defaults, this is a required option.


This must be a Cache::* object which uses the same API as Cache / Cache::File (most important, it requires a get() and a set() method which act like Cache::*). No defaults, this is a required option.


A list of stash variables which will be used to construct a unique key for the given page. Default is qw{ template_path }


Cache, Cache::File, Cache::Memory, MojoX::Renderer


Hanno Hecker <>


Copyright 2009 Hanno Hecker, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.