On 2007.09.28, Bas Scheffers <[EMAIL PROTECTED]> wrote:
> My solution to that problem was simply caching in the filesystem and  
> serving static files. The way this works in a multi-server  
> environment is that the custom 404 handler figures out the request  
> was for "/photo/123/axbcgsfdt.jpg" and just grabs it from the  
> database, caches it and redirects back to it.
> 
> That way I get the convenience of Tcl code and the speed of static  
> files - save for the first request.

Yes!  Finally, someone else who uses the 404-handler-as-request-processor
pattern!  Indeed, you can't beat static file serving performance.  And,
if you want to "clear the cache" you just unlink the file out of the
filesystem, and the next request through "re-caches" it.

The only tricky thing is to handle the "stampeding herd" situation,
where many requests for the same uncached object come in at the same
time.  You only want one request to populate the cache--you want all
other requests to block/yield.  A simple condvar/broadcast works well
for this--before starting the "expensive" work involved in populating
the cache, you push a request for the object to be generated into a
queue (producers) then wait on the condvar.  A thread pool (consumers)
gathers all requests for the same object and generates it once, then
broadcasts to unblock all the waiters.  They wake up, check to see if
their request was fulfilled--if it was, they consume from the cache.
Otherwise, they wait on the condvar again, until they time out.

This level of care is only really important when the cost of populating
a cache entry is "expensive" and the cost increases as concurrency
increases (i.e., concurrent database queries).

-- Dossy

-- 
Dossy Shiobara              | [EMAIL PROTECTED] | http://dossy.org/
Panoptic Computer Network   | http://panoptic.com/
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)


--
AOLserver - http://www.aolserver.com/

To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]> 
with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: 
field of your email blank.

Reply via email to