Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Storing data with mod_perl (are those handlers different) ?

by lindex (Friar)
on Apr 08, 2001 at 16:14 UTC ( #70823=perlquestion: print w/ replies, xml ) Need Help??
lindex has asked for the wisdom of the Perl Monks concerning the following question:

Ok so the situation is this:

I have a simple perl script that does processes on files and is run from cron, this script reads a configuration file passed to it from command line.

#!/usr/bin/perl -w use strict; use vars qw($file); $file = $ARGV[0]; open(CONF,$file) || die("$!: $file"); while(<CONF>) { ... code for parsing and storing config ... } close(CONF); ... more code for doing other stuff ...

I have yet another perl program, however it is not a script it is a package used as a handler for a mod_perl enabled apache web server with an apache configuration directive such as this ...

PerlModule My::Package::Foo; <Location /foohandler> SetHandler perl-script PerlHandler My::Package::Foo; PerlSetVar FooConfig /some/patch/config.cfg </Location>

Now as you can see there is a PerlSetVar that is the path of a config file, this would be the same config file that the perl script running from cron uses. It is very important that these they read the same config.

So its simple enough for me to use the same config file parser in the handler as I am inside of the perl script running from cron.

package My::Package::Foo; sub handler { my($r) = shift; my($file) = $r->dir_config('FooConfig'); open(CONF,$file) || die("$!: $file"); while(<CONF>) { ... same code for parsing stuff as before ... } close(CONF); ... more code for doing other stuff ... }

Ah! but it would much faster if I cached the data in the config file inside of apache, instead of re-reading/parsing that config file for every request.

So question one is... what is the best method of caching data inside of apache like so?

And question two is... if I define another location directive that has the same handler would these two handler's share that cached data or would they be in seperate memory space inside of apache, and if they are in the same space whats the best way to seperate them?

ever delusional

-lindex
I know I should rtfm :)




lindex
/****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/

Comment on Storing data with mod_perl (are those handlers different) ?
Select or Download Code
Re: Storing data with mod_perl (are those handlers different) ?
by lachoy (Parson) on Apr 08, 2001 at 20:53 UTC

    If it's a relatively simple configuration file/info, you can just store the data structure in a package variable. It won't be shared among all the Apache children (each will generally have its own copy) but if you're not making changes to the file that's not a big deal. And I think depending on how your OS implements shared memory the memory might be shared among the children until you make a change. (Check out the mod_perl Guide for lots of info on shared memory.

    One for something like this is to read in the file and set the config info in a ChildInit handler. To do this, define your routine from above in your startup.pl startup file and then add the routine into the ChildInit handler stack. That way you'll always know it's there.

    The downside of this is that you can't do dir_config calls within your startup.pl file because you're not in a directory when Apache is starting up. You can, however, read server configuration variables and work from there. For instance, if you have five different configuration files you could map a configuration name to a file location, read them in at startup and store them in a My::App::Config package, then call the specific configuration from other handlers depending on the configuration name set in a PerlSetVar location directive.

    For something like this you might also look into the Singleton pattern (implemented by Class::Singleton) so you can refer to the configuration from any other handler with a simple call like My::App::Config->instance.

    As for your second question, the two would be in the same memory/name space. If you want you can separate them by setting an additional PerlSetVar in the two different location directives that specifies which configuration (or whatever) you're using.

    Hope this helps.

    Chris
    M-x auto-bs-mode

      Actually, if you read the config file during the execution of a "PerlRequire" or "PerlModule" (or <Perl> ?) directive, then it will be stored in the parent process and hence *will* be shared amongst the child processes.

      That's also your chance to do any setting up that you need to do as root, e.g. creating new directories and "chown"ing them.

      So you could have a read_config.pl in /etc/apache (or wherever) that simply reads the config data and drops it into your handler's package. Then just PerlRequire read_config.pl and there you go!

      The disadvantage with doing this, of course, is that the configuration file is not reread simply by doing apachectl graceful. Which may not be what you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://70823]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-07-14 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (256 votes), past polls