Enabling Browser Caching of ASP Pages

If you have been coding in ASP for some time, you must have realised that your ASP pages take a bit of time on slower connections. Not just your problem, but it is a problem faced by many. You make a website that has some functionality that uses ASP like getting some content from a database. Though there are other pages on your website too, but for sake of symmetry in file naming, you make your all pages as ASP even though you don't require to do that.

Now, most browsers, by default, don't cache pages that are dynamically generated by the server. ASP pages also come into this category. But there are times when the content of your webpage doesn't change in a while but still, every time a visitor wants to see that page, its requested from the server by your browser. In these circumstances, cacheing can really speed up your visitor's experience & chances are that he will visit your website again.

So how do we go about it? Simple, we will just have to make sure the browser cache our pages. But first question should be, what is a cache & why should you cache your page.

Cache is a browser's personal storage where it stores every web page that you visit. Its emptied by the browser after a set period of time if you don't empty it yourself. You should cache your those pages that don't change often. Its potential benefits are:-

  1. Most importantly, the visitor is presented with the cached page. Since its present on local computer, its delivered faster which just delights the visitor. Afterall whoever wants to wait for even 10-20 seconds if they have another option?
  2. It also helps your website to use less bandwidth of the server. Important for people who have limited data-transfer allowance per month for their websites.

So enough talks, lets get down to some coding. Its not much, only a few tit bits, I promise.

First, is the EnableSessionState directive. You must be familiar with the following directive:

<%@LANGUAGE="VBScript" %>

Now just add the EnableSessionState directive to it & set it False so it becomes

<%@LANGUAGE="VBScript" EnableSessionState="FALSE" %>

The EnableSessionState directive tells whether should the server start a new session every time the page is requested or not. This is True by default. By turning it False, the server does not initiate a new Session every time the page is requested by a visitor. It can be put safely on pages that don't use any session calls, etc.

Now another piece of work is to define the cache as public for the page. For dynamic pages like that of ASP, the cache control is private by default. It means that the cache is not available & the page will be delivered by the server every time it is requested. By defining it public, we ensure that its available to every user of the browser & the server will not be asked to deliver the page every time the page is requested. The code for that is

<% Response.CacheControl="Public" %>

All you have to do is put this before you send any headers to the browsers, like any <html> tags etc. Best place to put it is right after the language directive at the top of the file, to ensure that it works. This way you can make sure that at least the static pages of your website are delivered fast to your visitors.

You might also like...

Comments

Amit Gupta

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”