RE: [PHP] Poor man's cache...

From: Date: Fri, 28 Sep 2001 09:16:49 +0000
Subject: RE: [PHP] Poor man's cache...
Groups: php.general 
Request: Send a blank email to php-general+get-69142@lists.php.net to get a copy of this message
Okay, so how do you get a 404 error sent for a page that doesn't exist but has been requested with an extension that apache has been told is php, e.g. http://www.stivesdirect.com/fred.html <http://www.stivesdirect.com/fred.php> . Yes I know something is wrong but does anybody have any hints as to what is wrong? Tim Ward ---------- From: Rasmus Lerdorf [SMTP:rasmus@php.net] Sent: 27 September 2001 19:57 To: Chris Hobbs Cc: php-general@lists.php.net Subject: Re: [PHP] Poor man's cache... Instead of the redirect like that, you could set up an ErrorDocument 404 handler in your httpd.conf and in that handler script check for that specific URL and generate it. If the file exists your 404 handler won't get called and you save yourself a stat() and a redirect. The other way is to stick a reverse-proxy like Squid in front of your server and send an appropriate Cache-Control header from your script. Or even a combination of both. -Rasmus On Thu, 27 Sep 2001, Chris Hobbs wrote: > Well, I built a really awful Table of Contents script yesterday that has > to grep through about a thousand files (Word docs, no less) to find the > titles. Anyway, the script works great, but it is slow - 20-30 seconds > per load, which is hardly acceptable. So, since the data will only > change occasionally, I wrote the following wrapper for the script to > check if a cachefile already exists, and if it does, it redirects to it. > If not, it uses php's output buffering to build the page, writes the > buffer out to the cache file, and then dumps the buffer to the browser. > > When I change the docs on the site, I merely have to delete the cached > file - the next person that loads it will rebuild it for me. > > Anyway, I include it here for your edification. > > <? > // Get just the script name, no /'s > ereg(".*/(.*)", $PHP_SELF, $regs); > $cachefilename = $regs[1].".html"; > if (file_exists("/path/to/your/cache/$cachefilename")) { > header ("Location: $cachefilename"); > exit (); > } > ob_start(); > > // All of your normal script goes here, complete with > // echo's, pure HTML between ?> and <?, whatever... > > $complete_page=ob_get_contents(); > $fd = fopen ("/path/to/your/cache/$cachefilename", "w"); > fwrite ($fd, $complete_page); > fclose($fd); > ob_end_flush(); > ?> > > Hope you find it useful! > >

« previous php.general (#69142) next »