ASP Solution with No Cache

ASPInternet browser and proxy caching makes all ours lives easier. It makes surfing the web far quicker than what it would normally be and saves us a lot of bandwidth in the process. However, to dynamic script developers, caching can be a bit of a nightmare, and more often than not one sits with the burning question, “How do I stop users from caching my ASP pages!?!” (Well no, I doubt that anyone sits with that exact question in mind, apart from people – like me – who hopes that phrasing the question like that will result in more search engine hits.)

For those of you who are already lost, caching refers to the practice of your browser or proxy server saving downloaded files to a temporary storage area and when a web page refers to an already downloaded file or picture, serving the saved file instead of downloading a fresh version of it.

The Response object in ASP provides us with several properties that help control browser caching and these are the most common ones: Response.Expires, Response.ExpiresAbsolute and Response.CacheControl. Setting these variables correctly and putting it at the top of all your pages (think along the lines of an include file) should prevent most browser caching and leave you one happy camper with regards to caching headaches. So for example, you could use something like this at the top of you pages:

<%
Response.Expires = 5
Response.ExpiresAbsolute = Now() – 2
Response.AddHeader “pragma”,”no-cache”
Response.AddHeader “cache-control”,”private”
Response.CacheControl = “private”
%>

Small disclaimer: Note that not all browsers play nice and despite the fact that all the above does technically work and should prevent browser and proxy caching, sometimes the only way to be really, really be sure is to add a variable GET value (include maybe a timestamp) to the application’s outgoing URLs in order to confuse either the browser or proxy cache so that it is forced to pull down a new copy of the page every time anyway.

Related Posts :

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code. Bookmark the permalink.