I’m not sure if this is all new or if I’m slow and everyone else already knew this or if there’s a select niche of people who have realised what I’m about to talk about. I always see a huge problem in PHP Web Apps, the total and utter eradication of local caching. It’s the ‘bomb them all’ method of ensuring your site is always serving the most up to date copy and everyone uses it. Frankly, it’s bad form, it’s not cool and it has to stop. Unless you have a compelling reason for serving fresh pages EVERY single hit and you have the server to handle it, i suggest you learn from our mistakes.
We do a lot of online stores and one thing they have a lot of, is IMAGES. One thing we do people hate us for, is store these in the database. “It’s slow” they say, “It’s flexible” i say and we argue back and forth for hours. Here’s the thing though, it’s only ever noticeably slow when you’re re-sizing them on the fly, otherwise the page load times are fine.
The problem only comes in to play when people are hammering your server and your image retrieval scripts are querying for images over and over again for images the users seen thousands of times before and already have a copy of in their local cache. All because every piece of image display code you ever read for PHP never EVER includes local cache control, so everytime the person refreshes the page they get all 20 or so images again, for no reason.
So heres the secret, timestamp every row in your database with a last modified date, excessive? perhaps.. usefull? you betcha! Now, as early on in your application, as soon as you can, before doing excessive tasks that take time, check the date that corresponds to your page/image/whatever and send a Last-Modified: header value of that time. This tells the web browser that you want to cache this file and that next time, it should send an If-Modified-Since: header in it’s request.
If-Modified-Since: is where the beauty lies, before you output the Last-Modified header and while you have the dates from your database, check them against each other, if the database is newer than If-Modified-Since, continue on as you would serving the site, but if not, fire off a HTTP 304 Status code and exit(); the application.
Why serve it if you don’t need to? the local cache is there for a reason. Coming next, examples.