Peter1 Alvin  <[EMAIL PROTECTED]> wrote:

> Please tell me I can do this!
>  
> Using mod_perl, how do you keep Perl objects in RAM from page to page? 
> I don't want to re-instantiate my objects on every page request, and I
> don't want the overhead of serializing my objects to a persistent store
> from page to page (I use A LOT of objects).
>  
> I've done extensive reseach and I've not found any "application space"
> to store objects indefinitly.
>  
> I can't believe Perl doesn't support keeping objects in RAM.  This would
> de-qualify Perl for half of the projects I need to develop.

If you stick something into a global variable it will stay around from one
request to another, as long as the server stays running.  But since you
can't guarantee that the next request from a particular browser will come
to the same server, if you're hoping to keep "session" information this
way, don't bother.

But if you just want to cache the result of some expensive operation, and
the result won't be changing from one request to the next, you can either
use a global variable, or (better) a lexical variable that never goes out
of scope.

As an example, I have a handler that needs to read a directory listing.
Most of the time it will be looking at the same directory over and over,
so I decide that it's okay to have to restart the server if the listing
will change (the listing won't change much) and do something like:

{ # start a lexical scope
my %d_cache;
sub handler {
    # stuff
    if (not defined($d_cache{$foo})) {
        # pull in directory listing and store it in $d_cache{$foo}
    }
    # proceed to use $d_cache{$foo} information
}
} end lexical scope

This way, a directory is only actually read once (at most) per server
child process.

Note: I'm no expert in threaded programming, but the above doesn't look
thread-safe to me.  I use the pre-fork MPM.

-- 
Jeremy  |  [EMAIL PROTECTED]

Reply via email to