How To Make IE Cache Less

Internet Explorer is known to cache the responses of GET calls. The problem occurs if your javascript functions request the same url over and over again. Internet Explorer will cache the response of the first call, and subsequent calls will automatically return the same response, without actually contacting the server. There are two approaches to solve this problem.

One approach could be to add a random part to the url (i.e.: /poll?random=f2dee87716f). So, the browser will think of it as a different URL everytime. An alternative approach could be to set the response headers correctly. The HTTP headers that need to be set are as follows:

Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache 

Here’s how I do it in TurboGears. To each method that I don’t want IE to cache, I add the strongly_expire decorator, like this:

    def cant_cache_me(self, position):

The code of the decorator, which is responsible to set the headers propertly:

def strongly_expire(func):
    """Decorator that sends headers that instruct browsers and proxies not to cache.
    def newfunc(*args, **kwargs):
        cherrypy.response.headers['Expires'] = 'Sun, 19 Nov 1978 05:00:00 GMT'
        cherrypy.response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
        cherrypy.response.headers['Pragma'] = 'no-cache' 
        return func(*args, **kwargs)
    return newfunc
This entry was posted in howto, turbogears. Bookmark the permalink.

45 Responses to How To Make IE Cache Less

  1. Pingback: From here to eternity » Blog Archive » Serving files with Cherrypy and the browser cache?

  2. Pingback: Making a Flickr Killer With TurboGears - Part 2: A Flickr Clone in 37 Minutes Flat · Nadav Samet's Blog

  3. SeriousUser says:

    Where did you add your decorator definition? The code for
    def strongly_expire(func):

  4. Nadav says:

    Just in the top of the controller module.

  5. SeriousUser says:

    Thanks for your reply. I tried implementing this in my code but it just seems to break it. Without adding the decorator, I am receiving the data fine in JSON format. However, if I include the
    before my function header, it seems to break it and no data comes through. Any thoughts?

  6. SeriousUser says:

    Sorry, I made such a novice error. Please disregard the previous message. Thank you for providing this code snippet.

  7. Admiyn says:

    this post was helpful to me,
    thanks for everybody posting here

  8. This paragraph will assist the internet viewers for creating new
    webpage or even a weblog from start to end.

  9. Hello guys if you can’t earn any money online try – bluehand
    roulette system – it earns me decent money everyday, just google

  10. Carlota says:

    I see a lot of interesting content on your blog.
    You have to spend a lot of time writing, i know how to save you a lot of work, there
    is a tool that creates unique, SEO friendly articles in couple of
    minutes, just type in google – laranita’s free content source

  11. Ruben says:

    my page: webpage (Ruben)

  12. Evangeline says:

    Here is my webpage webpage (Evangeline)

  13. Winfred says:

    Feel free to visit my web site :: web page (Winfred)

  14. Google says:

    Using Ancient Rome 3D in Google Earth, you can explore Rome as it appeared in 320 A.
    Based on their experience, they could know how much is required before going into details.
    Besides placing advertisers ads on your Blog, you can also make money Blogging by
    placing Google
    Adsense into your Blog.

  15. Despite the specifics search engine marketing of how the site and does the same
    time not crowded.

    Feel free to surf to my web site … search engine marketer

  16. Pay-per-click advertising [ppc] is platform whereby
    search engine marketing you can create best web design for your budget.
    Engaging With the advancement of technologies.

    Feel free to visit my web page – search engine optimization service provider

  17. I constÉ‘ntly spent my Òºalf an hour to read this website’s posts everyday alß‹ng with а mÕ½g of coffee.

  18. LienGileswet says:

    I see your page needs some fresh content. Writing manually is time consuming, but there is solution for this.
    Just search for: Masquro’s strategies

  19. example says:

    Hello I am so delighted I found your web site, I really found you
    by error, while I was browsing on Digg for something else, Anyhow I am here now and would just like to say many thanks for a remarkable post and a
    all round interesting blog (I also love the theme/design), I don’t
    have time to browse it all at the minute but I have book-marked it and also
    added in your RSS feeds, so when I have time I will be back to read much more, Please do keep up the awesome

  20. I am no longer certain the place you are getting your info, but gookd topic.

    I needs tto spend a whilke finding out much moe or figuring out more.

    Thanks for wonderful information I was in search of this info for
    my mission.

Leave a Reply

Your email address will not be published. Required fields are marked *