Caching in ASP.NET
In this article we will see how caching in ASP.Net is used to improve application performance.
Caching can improve application performance. Caching is more important for web applications since performance is an important factor in the case of web applications. In this article we will see how caching in ASP.Net is used to improve application performance.
Creating web applications often requires storing items in-memory so that they are easier to retrieve the next time they are requested. For example suppose that a web page is generated the first time it is requested and if we can save the generated web page output then we don't need to create the web page output again the next time it is requested thus saving time.
We can store the items on the web server or another location such as the web browser. ASP.NET provides many techniques for caching. If we have the ASP.Net page output that we want to store then we can use output caching. For storing the application data we can use application data caching. Both of these types of caching helps us create high-performance web applications since generated output or data doesn't need to be recreated. And if the data or output is expensive to create then it is always better to cache such an item. Caching is more suitable for items that are changed infrequently.
Using Application Data Caching we can store any arbitrary data that we want to cache using name/value pairs. An application cache is similar to an application state but an application cache is managed by ASP.Net such that ASP.Net automatically removes the items from the application cache whenever they expire or when memory is low. Therefore we should always check if the item is in the cache before accessing it to ensure that it has not been removed from the cache.
Using the Output Cache we can store the generated output of the ASP.Net page so that the page doesn't need to be processed again. This is especially beneficial if the page output is expensive to create, such as if the output fetches some values from the database. We can cache multiple versions of the page depending upon the request like the querystring values. To use output caching we use the OutputCache directive in the ASP.Net page. The following directive will store the various versions of the page output based on the querystring value for 60 seconds.
<%@ OutputCache VaryByParam="name" Duration="60" %>
Other attributes we can use with the OutputCache directive are:
- VaryByParam for storing multiple versions of the page output depending on the query string.
- VaryByControl for storing multiple versions of the page output depending on control value.
- VaryByHeader for storing multiple versions of the page output depending on the request's HTTP header.
- VaryByCustom for storing multiple versions of the page output depending on the custom string that you specify.This is useful for scenarios such as storing multiple versions of the page depending upon the browser.
ASP.NET can automatically remove the data from the cache depending on the following conditions:
- Server memory is low.
- The cache item has expired.
- The Cache dependency has changed.
When adding an item to the cache we can set its expiration using two different modes.
Absolute expiration specifies that the item is to be removed at a fixed time.
Sliding expiration specifies when to remove an item after it was last accessed.
When adding an item in the cache we can specify that the item's lifetime is dependent on a different component such as a table in a database or a file. We can implement a dependency on the database using the SqlCacheDependency class and use the CacheDependency class for implementing the dependency on a file or any other component.
Sometimes there are scenarios where we want to store only parts of the ASP.Net page instead of the entire page. We can implement such kinds of scenarios using fragment caching or control caching. For such a scenario we identify parts of the page that are expensive to create and create user controls for them and mark the user controls as cacheable using the OutputCache directive.
Instead of specifying cache settings on every ASP.Net page we can specify them once in the web.config file in the outputCacheProfiles section and apply it to all the pages we want to specify the settings. We can apply the cacheprofile to individual pages using the CacheProfile attribute of the @OutputCache directive.