mod_perl and global variables during startup

N

nicc777

Hi,

I am not exactly new to Perl, but very new to mod_perl.

In short, here is what I want to achieve:

1. Have two global hashes loaded during startup (maybe in startup.pl
??). The values are obtained from a DB

2. From time to time send a special HTTP request to the server to
refresh the hashes from a DB source

In theory, this should prevent me from going to the DB for info that
changes rarely, and when they do change I could have a mechanism (my
idea is around a special HTTP request) to reload the hashes if the data
do change.

Any ideas? Code samples will be most welcome.

Thanks
 
K

Keith Keller

In short, here is what I want to achieve:

1. Have two global hashes loaded during startup (maybe in startup.pl
??). The values are obtained from a DB

2. From time to time send a special HTTP request to the server to
refresh the hashes from a DB source

You could run into problems with 2. On some (all?) systems, each http
process has its own variable space, and you have no way of specifying
with an HTTP request which httpd process you get. So, if you send five
requests, for example, and the server has 10 httpd's running, you might
hit half of the httpd's and reload their variables, or you might hit the
same httpd 5 times.
In theory, this should prevent me from going to the DB for info that
changes rarely, and when they do change I could have a mechanism (my
idea is around a special HTTP request) to reload the hashes if the data
do change.

What is the reason you don't want to hit the db? In any case, you might
use a file instead, and touch the file (or update its contents with the
new data, even); then, a CGI can look at the file to see if it needs to
hit the database (or even look at the file to get the necessary
changes).

In general, trying to tell a mod_perl httpd to change something is
nontrivial, and you should try to put such logic in your script (or
mod_perl library) instead, and do as little as possible in startup.pl.

Finally, you might ask for strategies on the mod_perl mailing list; many
more mod_perl experts frequent there than here (and all much more expert
than I). Look at perl.apache.org for mailing list info.

--keith
 
R

Richard Gration

You could run into problems with 2. On some (all?) systems, each http
process has its own variable space, and you have no way of specifying
with an HTTP request which httpd process you get. So, if you send five
requests, for example, and the server has 10 httpd's running, you might
hit half of the httpd's and reload their variables, or you might hit the
same httpd 5 times.

I have exactly the same global hash situation as you, except I have only
one, not 2 ;-) It is initialised from the database at server startup time.
The Unix epoch time at which this happens is stored in the hash, under the
key reconfig. I also have a field called reconfig on the cfg_main database
table.

For every request, I check to see if the reconfig time in the db is more
recent than the one stored in the config hash. If I need to reread the
config, I do, which updates the reconfig value in the hash too.

This totally gets round the multiple children issue, they all re-read as
necessary. This system has worked well for me for years.

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top