Page 1 of 1

Page Caching

Posted: Mon Sep 29, 2008 1:31 am
by supermike
Okay, so I've got page compression turned on, and I'm using APC page accelerator. But I've got a client site I'm working on that does a lot of busy work on the homepage but unfortunately only gets updated about once a day. Is there a quick and easy trick that I can use in my .htaccess (Apache2) or PHP5 page such that I tell the browser to cache the homepage at least all day unless the user forces a reload?

Sorry to sound so dumb on this, but I have never needed to do this before. This is the first client I've had where I'm doing so much on the homepage per his requirements, and I feel if it's slow now, it'll be REALLY slow when it gets hit by several users.

Re: Page Caching

Posted: Mon Sep 29, 2008 1:56 am
by alex.barylski
Trivial caching is ummm...well trivial. Haha.

Although it sounds like you want to cache on the client side too -- unless the client deliberately refreshes.

Basically the idea is this...

0. Check cache URI -- use cache and skip steps 1 & 2
1. Build page HTML
2. Save page HTML

It's really that simple for trivial full page caching. Have a cron job delete the cache files every 24 hours.

Things get tricky when you need partial caching and event based flushing...

Re: Page Caching

Posted: Mon Sep 29, 2008 9:01 am
by supermike
Hockey, you may not have seen this article. I just read it after posting my original statement. It's a bit difficult for me to comprehend just yet -- I mean, it's hard to think when I'm under such terrible project deadlines right now.

http://www.askapache.com/htaccess/speed ... ching.html

Re: Page Caching

Posted: Mon Sep 29, 2008 10:17 am
by panic!

Code: Select all

 
$uri_hash=md5($_SERVER['REQUEST_URI']);
if(file_exists('cache/$uri_hash'))
{
    print file_get_contents('cache/$uri_hash');
}
else
{   
        ob_start();
        //do your stuff here
        $page=ob_get_contents();
        ob_end_clean();
        file_put_contents('cache/$uri_hash',$page);
}
 
extremely simple caching.

Re: Page Caching

Posted: Mon Sep 29, 2008 1:38 pm
by alex.barylski
supermike: I think that .htaccess hack is probably implementing client side caching. That it, it's telling the browser which files to cache on the client side and for how long.

The method I show is server side caching. I prefer server side because then you have explicit control not to mention programatic control over your cache.

Either will work. I imagine all you have to do with .htaccess approach is change the expiry date to sometime the past to flush the client cache and re-download the HTML files.