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.

-stvn

In reply to Re: [mod_perl] when does it keep data in memory? by stvn
in thread [mod_perl] when does it keep data in memory? by Jaap

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.