Understanding Caching in ASP.Net
Caching is a feature in ASP.Net that is very useful in creating dynamic web pages. You know that dynamic web pages contain content that is constant changing and they may even vary according to the users. For example a simple query to return a list of products from an online store will return the products based on the query the user inputs.
would have requested the products from a particular manufacturer or products
within a particular range of price. The output of such queries will vary
on the users input to the query engine.
In simpler terms the cached page is stored in the memory of the server and this page is served from the memory without generating the page. Thus faster response is provided for the requests made to a cached page.
Caching can be done in to three types. One is Page caching, which enables you to cache the entire page, the other is caching the page elements, and the Data caching. When you cache the page elements, some of the elements of the page are cached. In data caching the data that is available in the page is cached.
In Page caching, the entire page is cached. To achieve this you have to use the @OutputCache directive at the top of the page that is to be cached. The syntax of this directive is as given below:
<%@ OutputCache Duration=6 VaryByParam="id" VaryByCustom="browser" %>
The attributes of this directive are Duration, VaryByParam, and VaryByCustom. The Duration directive is used to specific how long the output of the page should be cached. After the specified duration in this attribute the cache of this page is removed and the page will be generated for the next request. The value of the Duration attribute is in seconds. Let us say that you have specified 6 as the value for the Duration attribute.
Then the page output will be cached for 6 seconds before it is removed from the cache. After 6 seconds the page will be generated for the next request. The VaryByParam attribute specifies the querystring parameters to vary the cache. This is a compulsory attribute.
If the value of this attribute is None then the page output that is delivered is the same whatever be the parameters for the querystring. For example if you specify the value of the VaryByParam value as category, then the request to this page will be given an output based on the query string category. The directive for such a page will look like,
<%@ OutputCache Duration=6 VaryByParam="category" %>
and the request to the page would look something like,
The VaryByCustom tag is used to display content based on the browser that is used by the user. This is used to give page output for different browsers in a different manner.
You might know that the header and the footer for a web page do not change in most of the web pages. To just cache the header and the footer you can use techniques called Fragment caching. This allows a particular part of the web page to be served from the cache.
For fragment caching you have to put the content that is to be cached in a user control and give the caching directive at the top of the user control and not in the page that contains the user control. This caches the user control for a particular duration while the page serves the dynamic content.
The above code stores the company name in the cache. To retrieve this value you have to first check whether that object is available in the cache and then use it.
You can also use the Add methods or the Insert methods of the Cache class to insert objects into the Cache class. The following example show you how to use the insert method of the cache class.
The first two parameters of the insert method are key and the object that have to be inserted into the cache. The third parameter is called the CacheDependency that can be used to set dependency to a file. Null indicates that there is no dependency.
The next parameter specifies the time at which the object has to be removed from the cache. The last parameter is called the sliding expiration parameter which shows the time interval after which the object is to be removed from the memory.
Thus by using
the Caching features of ASP.Net you can provide powerful dynamic pages
in a web application which provides faster response to the requested pages.
| Design Guideline for C# Structs (C Sharp) | Design Patterns Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? | The Benefits of Using ASP.NET Core for Web Development: A Guide for Businesses