On Thu, 2 Nov 2000, Neil Conway wrote:

> Date: Thu, 2 Nov 2000 16:45:28 -0500
> From: Neil Conway <[EMAIL PROTECTED]>
> To: ModPerl List <[EMAIL PROTECTED]>
> Subject: dynamic vs. mostly static data
> 
> I'm writing a web app in mod_perl, using a PostgreSQL database
> backend and HTML::Template. In looking for ways to optimize
> performance, I noticed that although my code is doing several
> (say, 4-5) database queries per handler/webpage, a large part
> of the data (~2 queries) is mostly static (it will change
> perhaps once per week, or once per month). It's obviously
> inefficient to run these queries on the database for every
> single request.
> 
> How can I 'cache' this data so that all Apache children can
> access it quickly? Is there a way to automatically update
> this cache periodically (say every 10 minutes)? Also, this
> solution should work on any reasonably modern UNIX system
> (Win32 is not important for now).
> 
> I couldn't find this anywhere, but if someone tells me where,
> I'd be happy to RTFM. Ask me if you need more info.
> 
> TIA,
> 
> Neil

neil,

i often do something like this where i allow each individual child
process to cache it's data.  i do something like this:

package Apache::Foo;

use strict;
use Apache::Constants;
use POSIX 'strftime';

use constant CACHE_EXPIRES => 3600; # one hour
use vars qw[ %CACHE ];
%CACHE = ();
    
sub handler {
    my $r = shift;

    eval {
        my $expires = $CACHE{'expires'} || 0; 
        if ($expires < time) {
            my @data = < some routine >;
            my $t = HTML::Template->new(filename          => 'foo.tmpl',
                                        die_on_bad_params => 0,
                                        cache             => 1);  
            $t->param('data', \@data);

            $CACHE{'data'}    = $t->output;
            $CACHE{'expires'} = time + CACHE_EXPIRES;
        }
        $r->print($CACHE{'data'});
    };

    return print_err($r, $@) if $@;
    return OK;
}

1;

btw, i'd really recommend you look into using Template Toolkit.  it's a
much more powerful and flexible templating system than HTML::Template,
but i digress (and might start a flame war against myself by saying this).

hth,

ky

Reply via email to