Snippets

Brian Medley Attempt at rudimentary render cache.. probably broken in various obvious and non-obvious ways

Created by Brian Medley
#!/opt/perl

use Mojolicious::Lite;

helper cache => sub {
    my $c = shift;

    state $cache = {};

    my $renderer = $c->app->renderer;

    my $tmpl = $renderer->template_for($c);

    # What about params?
    if (!exists $cache->{$tmpl}) {
        my ($output, $format) = $renderer->render($c, {
            template => $tmpl
        });

        $cache->{$tmpl} = $output;
    }

    return $cache->{$tmpl};
};

get '/' => sub {
    my $c = shift;

    $c->render(text => $c->cache, format => 'html');
} => "slash";

app->start;

__DATA__
 
@@ slash.html.ep
Hello, world.<br>
<%= scalar(localtime) %>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.