Caching multiple versions of a page
A web page may be required to display different data on user selection. Caching such pages will enable visitors to browse all the pages speedily and avail a better user experience. Creating a cache for such pages can be done by using the declarative method of using OutputCache directive. The declarative method is the most simple and straight forward method of implementing a cache.
We have already seen how to implement a basic technique of page output caching. To recap, we enable page output caching by including following page directive in a web page.
<%@ OutputCache Duration=”60” VaryByParam=”none” %>
An example of displaying data on user selection is when a web page has to display data pertaining to a country based on the country selected by the user. Such web pages are also referred to as multiple versions of a web page. To program such a web page, we pass a value selected by the user, to a stored procedure and the resulting data is displayed on the web page.
By using page output caching, we can cache each version of the web page. For the example discussed above we can have a cache of the data for each of the country. So, whichever the country the user selects, the cache will be displayed thereby enhancing performance.
In the code statement given above we have used the OutputCache directive which includes two attributes using which we can modify the cache output.
The two attributes are:
VaryByParam
VaryByHeader
VaryByParam
The VaryByParam attribute specifies how a page should be cached when a request for that page contains parameters. A simple and straight forward implementation is the use of *. This directs that a cache be created for each of the form field parameters. However in cases where the parameters are many in number, it is advised to list the parameters separated by the semicolon, so that a cache is created for those parameters listed only. This will optimize the resouce usage on the server.
<%@ OutputCache Duration=”60” VaryByParam=”Country,State” %>
VaryByHeader
This attribute is used to overcome the problem of displaying the same cached content to all browsers. By using this attribute we can store different versions of the web page content for different browsers. We assign User.Agent value to this attribute. To identify the browser, we use the statement in the web page, as shown below.
Sub Page_Load
If Request.Browser.Brower =”Netscape” Then
// display content in bold
Else
// display content in italics
End If
End sub
From the above code, the User.Agent header identifies the type of browser being used to request the page. If the browser is Netscape, it display the content of a control in bold. Similarly we can alter the content to be displayed based on the type of browser. The various versions of a browser are also accommodated with this attribute.
Related articles
Caching multiple versions of a web page
Related topics
Caching Multiple Versions of a User Control using SetVaryByCustom()
Page fragment caching
Varying page fragment caching by parameter
Page data caching
Cache file dependencies