Hi All,

I'm developing an application that displays family history data from a
GEDCOM file [1]. Generally, loading and looking through these files is
an expensive set of operations, so I decided it would be a good idea to
cache the output.

The caching mechanism (which uses Cache::FileCache) is contained in the
cgiapp_prerun sub and looks like so:

sub cgiapp_prerun {
        my $self    = shift;
        my $mode    = shift;

        return if $mode eq 'AUTOLOAD';

        my $cache   = $self->param( 'cache' );
        my $version = $self->param( 'version' );
        my $page    = &{ $modes->{ $mode } }( $self );

        my $data = $cache->get( $page . '::' . $version );

        # If the data hasn't been cached, execute
        # the runmode and cache the results
        unless ( defined( $data ) ) {
                $data = $self->$mode;
                $cache->set( $page . '::' . $version, $data ) if $data
=~ /\S+/;
        }

        $self->teardown;
        print $self->_send_headers, $data;

        exit( 0 );
}

---
Explanation:
The first part of this sub checks to see if we've hit the AUTOLOAD mode,
which turns out to be the only mode I will never want to cache (in this
app, atleast).

The next 3 assignments get the cache object, the version of the GEDCOM
file (its timestamp in reality) and a page key (the $modes hashref is
just a bunch of anonymous subs that return a key based on CGI data. This
could probably be based on the URL (md5 hash of most likely) just as
easily) to attach to the version to see if it exists in the cache.

If it's not in the cache, execute the runmode and cache it as long as
it's not blank (this avoids breaking modes that might do a redirect).

Send the data to the browser.
---

Any thoughts on this process? Are there better ways or "best practices"?

-Brian Cassidy

[1] http://search.cpan.org/dist/Gedcom/


http://www.gordano.com - Messaging for educators.

---------------------------------------------------------------------
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