Page Caching

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Page Caching

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Page Caching

Post 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...
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Re: Page Caching

Post 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
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Page Caching

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Page Caching

Post 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.
Post Reply