Re: Poor man's cache...
| From: | Rasmus Lerdorf | Date: | Thu, 27 Sep 2001 18:56:37 +0000 |
| Subject: | Re: Poor man's cache... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-69034@lists.php.net to get a copy of this message | ||
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!
>
>