Shared memory usage in mod_perl environment
I have a requirement where
-
I need to place a data structure (Perl hash) in memory so that every HTTP process (by running a Perl script) will use this hash.
-
The hash structure is around 300MB.
-
Wednesday - mod_perl
I was thinking about creating a module to load on Apache startup that creates a hash in the shared area and returns a link to it.
Can you comment on the behavior or suggest alternative solutions. Also, please point to some helpful resources to check examples.
source to share
If you put a huge hash in mod_perl's memory, then the parent process mod_perl reads it at server startup.
First, you create a directory Your/HugeData.pm
in @INC
.
package Your::HugeData;
our %dictionary = (
....
);
Next, the apache process reads this at startup.
# In apache.conf (or anywhere apache config file)
PerlModule Your::HugeData
Then your script can be used %Your::HugeData::dictionary
as a package variable.
# In mod_perl handler script or ModPerl::Registry (CGI emulate) script.
use Your::HugeData;
...
my $tokyo = $Your::HugeData::dictionary{tokyo};
When you use prefork MPM on Linux Apache, the OS prefers the Copy to Write mechanism. This way, forked child processes see the data of the parent processes if you are only reading the data. In other words, memory may not deteriorate.
source to share