Click here to Skip to main content
13,146,742 members (91,659 online)
Click here to Skip to main content
Add your own
alternative version

Stats

2.8K views
1 bookmarked
Posted 11 Oct 2013

Cached Data Reference Pattern in ASP.Net

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
 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,

Editorial Note

This articles was originally at wiki.asp.net 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.

License

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

Share

About the Author

ASP.NET Community
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group

765 members


You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid