Well I don't know the details of your set up, but this is how I have shared large datastructures across mod_perl/Apache processes in the past.
First, you need to have a place for the data-structure, as someone already mentioned, a package global is the way to go. Its really just this simple:
package My::Big::Data::Structure;
our $BigDataStructure = ...
Then assuming that you use My::Big::Data::Structure at the top of your registry script, you should be able to access your global like this;
# make a local reference
# to it to save typing
my $big_data_struct = $My::Big::Data::Structure::BigDataStructure;
Next, you need to as your host to allow you to have a startup.pl file. This is a file which Apache will load upon server start-up. The Apache conf should have this in it
PerlRequire "path/to/startup.pl"
It is really just a perl file, nothing much special. And in it you can initialize your big-data-structure in it like this
use My::Big::Data::Structure;
$My::Big::Data::Structure::BigDataStructure = build_big_data_strucure(
+);
Of course the better way might be to encapsulate the big data structure into the My::Big::Data::Structure module, but that is another question.
You do need to be aware of a few things though. First, the memory this data-structure occupies will be "shared" between all the apache child processes. Which means as long as you don't change the data-structure (and I am assuming you aren't) there will only ever need to be a single copy of this data. If however, you change the data-structure, know that the underlying OS will perform a copy-on-write and then that Apache child process, and ONLY that Apache child process will get a new copy of the data with the change applied. It is important to note that no other Apache child process will see the change in the data-structure nor will the parent process from which it was copied. Basically, you don't want to change that data-structure or you're in for a world of trouble.
You also should know that your Apache server will take an extra 10 seconds to startup, since your big-data-structure is not getting initialized at server startup. Be sure to tell your host about this, or they may think something is wrong next time they go to restart your server and it seems to hang.
|