This tutorial will show numerous options of caching pages, how we can create different versions of cached pages depending upon different factors. VB version.

The output cache is used to ease the load on the server by saving versions of pages in memory, instead of compiling pages every time they are requested by users. ASP.NET 3.5 gives us plenty of options to configure caching to fit our needs. This tutorial will explain some ways in which we can vary our output cache.

When using Output Cache, we use the following Directive at the top of our ASPX page, which looks something like this:

Varying the Output Cache by Header
Using this attribute in the OutputCache directive will create different versions of a page depending upon the value of the particular browser header. The standard browser headers that are transmitted include Accept-Language, which is a list of languages that the client making the request prefers; User-Agent, which is the type of device making the page request; and Cookie, which are the browser cookies created in the current domain.
Using the Accept-Language would look something like this:

We can also use the User-Agent value to create different cached pages depending upon the type of browser used. This directive would look something like this:

We can also see what value the User-Agent is with the following VB code:

We use the time to see if we are viewing a cached page or a generated one, and the UserAgent shows us what the header sees. It is often impractical to use the UserAgent value when using VaryOutputByHeader, as there are so many variations of the string. This results in many different cached versions of the page.

Varying Output Cache by Browser

One way of getting roung the issue mentioned above is to use the VaryByCustom attribute of the OutputCache directive, and use the browser value. We would do this by using the following:

The above directive would create cached pages of different browser types, and major versions.

Varying the Output Cache by Custom Function

We can also create our own functions to vary caching by. We can do this by using the VaryByCustom attribute of the directive and using our own custom string and then modifying the Global.asax file. For example, if we wanted to create different cached pages for browsers that supported CSS and those that didn’t, then we could add the following method into the Global.asax file:

We then use the VaryByCustom attribute as follows:

Download Source Files