[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    RE: Caching a hash - am I missing something?
From:       "cfaust-dougot" <cfaust () doyougot ! com>
Date:       2008-08-20 10:22:38
Message-ID: B1DE2E9211866F4C840F440F731A0AE606C835 () exchback1 ! pub ! prevare ! com
[Download RAW message or body]

Thanks Andre and Tyler.
 
Believe it or not I never even thought of that until you guys pointed it out (that \
the hash would live for the the life of the apache child). I can be such a fool \
sometimes :)  
In this case I assume %cache is global - it only appers in that sub, there is no "my \
%cache = ()" any place else in the pm file and there is no "Use strict".  
I guess I'll just keep it the same way except I'll "use strict" and I'll declare "my \
%cache = ()" outside that sub.  
Thanks Guys!!
-Chris

________________________________

From: André Warnier [mailto:aw@ice-sa.com]
Sent: Wed 8/20/2008 3:28 AM
To: mod_perl list
Cc: W. Tyler Gee
Subject: Re: Caching a hash - am I missing something?



W. Tyler Gee wrote:
> On Tue, Aug 19, 2008 at 5:35 PM, Chris Faust <cfaust@doyougot.com> wrote:
> > Hi,
> > 
> > 
> > 
> > This might be a little off topic, I hope it's OK to post. I'm not positive
> > if mod_perl matters or not because it's a little confusing to me.
> > 
> > 
> > 
> > I've taken over some pretty old code that I'm updating and making mp2
> > content handlers out of. The main script is a standard cgi script
> > "start.cgi" there is nothing special in the apache conf for it.
> > 
> > 
> > 
> > <Directory /xxx/>
> > 
> > SetHandler perl-script
> > 
> > PerlFixupHandler My::Fixup
> > 
> > PerlResponseHandler ModPerl::PerlRun
> > 
> > PerlOptions +ParseHeaders
> > 
> > DirectoryIndex start.cgi
> > 
> > Options +ExecCGI +Indexes
> > 
> > allow from all
> > 
> > </Directory>
> > 
> > 
> > 
> > start.cgi calls a custom module (use CustomModule;) which exports a bunch of
> > subs, for example foobar and all over the place in the subs that are
> > exported from CustomModule I see code like
> > 
> > 
> > 
> > 
> > 
> > sub foobar {
> > 
> > my $key = @_;
> > 
> > 
> > 
> > if ($cache{$key}) {
> > 
> > return $cache{$key};
> > 
> > } else {
> > 
> > my $do_some_query = xxxx;
> > 
> > $cache{$key} = $do_some_query_results
> > 
> > return $cache{$key};
> > 
> > }
> > 
> > }
> > 
> > 
> > 
> > My question is isn't the "else" in foobar always going to be true anyplace
> > where start.cgi is calling "&foobar('somekey')"??????
> > 
> > I don't understand how %cache could already be populate from a previous
> > browser request or something? I'm I just missing something stupid?
> 
> %cache is defined outside the scope of the sub so it will persist for
> the lifetime of the apache server.  The very first time
> foobar('somekey') is called it will do the query lookup, the next time
> it will return from cache.
> 
I believe the above is almost, but not totally true.  It should probably
be "for the lifetime of this particular apache child".
Each apache child process has it's own copy of the above code, and it's
own copy of the above "global" (*) %cache hash.  Thus whether the first
or second part of the if will run, depends of the previous history of
the particular apache child which handles the current request.  If this
particular child has already previously accessed the same hash key, it
will server it from (it's own) cache, and otherwise it will execute the
query to create the key (in it's own cache).
Apache children are created, and die, as directed by the main Apache
process configuration, and HTTP requests are served more or less at
random, by whichever child is available when the request comes in.
Is is thus quite possible for instance that the first 10 requests for a
particular hash key would each be handled by a different apache child,
and would each result in a query; then the 11th request would be handled
by a child that has already accessed this same key before, and thus
served from cache; the 13th request would be handled by a brand-new
apache child just created, thus would re-do the query, etc..

I this would be useful, I believe that it would be possible to avoid
this, by "priming" the mod_perl module during the initial start of
Apache, before it forks into children.  Then each new child (being a
copy of the main apache) would start it's life with a number of keys
already in its cache.  Of course this would work only if the contents of
the keys of the hash are never modified while apache is running.
And it probably involves some delicate mod_perl programming to do the
priming process.

(*) like the previous contributor, I guess that the %cache hash is
somehow global, because it is not declared within the sub that you show.
  Whether it really is though, may depend on other code that we don't
see here.  But anyway, "global" would mean only "global to this apache
child", not to the whole apache server.

And something that I don't know at all, is how this all works out with a
threaded apache, such as under Windows e.g.

André


[Attachment #3 (text/html)]

<HTML dir=ltr><HEAD><TITLE>Re: Caching a hash - am I missing something?</TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.6000.16705" name=GENERATOR></HEAD>
<BODY>
<DIV id=idOWAReplyText4091 dir=ltr>
<DIV dir=ltr><FONT face=Arial color=#000000 size=2>Thanks Andre and \
Tyler.</FONT></DIV> <DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial size=2>Believe it or not I never even thought of that \
until you guys pointed it out&nbsp;(that the hash would live for the&nbsp;the life of \
the apache child). I can be such a fool sometimes :)</FONT></DIV> <DIV dir=ltr><FONT \
face=Arial size=2></FONT>&nbsp;</DIV> <DIV dir=ltr><FONT face=Arial size=2>In this \
case I assume %cache is global - it only appers in that sub, there is no "my %cache = \
()" any place else in the pm file and there is no "Use strict".</FONT></DIV> <DIV \
dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV> <DIV dir=ltr><FONT face=Arial \
size=2>I guess I'll just keep it the same way except I'll "use strict" and I'll \
declare "my %cache = ()" outside that sub.</FONT></DIV> <DIV dir=ltr><FONT face=Arial \
size=2></FONT>&nbsp;</DIV> <DIV dir=ltr><FONT face=Arial size=2>Thanks \
Guys!!</FONT></DIV> <DIV dir=ltr><FONT face=Arial size=2>-Chris</FONT></DIV></DIV>
<DIV dir=ltr><BR>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> André Warnier \
[mailto:aw@ice-sa.com]<BR><B>Sent:</B> Wed 8/20/2008 3:28 AM<BR><B>To:</B> mod_perl \
list<BR><B>Cc:</B> W. Tyler Gee<BR><B>Subject:</B> Re: Caching a hash - am I missing \
something?<BR></FONT><BR></DIV> <DIV>
<P><FONT size=2>W. Tyler Gee wrote:<BR>&gt; On Tue, Aug 19, 2008 at 5:35 PM, Chris \
Faust &lt;cfaust@doyougot.com&gt; wrote:<BR>&gt;&gt; \
Hi,<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt; This might be a little off topic, \
I hope it's OK to post. I'm not positive<BR>&gt;&gt; if mod_perl matters or not \
because it's a little confusing to \
me.<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt; I've taken over some pretty old \
code that I'm updating and making mp2<BR>&gt;&gt; content handlers out of. The main \
script is a standard cgi script<BR>&gt;&gt; "start.cgi" there is nothing special in \
the apache conf for it.<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
&lt;Directory /xxx/&gt;<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
SetHandler perl-script<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
PerlFixupHandler My::Fixup<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
PerlResponseHandler ModPerl::PerlRun<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
PerlOptions +ParseHeaders<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
DirectoryIndex start.cgi<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
Options +ExecCGI +Indexes<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
allow from all<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
&lt;/Directory&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt; start.cgi calls a \
custom module (use CustomModule;) which exports a bunch of<BR>&gt;&gt; subs, for \
example foobar and all over the place in the subs that are<BR>&gt;&gt; exported from \
CustomModule I see code \
like<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt; sub \
foobar {<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
my $key = @_;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
if ($cache{$key}) {<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& \
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
return $cache{$key};<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
} else {<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
my $do_some_query = xxxx;<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& \
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
$cache{$key} = $do_some_query_results<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;& \
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
return $cache{$key};<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
}<BR>&gt;&gt;<BR>&gt;&gt; }<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt; My \
question is isn't the "else" in foobar always going to be true anyplace<BR>&gt;&gt; \
where start.cgi is calling "&amp;foobar('somekey')"??????<BR>&gt;&gt;<BR>&gt;&gt; I \
don't understand how %cache could already be populate from a previous<BR>&gt;&gt; \
browser request or something? I'm I just missing something stupid?<BR>&gt;<BR>&gt; \
%cache is defined outside the scope of the sub so it will persist for<BR>&gt; the \
lifetime of the apache server.&nbsp; The very first time<BR>&gt; foobar('somekey') is \
called it will do the query lookup, the next time<BR>&gt; it will return from \
cache.<BR>&gt;<BR>I believe the above is almost, but not totally true.&nbsp; It \
should probably<BR>be "for the lifetime of this particular apache child".<BR>Each \
apache child process has it's own copy of the above code, and it's<BR>own copy of the \
above "global" (*) %cache hash.&nbsp; Thus whether the first<BR>or second part of the \
if will run, depends of the previous history of<BR>the particular apache child which \
handles the current request.&nbsp; If this<BR>particular child has already previously \
accessed the same hash key, it<BR>will server it from (it's own) cache, and otherwise \
it will execute the<BR>query to create the key (in it's own cache).<BR>Apache \
children are created, and die, as directed by the main Apache<BR>process \
configuration, and HTTP requests are served more or less at<BR>random, by whichever \
child is available when the request comes in.<BR>Is is thus quite possible for \
instance that the first 10 requests for a<BR>particular hash key would each be \
handled by a different apache child,<BR>and would each result in a query; then the \
11th request would be handled<BR>by a child that has already accessed this same key \
before, and thus<BR>served from cache; the 13th request would be handled by a \
brand-new<BR>apache child just created, thus would re-do the query, etc..<BR><BR>I \
this would be useful, I believe that it would be possible to avoid<BR>this, by \
"priming" the mod_perl module during the initial start of<BR>Apache, before it forks \
into children.&nbsp; Then each new child (being a<BR>copy of the main apache) would \
start it's life with a number of keys<BR>already in its cache.&nbsp; Of course this \
would work only if the contents of<BR>the keys of the hash are never modified while \
apache is running.<BR>And it probably involves some delicate mod_perl programming to \
do the<BR>priming process.<BR><BR>(*) like the previous contributor, I guess that the \
%cache hash is<BR>somehow global, because it is not declared within the sub that you \
show.<BR>&nbsp; Whether it really is though, may depend on other code that we \
don't<BR>see here.&nbsp; But anyway, "global" would mean only "global to this \
apache<BR>child", not to the whole apache server.<BR><BR>And something that I don't \
know at all, is how this all works out with a<BR>threaded apache, such as under \
Windows e.g.<BR><BR>André<BR><BR></FONT></P></DIV></BODY></HTML>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic