I'm working on a site that does a lot of mysql hits, and I want to cache pages for an hour. Here is a quick summary of how I'm doing it, in a base class for all CGI::Application modules. I will add some kind of package or method regex when I get to the pages that I don't want cached. Is this the best approach, or is there a better way? This might run on a shared host as CGIs for now, so I'm concerned about performance, but I'm trying to keep things relatively simple.

In the examples below, $cache is created in the base class with:

my $cache = new Cache::FileCache( { 'namespace' => 'mynamespace',
'default_expires_in' => '1 hour' } );



In cgiapp_prerun, check for cache and switch mode if found

sub cgiapp_prerun {
    my ($self, $rm) = @_;
    my $cache_key = ref($self) . "-$rm";
    $self->param(cache_key => $cache_key);

    my $output = $cache->get($cache_key);
    if ($output) {
        $self->param(cached_output => \$output);
        $self->prerun_mode('output_cached');
    }
}

---

In cgiapp_postrun, cache output using the cache_key set above

sub cgiapp_postrun {
    my $self = shift;
    my $output_ref = shift;
    my $rm = $self->get_current_runmode();
    $cache->set($self->param('cache_key'), $$output_ref);
}

---

output_cached is in the base class too -

sub output_cached {
    my $self = shift;
    warn "output_cached called";
    return $self->param('cached_output');
}





--
Barry Hoggard
Tristan Media LLC
w: www.tristanmedia.com
yahoo/aim: hoggardb


--------------------------------------------------------------------- Web Archive: http://www.mail-archive.com/[email protected]/ http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2 To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to