You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Octavian Rasnita <or...@fcc.ro> on 2005/08/26 16:04:16 UTC

memory resident config hash

Hi,

I have a config file which contains a hash ref and I get its values by using
the do() function.

I don't think this is the best solution, because the speed might be slow.
That file is accessed on each access to each page.

Is it possible to store that hash in the memory somehow?

I can store that hash at the server startup or after that... doesn't matter.

Thank you.


Teddy



Re: memory resident config hash

Posted by Octavian Rasnita <or...@fcc.ro>.
Ok, thank you. That example from perlref is what I was searching for, but it
seems that  it can't do what I want. (I didn't know that programs that use
'require' don't share the memory).

I will try to use Config::Simple.

I need to store a few hashrefs in the memory.

Thank you very much.

Teddy

From: "Michael Peters" <mp...@plusthree.com>
> If these are just .ini files, then why don't you use something like
> Config::Simple to create a object from which to read the data. You could
> then create a wrapper class that would give access to this object as a
> singleton. Load the class on server startup and initialize the object
> there too. As long as you don't change the data it should be stay shared
>  by all of your apache children.
>

> Once you do a 'require' in a process the data is no longer shared
> between processes. Another request that needs the same configuration
> data that is handled by a different apache process will need to do the
> same 'require' thus increasing it's unshared memory, etc.
>
> Sometimes this is necessary, but it should be avoided if possible. How
> many of these language specific configs do you have? Do you know which
> ones are more likely to be used? If you do then preload as many as
> possible ahead of time.


Re: memory resident config hash

Posted by Michael Peters <mp...@plusthree.com>.

Octavian Rasnita wrote:
> Thank you, I have done so, but I still have a problem.
> 
> I used to run those config files using do() for getting the data based on a
> variable, for example:
> 
> my $ref = do("$language.ini");
> 
> And depending on the value of the $language variable, it is launched a
> different .ini file.

If these are just .ini files, then why don't you use something like
Config::Simple to create a object from which to read the data. You could
then create a wrapper class that would give access to this object as a
singleton. Load the class on server startup and initialize the object
there too. As long as you don't change the data it should be stay shared
 by all of your apache children.

> I don't know how to get the variable from a module if I know the name of
> that module only at runtime.
> 
> I have tried:
> 
> my $module = "Teddy::ModuleName";
> eval "require $module";

Once you do a 'require' in a process the data is no longer shared
between processes. Another request that needs the same configuration
data that is handled by a different apache process will need to do the
same 'require' thus increasing it's unshared memory, etc.

Sometimes this is necessary, but it should be avoided if possible. How
many of these language specific configs do you have? Do you know which
ones are more likely to be used? If you do then preload as many as
possible ahead of time.

> But now I don't know how to get the value of $content variable from
> Test::ModuleName.
> 
> I know that I can get it using $Teddy::ModuleName::content, but I know the
> name of that module only at runtime.
> 
> I have tried $module::content, but it doesn't work.
> 
> Please advice how I can do that.

I was trying to get this to work with a hash as the package data, but
for some reason it didn't work. But perlref show how to do it with a
hashref.

my $DATA = $MyConfig::DATA;
my $package = $DATA->{'lang_config'};
eval "require $package";
my $LANG_DATA;

{
    no strict 'refs';
    $LANG_DATA = ${"${package}::DATA"};
}


==========MyConfig=============
package MyConfig;

our $DATA = {
    other => 'MyConfig::Other',
};

1;

=========MyConfig::Other=======
package MyConfig::Other;

our $DATA = {
    foo => 'more stuff',
};


-- 
Michael Peters
Developer
Plus Three, LP


Re: memory resident config hash

Posted by Octavian Rasnita <or...@fcc.ro>.
Thank you, I have done so, but I still have a problem.

I used to run those config files using do() for getting the data based on a
variable, for example:

my $ref = do("$language.ini");

And depending on the value of the $language variable, it is launched a
different .ini file.

I don't know how to get the variable from a module if I know the name of
that module only at runtime.

I have tried:

my $module = "Teddy::ModuleName";
eval "require $module";

But now I don't know how to get the value of $content variable from
Test::ModuleName.

I know that I can get it using $Teddy::ModuleName::content, but I know the
name of that module only at runtime.

I have tried $module::content, but it doesn't work.

Please advice how I can do that.

Thank you.

From: "Michael Peters" <mp...@plusthree.com>
> Just put it into it's own package and preload it. As long as you don't
> change the data it'll stay shared.
>
> package MyConfig;
>
> our %DATA = (
> ...
> );
>
> 1;
>
> Then reference it anywhere as MyConfig::DATA{foo};
>
> HTH
> -- 
> Michael Peters
> Developer
> Plus Three, LP
>


Re: memory resident config hash

Posted by Michael Peters <mp...@plusthree.com>.

Octavian Rasnita wrote:
> Hi,
> 
> I have a config file which contains a hash ref and I get its values by using
> the do() function.
> 
> I don't think this is the best solution, because the speed might be slow.
> That file is accessed on each access to each page.
> 
> Is it possible to store that hash in the memory somehow?

Just put it into it's own package and preload it. As long as you don't
change the data it'll stay shared.

package MyConfig;

our %DATA = (
...
);

1;

Then reference it anywhere as MyConfig::DATA{foo};

HTH
-- 
Michael Peters
Developer
Plus Three, LP