Forum Moderators: phranque

Message Too Old, No Replies

Best practice for caching dynamic sites?

         

encyclo

2:42 pm on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm looking for some guidance on how best to improve cachability for dynamically-driven sites which nevertheless change infrequently. I have a directory site which is PHP/MySQL driven, but the page contents evolve slowly. Although there are additions to categories each day, many pages remain unchanged from day to day, and decent caching will help improve site performance, reduce bandwidth and allow for easier spidering (GoogleGuy said so!).

I would like the pages (which use mod_rewrite to give static URLs) to be cacheable as much as possible, but as PHP doesn't send the appropriate headers by default, I need to set them manually.

At the moment, I have simply added a header with

Cache-Control: max-age=86400
(1 day) for each generated page. Is this appropriate or sufficient, or is there a more robust or batter way of doing this?

Thanks for your replies.

pete_m

11:23 pm on Nov 16, 2004 (gmt 0)

10+ Year Member



As far as I understand web caches, your
Cache-Control: max-age=86400
should work fine - at least with everything that understands HTTP 1.1 (i.e. every modern browser, and most spiders).

Another option is to set a dynamic

Expires: 
header, set to 1 day in the future. This is compatible with HTTP 1.0, but will be inexact if the server clock isn't correct.

There's an excellent article about caching at [mnot.net ].

I can't seem to find GoogleGuy's comments on caching - do you know which topic it was?

encyclo

12:04 am on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply, pete_m.

This is the GoogleGuy thread talking about caching, especially supporting If Modified Since:

[webmasterworld.com...]

It's a couple of years old but still very relevant.

Trouble is, using the cache-testing sites, it is not clear if the max-age header is sufficient. I'm not sending a Content-Length or Last-Modified header, and I'm not certain if I should be trying to.

jollymcfats

6:25 pm on Nov 17, 2004 (gmt 0)

10+ Year Member



Trouble is, using the cache-testing sites, it is not clear if the max-age header is sufficient. I'm not sending a Content-Length or Last-Modified header, and I'm not certain if I should be trying to.

Last-Modified is the best header-based cache tool in the arsenal. If you know or can calculate when the sources for your dynamic page last changed, definitely send Last-Modified and add logic to handle If-Modified-Since requests. You'll immediately gain caching benefits exactly on par with static content.

Another approach for infrequently updated content (say, less than once a minute) is to simply pre-generate static versions of the content. That's usually easier than tracking & clocking the data dependencies behind dynamic pages. Not only will the web server handle Last-Modified negotiations for you, but you'll also have almost the best possible site performance.

Even a simple pre-generation implementation will be very effective- perhaps something as simple as generating and saving all possible pages every few minutes and publishing the changed files, say by diffing the latest generated content vs. the cached files under document root.