[Catalyst] Partial page cache plugin

Octavian Râsnita orasnita at gmail.com
Sun Dec 6 10:56:09 GMT 2009


From: "Julien Sobrier" <julien at sobrier.net>
> Hello,
> there is a plugin for caching an entire page.  I am looking for the same
> type of plugin to cache part of the page, but did not find one. Waht is 
> the
> best way to achieve this in Catalyst?

You can use Catalyst::Plugin::Cache. You can do:

use Digest::SHA1 'sha1_hex';
use base 'Catalyst::Controller';

sub foo : Local {
  my ($self, $c) = @_;

my $name = $c->req->params->{name};
my $age = $c->req->params->{age};
my $page = $c->req->params->{page};

  my $hash = sha1_hex('foo', $name, $age, $page);
  my $cache = $c->cache;
  my $page_part = $cache->get($hash);

unless ($page_part) {
#Here make the selection from the database, get the data from somewhere 
else...
my $object = $c->model("DB::Table")->search({
name => {-like => "%$name%"},
age => $age,
},{
order_by => 'name',
rows => 20,
page => $page,
});

$c->stash(obj => $obj);
$page_part = $c->view('TT')->render($c, 
'template/for/that/part/of/the/page.tt');

$cache->set($hash, $page_part, 600);
}

$c->stash(page_part => $page_part);
}

This way it works fine, although if someone has an easier and/or nicer 
solution, please tell us.

PageCache has some conflicts with another plugin, if I remember well is 
Catalyst::Plugin::Unicode.

Template::Plugin::Cache that can be used for page parts caching very easy 
doesn't work well with templates that contain special UTF-8 chars (I was 
informed that this is because the FileCache module and I heard that it could 
work with a CHI object).

Anyway, if somebody has some recommendations for speed improvements with 
Catalyst, please tell us if there are (I have checked a PHP page with many 
dynamic types of data that have a 44 requests/second, but I wasn't able to 
create a much simpler page with Catalyst that displays so fast).
Yes, I know, the PHP page is low level, harder to create and to maintain, 
gives bad errors, but I can't convince that Catalyst is good if it is not 
fast enough, so any tips for using caches or other ways of improving the 
speed are welcome.

Thank you.

Octavian




More information about the Catalyst mailing list