Forum Moderators: phranque
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.
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?
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.
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.