Re: Poor man's cache...

From: Date: Thu, 27 Sep 2001 19:51:53 +0000
Subject: Re: Poor man's cache...
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-69039@lists.php.net to get a copy of this message
I had problems with using a 404 document, the 404 headers were still being sent, the 404.php file would pickup and send the correct data, but the headers still contained a 404 error, this was causing strange errors in some browsers having the 404.php file output images, more of a problem was having the 404.php doc output a file for download, the 404 error was messing things up. I dont know how to change it so the 404 header doesnt get sent. -- Chris Lee lee@mediawaveonline.com "Rasmus Lerdorf" <rasmus@php.net> wrote in message news:Pine.LNX.4.33.0109271153440.1491-100000@master.php.net... > 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 (#69039) next »