Scott Cate Weblog 

scott.cate@myKB.com
http://scottcate.myKB.com



Scott Cate KB > ASP.NET Code Samples - Findings > ASP.NET Class Start Over
Search the Knowledge Base
 
Start Search in the Following Category
Date Modified
Saturday, October 06, 2007
Steve Smith's Awesome Caching Pattern

My friend Steve Smith who wrote this article has solved a major problem for me. With his permission I'm duplicating some of his code here.

About one in 50,000 page views, I was getting an "Object Not Set" exception being thrown in kbAlertz.com. This was a mystery for a very long time, and it's finally solved (Thanks Steve :). Here is ( IMHO ) how the typical developer retreives something from the cache in an ASP.NET application.

1:     HttpContext ctx = HttpContext.Current;

2: if(ctx.Cache["myCacheDataSet"] != null)
3: {
4: DataSet ds = (DataSet)ctx.Cache["myCacheDataSet"];
5: return ds;
6: }
7: else
8: {
9: //Go build myCacheItem
10: DataSet ds = new DataSet;
11: //Set ds Properties
12: ctx.Cache.Insert("myCacheDataSet",ds,null,DateTime.Now.AddHours(1), TimeSpan.Zero);
13: return ds;
14: }

Steve Smith suggests that you should actually create your object, from the cache, and then check your object != null. This means that you only hit the cache object once, vs. the above method of checking the if the cache item is there, and if so, then hit the cache again, and get your object. Once in a blue moon, the object acually falls from cache in the time between (Is it there = true) and (ok it's there, give it to me), thereby creating a null reference exception.

1:     //Steve Smith's Caching Pattern

2: Object cacheItem = Cache[key] as DataTable;
3: if(cacheItem == null)
4: {
5: cacheItem = GetData();
6: Cache.Insert(key, cacheItem, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
7: }
8: return (DataTable)cacheItem;

So I built a couple of static helper methods that will let me easily get object from the cache.

1:     /// <summary>

2: /// All objects being pulled from Cache should be used by this method. This method
3: /// is code safe, and ensures if an object is in the cache, it's used, and it can't
4: /// (or won't) be removed before the reference is set to it. This was causing random
5: /// Null Exceptions throughout code, because if you check for Cache["Whatever"] != null
6: /// and then set string whatever = Cache["Whatever"].ToString(), there is a possibility
7: /// that the cached item, could expire and not be available by the time the reference is set.
8: /// </summary>
9: /// <param name="CacheName">String Name of Cached Item</param>
10: /// <returns>Object: If object is not found, then null is returned, so ALWAYS check for != null</returns>
11: public static object RetrieveObjectFromCache(string CacheName, HttpContext ctx)
12: {
13: object cacheItem = ctx.Cache[CacheName.ToLower()];
14: return cacheItem;
15: }
16: public static object RetrieveObjectFromCache(string CacheName)
17: {
18: object cacheItem = Global.RetreiveObjectFromCache(CacheName, HttpContext.Current);
19: return cacheItem;
20: }
21:

And Finally, I would use these static methods through out my code like the following ...

1:     DataSet dsTmp = (DataSet)Global.RetrieveObjectFromCache("myCachedDataSet");

2: if(dsTmp != null)
3: {
4: //DataSet found
5: }
6: else
7: {
8: //No DataSet in Cache
9: }



All rights reserved. All details are the personal opinion of Scott Cate.
All trademarks referenced are the property of their respective owners.
Scott Cate is a lead programmer for www.myKB.com and
owner of The Arizona .NET User Group and an all around nice guy ;)


Knowledge Base Software - myKB.com

 

Cameron Cate Pictures
Cameron Cate Pix

Site Navigation

Home
Knowledge Base
Wiki Discussions
Photo Album

Sites I Own & Run


Easy Search ASP.NET
mykb.com knowledge base software logo
myKB.com
Knowledgebase Software

KBAlertz.com
Arizona .NET user Group
Group Leader

Affiliations


ASP.NET MVP
2004
2005
2006
2007
2008
2009


Blog Sites I Read

Brady Gaster
Rob Howard
G. Andrew  Duthie
Robert McLaws
Alex Lowe

World of Scott.Net

Scott Guthrie
  -  Father of ASP.NET

Scott Watermasyk
  - .Text (Weblog)

Scott Sargent
Scott Mitchell
ScottG.net
Scott Bellware
Scott Forsyth
Scott Hanselman
Scott Cate

Favorite Books


ASP.NET Cookbook


First Looks @ ASP.NET 2.0