Click here to Skip to main content
14,238,591 members

Cached Data Reference Pattern in ASP.Net

Rate this:
0.00 (No votes)
Please Sign up or sign in to vote.
0.00 (No votes)
11 Oct 2013CPOL
 Whenever an attempt is made to access data from the cache, it should be with the assumption that the data might not be there any more. Thus,

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.


Whenever an attempt is made to access data from the cache, it should be with the assumption that the data might not be there any more. Thus, the following pattern should be universally applied to your access of cached data. In this case, we're going to assume the object that has been cached is a DataTable.

<span style="font-size:85%;">public DataTable GetCustomers(bool BypassCache)<br />{<br />  string cacheKey = "CustomersDataTable";<br />  object cacheItem = Cache[cacheKey] as DataTable;<br />  if((BypassCache) || (cacheItem == null))<br />  {<br />     cacheItem = GetCustomersFromDataSource();<br />     Cache.Insert(cacheKey, cacheItem, null,<br />     DateTime.Now.AddSeconds(GetCacheSecondsFromConfig(cacheKey),<br />     TimeSpan.Zero);<br />  }<br />  return (DataTable)cacheItem;<br />}<br /></span>

There are several points I'd like to make about this pattern:

  • Values, like cacheKey, cacheItem and the cache duration, are defined once and only once.
  • The cache can be bypassed as needed—for example, after registering a new customer and redirecting to a list of customers, it would probably be best to bypass the cache and repopulate it with the latest data, which would include the newly inserted customer.
  • Cache is only accessed once. This has performance benefits and ensures that NullReferenceExceptions don't occur because the item was present the first time it was checked but had expired before the second check.
  • The pattern uses strong type checking. The "as" operator in C# will attempt to cast an object to a type and will simply return null if it fails or if the object is null.
  • The duration is stored in a configuration file. All cache dependencies, whether file-based, time-based, or otherwise, should ideally be stored in a configuration file so that changes can be made and performance measured easily. I also recommend that a default cache duration be specified, and that the GetCacheSecondsFromConfig() method uses the default if no duration is specified for the cacheKey being used.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

760 members

Comments and Discussions

-- There are no messages in this forum --
Posted 11 Oct 2013


1 bookmarked