Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

mod_perl memory question

by avo (Pilgrim)
on Mar 05, 2008 at 14:54 UTC ( #672185=perlquestion: print w/replies, xml ) Need Help??
avo has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have being designing a module in Perl which is using a private hash to store some data when certain method is called. It looks like this:
package sharedcache; use strict; my $cache; sub store { $cache .= $_[0]; } sub get { return $cache; } 1;
In mod_perl environment the data stored in $cache doesn't die after execution of the mod_perl script. The data stays in $cache until a web server restart. The issue I have is that because Apache forks couple of processes each process carry its own copy of this module and $cache. I need fast temporary storage for better response time towards the client. In my application I store the data in $cache until certain moment when the client confirms the data, when I store it in MySQL table. I also tried using tied db like this:
tie %tempdb, 'SDBM_File', $databasefile, O_CREAT|O_RDWR, 0644;
Which had exactly the same issue - each apache process kept its own copy of it. Is there a better way to do that. Thanks.

Replies are listed 'Best First'.
Re: mod_perl memory question
by moritz (Cardinal) on Mar 05, 2008 at 14:59 UTC
Re: mod_perl memory question
by perrin (Chancellor) on Mar 05, 2008 at 16:20 UTC
    Both MySQL and SDBM_File should work fine for this. MySQL is very fast at this kind of simple data access. If you'd rather use a dbm file, try using SDBM_File through the MLDBM::Sync module, which handles the locking.

    If this is really a cache, not a database, and you don't care if you might lose the data, then memcached is ok too, but if you only have one machine (not a cluster) and don't mind the possibility of data loss, Cache::FastMmap is the fastest thing around.

    You might also look at CHI for a high-level interface that lets you try various caching modules.

Re: mod_perl memory question
by Rhandom (Curate) on Mar 05, 2008 at 15:46 UTC
    If the cache needs to be shared among children - I second the vote for memcache.

    If each child needs its own, I'd do something like the following using the End module:

    # add clear_cache method to your sharedcache package sharedcache; sub clear_cache { $cache = undef } # inside your apache handler use the following: use End; sub handler { my $clear_cache = end { sharedcache::clear_cache() }; # normal handler operations go here }

    When your handler goes out of scope - the object stored in $clear_cache will go out of scope and the stored coderef will fire - clearing your cache.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://672185]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-08-16 02:46 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (166 votes). Check out past polls.