How to Force clear cache

Website

As all developers know our best friend and worst enemy is the cache. You can make all your changes and be confident they will work, then they don’t change just when you want them to. It is great that you can clear your own cache, but how can you refresh all the users cache? Here are some of the methods I know of and used.

Bit of a FYI part, this is focused to JavaScript on a Windows server running IIS, but some of these methods can be used on multiple languages.

Query string

First up is the query string method. When the browser caches the file it caches the URL for it location. This included the query string of the URL. It makes it like the key to the value, so if you change the key then it needs to get a new value.

For Example:
http://www.myurl.com/myjavascript.js?v=1.2

The query string can be anything, so you could even just put the number.

For Example:
http://www.myurl.com/myjavascript.js?1.2

You can then make this dynamic my appending the build number of your deployment. This however is dependent on who, what and where your file is. Unfortunately as well, some browsers don’t use the whole URL. They cut the Query string off for the Key part of the cache. This means this method doesn’t work for some browsers.

IIS URL Rewrite

This can be done in IIS and also using the .htaccess on an apache server. For this you will need to install the IIS URL Rewrite tool from Microsoft.

The use of this is to rewrite the directory of the URL instead, so unlike the previous method it would work in every browser.  The only issue I have found with this is I don’t know how to add a version number on there without having to physically add a number on the end for each deployment. You can however also use this in the web.config of the application, where you might be able to dynamically get the number of the build.

Here is how to use the URL Rewriter:
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

Restart Application Pool

This is a more indirect method. You can force user to get a new copy of the site if you restart you application pool in IIS, but this would clear the whole site so if you just changed one JavaScript file they will be getting more than they needed. This can then be worst for the users that are on their mobile phones as it then takes more data to get the whole site.

If you are ok with this then you could also script a PowerShell script to restart the applications at will. This can be done with dunning iisreset from an elevated (on Vista/Win7/Win2008) command prompt, which will restart IIS and in turn restart all the application pools.

These are just some method that can used and there are also other way to use the premises, but in maybe better ways for your application. If you have any other methods please comment them below and share the knowledge.

Advertisements

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s