Joydip Kanjilal
Contributor

Best practices in caching in ASP.Net

opinion
Aug 27, 20154 mins
Software Development

Caching enables the web page to be rendered faster, and proper use of caching minimizes or reduces database hits or consumption of server's resources

Caching is a state management strategy often adopted in ASP.Net to improve the application’s performance by minimizing the consumption of resources in your system. If used properly, it can improve the performance of your application considerably by storing the wWb page in its entirety or partially, or even store the application’s data across the HTTP requests. Caching enables the Web page to be rendered faster, and proper use of caching minimizes or reduces database hits or consumption of server’s resources.

Caching in ASP.Net is of the following three types:

  1. page output caching
  2. page fragment caching
  3. data caching

Page output caching

This is a form of caching in ASP.Net that stores a copy of your Web page in the memory cache so that subsequent requests for the same Web page can be fetched directly from the cache — the cached output is sent to the application. This improves the application’s performance considerably. The following code snippet shows how you can implement page output caching.

<%@ OutputCache Duration="30" VaryByParam="*" %>

The VaryByParam option helps you to specify the variables in the Http Request that would need a new cache entry. Other possible options include: VaryByHeader and VaryByCustom. You can also specify Location and Duration in the OutputCache directive — you can use these to specify the location of the cache and also the duration for which the Web page should be cached respectively.

Page fragment caching

Page fragment caching is a caching strategy in which the Web page is cached partially – only fragments of the Web page are cached, not the entire Web page. You can use the same syntax as page output caching. However, you need to apply the OutputCache attribute to a user control instead of the Web page. Fragment caching is helpful when you would need to cache only portions of your Web page — typically in situations where your Web page contains a mix of common and dynamic sections. As an example, you can have a Web page that contains a mix of menu items and also certain dynamic sections that need to be populated and updated from the database often.

Data caching

ASP.Net exposes the Cache API for you to store data in the cache for retrieval later. The syntax for storing data in the Cache using the Cache API is given below.

Cache["key"] = "value";

You can also use the Add or the Insert methods. To remote an entry from the cache, you can use the Remove() method of the Cache class. The Insert() method of the Cache class enables you to specify cache dependency. Cache dependency is a strategy that ensures that when the data in the data store (from which the cache has been populated) changes, the cache would then be re-populated immediately. When data in the data store changes, the cache would expire, resulting in a re-populating of the cache with the latest data. You can read more on this from this MSDN article.

Best practices

You should cache as often as you can and cache data properly in every layer of your application. When using data caching, you should implement a proper strategy to ensure that data in the cache is in sync with that in the data store. You can take advantage of distributed cache managers like Memcached so that your caching strategy can also scale well and provide considerable performance gains — you can use Memcached to store large data. You should ensure that you cache relatively stale data only — there isn’t any point in caching data that would change often over time. Also, data that is unlikely to be reused should not be stored in the cache. You shouldn’t over-use SqlDependency or SqlCacheDependency.

And now, let’s know the downsides of caching too. The cache object is available to the current application domain only. So, if you would like to store data in the cache and make it accessible across a web farm, that isn’t a possibility. You would have to leverage distributed cache like Windows Server AppFabric Caching or other distributed caching frameworks to have the data in the cache globally accessible in a Web farm.

Caching is a powerful mechanism to boost the application’s performance by storing relatively stale data in the memory so that the same can be retrieved from the cache memory at a later point of time. I’ll discuss more on this topic with real life code examples in my future posts here.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author