'Cache-Control' or Expires

16 September 2005   3 comments   Zope

Mind That Age!

This blog post is 13 years old! Most likely, its content is outdated. Especially if it's technical.

Dieter Maurer was as kind as he always is one the Zope mailing list and explained to me the difference between using the Cache-Control and the Expires header for inducing browsers to cache your web elements. He explains:

Cache control is the modern (HTTP 1.1) way, "Expires" the old (HTTP 1.0) one.

My problem is that I've know about each but never bothered to find out why there are two ways of doing the same thing. In most of my projects I've used the oldschool method of setting RFC822 formatted future dates on the Expires header, but from now on I'll use both. Dieter continues on my question "What is best to use and for what?":

Use them both:

HTTP 1.1 clients will honour "Cache-Control" (which is easier to use and much more flexible).

HTTP 1.0 clients will ignore "Cache-Control" but honour "Expires". With "Expires" you get thus at least a bit control for these old clients.

Now, let's speak code. Here's what I now do to set caching on my pages when I want to:

def doCache(self, hours=10):
    """ set cache headers on this request if not in debug mode """
    if not self.doDebug():
        response = self.REQUEST.RESPONSE
        now = DateTime()
        then = now+int(hours/24.0)
        response.setHeader('Expires',then.rfc822())
        response.setHeader('Cache-Control', 'public,max-age=%d' % int(3600*hours))

This I can then use in say a stylesheet like this:

<dtml-call "doCache(48)">

Comments

Sukh
How do I add this to my site. I would like it so when the user goes back you get an Expired page appear. I have looked over the web for a solution but cannot seem to find one. Can I simply put the above method in a script and call it each time a page is loaded.
Colin Wallace
Could you kindly explain what the DateTime object is? Perhaps it's because I'm using Python 2.7, but the datetime class in the datetime module has no rfc822 method.
Thanks!
Peter Bengtsson
DateTime was a utility library used in Zope2.

Your email will never ever be published


Related posts

Previous:
Misstake or hidden Nazi message? 13 September 2005
Next:
Python regular expression tester 19 September 2005
Related by Keyword:
How to use django-cache-memoize 03 November 2017
django-cache-memoize 27 October 2017
cache_memoize - a pretty decent cache decorator for Django 11 September 2017
Fastest Redis configuration for Django 11 May 2017
Welcome to the world django-fancy-cache! 01 March 2013
Related by Text:
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
Ever wondered how much $87 Billion is? 04 November 2003
Guake, not Yakuake or Yeahconsole 23 January 2010