In PHP, I am trying to steal a page from Playbook Rails (see "Using Timestamps" here ):
By default, Rails adds assets, timestamps to all asset paths. This allows you to set the cache expiration date for the asset far into the future, but still be able to instantly void it by simply updating the file (and therefore updating the timestamp, which then updates the URL because the timestamp is part of this, which, in in turn destroys the cache).
It is the responsibility of the website server that you use to set the future expiration date of the cache assets that you need to take advantage of this feature. Here is an Apache example:
# Asset Expiration ExpiresActive On <FilesMatch "\.(ico|gif|jpe?g|png|js|css)$"> ExpiresDefault "access plus 1 year" </FilesMatch>
If you look at the source for the Rails page, you will see what they mean: the path to the stylesheet can be "/stylesheets/scaffold.css?1268228124" , where the numbers at the end are the timestamp when the file was last updated.
Therefore, it should work as follows:
- Browser says give me this page.
- The server says "here, and by the way, this style sheet called
scaffold.css?1268228124 can be cached for a year - it will not change." - On reboot, the browser says: "I am not asking for this css file because my local copy is still good."
- After a month, you edit and save the file that changes the timestamp, which means that the file is no longer called
scaffold.css?1268228124 , because the numbers change. - When the browser sees this, it says: "I never saw this file! Give me a copy, please. The cache is" busted ".
I think brilliant. So I wrote a function that splashes out stylesheets and javascript with timestamps added to file names, and I configured Apache with the above statement.
Now: how to determine if caching and cache work?
I view my pages with two plugins for Firebug: Yslow and Google Page Speed. Both seem to say that my files are cached: “Add expire header” in Yslow and “use browser caching” at page speed, both checked.
But when I look at the activity of page speed, I see a lot of requests and expectations and no "cache hits".
If I change the stylesheet and reboot, I will immediately see this change. But I do not know that due to the fact that the browser was never cached in the first place or because the cache is in busted state.
How can i say
Update: it works!
If anyone is interested, I just wrote a blog post explaining the details .