Register   Login
     
  Categories  
  Archives  
  Authors  
  Blog  
26

Late last week I fixed a particular interesting issue (for News Articles) that is only exposed under heavy loads.

It seems that I wasn't alone however, the pattern is commonly used in core code, and goes something like this:-

If (DataCache.GetCache("object") Is Nothing)
   Dim obj = code for loading config from files
   
DataCache.SetCache("object", obj)
End If
Return DataCache.GetCache("object")

When a more efficient approach should be:-

Dim obj = DataCache.GetCache("object")
If obj Is Nothing
   obj = code for loading config from files
   
DataCache.SetCache("object", obj)
End If
Return obj

You can find out more about the issue here. Kudos to james3838 from the DNN forums for picking it up. It should significantly reduce any exceptions appearing in sites with significant load.

Post Rating

Comments

Steve Sheldon
# Steve Sheldon
Thursday, March 16, 2006 1:16 PM
You should look at this:
http://weblogs.asp.net/ssmith/archive/2003/06/20/9062.aspx

He does it just slightly different, and I think it adds the benefit of not failing if it can't cast the cached data.

Object cacheItem = Cache[key]

as DataTable;

if(cacheItem == null)
{

cacheItem = GetData();

Cache.Insert(key, cacheItem, null, DateTime.Now.AddHours(1), TimeSpan.Zero);

}

return (DataTable)cacheItem;

Post Comment

Name (required)

Email (required)

Website

Enter the code shown above: