From here to eternity

Being a few words describing an ISV journey …

2006-09-27 : Serving files with Cherrypy and the browser cache?

I have been wrestling with a problem with cherrypy, the web application framework I use for all sorts of things. The problem has been that served files for download (not web pages or anything like that) are cached by browsers. So although you can serve files very easily with cherrypy, the stupid web browser doesn't even request the file again from the cherrypy application.

This means a link for a download (which is a cherrypy handler method) that actually does something in addition to serving the file, doesn't even get executed! Ultimately, this means that files which may change, but have the same url within the application are never served to a client UNLESS they clear the client browser clears their cache.

Why the browser doesn't re-request the file has been a mystery for several months, but I think I know why. It comes down to the reponse headers and it appears that a part of the reponse header determines how the link (and hence the downloaded document) is cached.

In FireFox you can get information about the cache by typing "about:cache" in the address bar. When I looked at the cache entries I noticed that the downloaded file's link had the "Expires" set to never!

You can set the reponse headers in cherrypy using cherrypy.response.headerMap (v2.1.0) and these need to be set:

cherrypy.response.headerMap['Expires'] = 'Sun, 19 Nov 1978 05:00:00 GMT'
cherrypy.response.headerMap['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
cherrypy.response.headerMap['Pragma'] = 'no-cache'

Obviously you can make the date as old as you want. All this was provided by a google search and in cherrypy you can set these within a decorator as described here. Note that the code at the link is cherrypy v2.2 so response.headers is used rather than response.headerMap (and I don't know the relevance for cherrypy 3.x).


Blog List

2008-08-22 : Something weird has happened to my sales

2008-08-06 : SimpleBugs Released

2008-01-28 : When Too Much Choice Is Bad

2007-12-22 : The Hard Disk Crash

2007-11-30 : What To Blog About

2007-10-27 : Facebook, Facebook and more bloody Facebook

2007-10-01 : What To Blog About

2007-07-04 : SRedirect and SBridge Released

2007-06-30 : War On Spam - Embedded

2007-05-15 : The War On Spam

2007-04-30 : TopDesk v1.5 Is Out

2007-04-08 : Spam, spam, spam, and more spam.

2007-02-02 : To \n or not to \n?

2007-01-03 : Why rolling your own code is fun

2006-12-31 : Where has the year gone?

2006-11-01 : Fsync Released

2006-09-27 : Serving files with Cherrypy and the browser cache?

2006-09-26 : SimpleQuality Hits Beta

2006-09-19 : Too busy to blog

2006-08-14 : What's in a name?

2006-08-13 : Webfaction and Snake Oil

2006-08-03 : PayPal is pay n'ful

2006-07-23 : Why the internet is what it is

2006-07-21 : Why automatic updates are a "bad" thing

2006-07-20 : From reality to fresh hope

2006-07-17 : ... freedom!

2006-07-10 : It's coming together