mod_perl and global variables during startup

Discussion in 'Perl Misc' started by nicc777, Jul 12, 2005.

  1. nicc777

    nicc777 Guest

    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
     
    nicc777, Jul 12, 2005
    #1
    1. Advertising

  2. nicc777

    Keith Keller Guest

    On 2005-07-12, nicc777 <> wrote:
    >
    > 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

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
     
    Keith Keller, Jul 12, 2005
    #2
    1. Advertising

  3. On Tue, 12 Jul 2005 12:14:16 -0700, Keith Keller wrote:

    > On 2005-07-12, nicc777 <> wrote:
    >>
    >> 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.


    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
     
    Richard Gration, Jul 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian douglas
    Replies:
    0
    Views:
    1,844
    ian douglas
    Aug 19, 2003
  2. Mikkel Heisterberg
    Replies:
    0
    Views:
    2,124
    Mikkel Heisterberg
    Jul 1, 2003
  3. news.internetia.pl
    Replies:
    2
    Views:
    569
    tricky
    Jan 12, 2008
  4. Gunnar Hjalmarsson

    Global variables in mod_perl

    Gunnar Hjalmarsson, May 17, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    237
    Gunnar Hjalmarsson
    May 18, 2004
  5. Replies:
    2
    Views:
    472
    Big and Blue
    Jan 25, 2006
Loading...

Share This Page