rajaman has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks,

I am trying to figure out how I can do server-side caching of dynamic webpages that Mojolicious generates in response to webrequests. So that, expensive processing (e.g. on data pulled from a database) done on regenerating the same webpages again is bypassed for subsequent webrequests.

I was looking for a file-based caching solution for Mojolicious, something like what this module does, but unfortunately the module seems outdated and does not install: Mojolicious::Plugin::Cache. In CGI, such a solution is implemented in CGI::Buffer, and Cache::Cache.

I found caching support in Mojolicious, but this appears to be client-side caching, as shown below:

helper 'cache_control.five_minutes' => sub ($c) { $c->res->headers->ca +che_control('public, max-age=300') }; get '/some_older_story' => sub ($c) { $c->cache_control->five_minutes; $c->render(text => 'This one can be cached for a bit.'); }; #ref: https://metacpan.org/pod/distribution/Mojolicious/lib/Mojoliciou +s/Guides/Rendering.pod

I also tried to use statements as below, but this does not seem to work for me, maybe I am missing something here.

use Mojolicious::Lite app->renderer->cache->max_keys(100);

Please advise how to go about.

Thank you very much!

Replies are listed 'Best First'.
Re: Server-side caching of webpages in Mojolicious
by Anonymous Monk on Dec 19, 2020 at 00:17 UTC

    but unfortunately the module seems outdated and does not install: Mojolicious::Plugin::Cache. In CGI, such a solution is implemented in CGI::Buffer, and Cache::Cache.

    Hello

    Your question also contains the answer ;)

    Mojo has https://metacpan.org/pod/Mojolicious::Plugin::CHI but it doesn't have the same sugar as Dancer::Plugin::Cache::CHI

    get '/mods/perltidy/:modname' => sub { my $ret = src2perltidy( params ) ; redirect "/mods" if not defined $ret or not length $ret; cache_page $ret; };