You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Simon Perreault <no...@lqt.ca> on 2005/07/22 16:19:17 UTC

Request-local storage?

Hi,

Is there a good way to have request-local storage? For example, I want to 
cache stuff (file reading primarily) for the length of a given request, and 
have that cache be local to the request (ie. other concurrent requests have 
separate caches). How would you do that? It should not matter wether Apache 
is threading or forking.

Thanks for any advice!

Re: Request-local storage?

Posted by Stas Bekman <st...@stason.org>.
Simon Perreault wrote:
> On Friday 22 July 2005 11:00, Stas Bekman wrote:
> 
>>But you need this functionality only under mod_perl, right?
> 
> 
> Well, not really. I would still need to cache stuff during the request when 
> running under mod_cgi but then the cache would be automatically reset at the 
> end of each script. Nothing special needs to be done. Under mod_perl, the 
> cache needs to be explicitly reset.
> 
> 
>>if ($ENV{MOD_PERL}) {
>>     require Apache2::RequestUtil;
>>     my $r = Apache2::RequestUtil->request;
>>     $r->pnotes(...);
>>}
> 
> 
> Very nice! I'll figure out the "else" part by myself. Thank you!

the else you could just stash it in the global variable, since mod_cgi 
always "forgets" everything.


-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Re: Request-local storage?

Posted by Simon Perreault <no...@lqt.ca>.
On Friday 22 July 2005 11:00, Stas Bekman wrote:
> But you need this functionality only under mod_perl, right?

Well, not really. I would still need to cache stuff during the request when 
running under mod_cgi but then the cache would be automatically reset at the 
end of each script. Nothing special needs to be done. Under mod_perl, the 
cache needs to be explicitly reset.

> if ($ENV{MOD_PERL}) {
>      require Apache2::RequestUtil;
>      my $r = Apache2::RequestUtil->request;
>      $r->pnotes(...);
> }

Very nice! I'll figure out the "else" part by myself. Thank you!

Re: Request-local storage?

Posted by Stas Bekman <st...@stason.org>.
Simon Perreault wrote:
> On Friday 22 July 2005 10:27, Stas Bekman wrote:
> 
>>$r->notes for strings
>>$r->pnotes for perl scalars
> 
> 
> Thank you very much for your quick reply. I didn't know about those methods.
> 
> I should have mentioned that I am running under ModPerl::Registry and am 
> trying to keep my scripts free from any mod_perlism so that they can still be 
> run stand-alone. Is there still a solution?

But you need this functionality only under mod_perl, right? in which case 
you can do something like:

# mp1.x

if ($ENV{MOD_PERL}) {
     my $r = Apache->request;
     $r->pnotes(...);
}

# mp2.x

if ($ENV{MOD_PERL}) {
     require Apache2::RequestUtil;
     my $r = Apache2::RequestUtil->request;
     $r->pnotes(...);
}





I'm not quite following you. You mean you need a solution that will work 
with mod_cgi too? or do you need




-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Re: Request-local storage?

Posted by Simon Perreault <no...@lqt.ca>.
On Friday 22 July 2005 11:08, Joe Cullin wrote:
> 1. Use $ENV{'UNIQUE_ID'}  (set my mod_unique_id?)
> This seems to be a different value for every request.  So I can create 2
> global variables, $lastId and %savedData.  If $lastId != $ENV{'UNIQUE_ID'}
> then I know I'm in a new request and I can wipe out %savedData.

Very nice. Using this solution I don't even need to test whether I'm running 
under mod_perl or not.

> 2. Use a global variable and clean it up at the end of each request using
> "cleanup_register" from APR::Pool.  This is within an "if ($mod_perl)"
> block, so it won't affect the mod_cgi script.

Oooooooh! That's cool! I now have many more solution options than I was asking 
for, I'll have the burden of choosing one among them.

Thanks a lot!

Re: Request-local storage?

Posted by Joe Cullin <jc...@unipress.com>.
We're in a similar situation.  I've found two ways of doing this.

1. Use $ENV{'UNIQUE_ID'}  (set my mod_unique_id?)
This seems to be a different value for every request.  So I can create 2
global variables, $lastId and %savedData.  If $lastId != $ENV{'UNIQUE_ID'}
then I know I'm in a new request and I can wipe out %savedData.

2. Use a global variable and clean it up at the end of each request using
"cleanup_register" from APR::Pool.  This is within an "if ($mod_perl)"
block, so it won't affect the mod_cgi script.

-Joe


On Fri, 22 Jul 2005, Simon Perreault wrote:

> On Friday 22 July 2005 10:27, Stas Bekman wrote:
> > $r->notes for strings
> > $r->pnotes for perl scalars
> 
> Thank you very much for your quick reply. I didn't know about those methods.
> 
> I should have mentioned that I am running under ModPerl::Registry and am 
> trying to keep my scripts free from any mod_perlism so that they can still be 
> run stand-alone. Is there still a solution?
> 



Re: Request-local storage?

Posted by Simon Perreault <no...@lqt.ca>.
On Friday 22 July 2005 10:27, Stas Bekman wrote:
> $r->notes for strings
> $r->pnotes for perl scalars

Thank you very much for your quick reply. I didn't know about those methods.

I should have mentioned that I am running under ModPerl::Registry and am 
trying to keep my scripts free from any mod_perlism so that they can still be 
run stand-alone. Is there still a solution?

Re: Request-local storage?

Posted by Stas Bekman <st...@stason.org>.
Simon Perreault wrote:
> Hi,
> 
> Is there a good way to have request-local storage? For example, I want to 
> cache stuff (file reading primarily) for the length of a given request, and 
> have that cache be local to the request (ie. other concurrent requests have 
> separate caches). How would you do that? It should not matter wether Apache 
> is threading or forking.

$r->notes for strings
$r->pnotes for perl scalars

http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_notes_
http://perl.apache.org/docs/2.0/api/Apache2/RequestUtil.html#C_pnotes_

-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com