Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This is sort of a follow up question on http://stackoverflow.com/questions/754661/httpruntime-cache-best-practices/11431198 where a reply from frankadelic contains this quote and code sample from http://msdn.microsoft.com/en-us/magazine/cc500561.aspx:

This is sort of a follow up question on https://stackoverflow.com/questions/754661/httpruntime-cache-best-practices/11431198 where a reply from frankadelic contains this quote and code sample from http://msdn.microsoft.com/en-us/magazine/cc500561.aspx:

appended answer 13772 as supplemental
Source Link
sepp2k
  • 9k
  • 2
  • 39
  • 51

Ok, so I'm going ahead with this code instead, where I've added one row that in my opinion must have been missing from the MSDN sample... Any feedback on this?

private static object _someDataCacheLock = new object();
public List<object> SomeData
{
 get
 {
 HttpContext ctx = HttpContext.Current;
 List<object> cachedResults = ctx.Cache["SomeData"] as List<object>;
 if (cachedResults == null)
 {
 // lock this section of the code
 // while we populate the list
 lock (_someDataCacheLock)
 {
 //This row was missing in the MSDN sample, I believe...
 cachedResults = ctx.Cache["SomeData"] as List<object>;
 // only populate if list was not populated by
 // another thread while this thread was waiting
 if (cachedResults == null)
 {
 cachedResults = GetSomeData(); //db access inside GetSomeData()
 ctx.Cache.Insert("SomeData",
 cachedResults,
 null, DateTime.Now.AddMinutes(10),
 TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
 }
 }
 }
 return cachedResults;
 }
}

Ok, so I'm going ahead with this code instead, where I've added one row that in my opinion must have been missing from the MSDN sample... Any feedback on this?

private static object _someDataCacheLock = new object();
public List<object> SomeData
{
 get
 {
 HttpContext ctx = HttpContext.Current;
 List<object> cachedResults = ctx.Cache["SomeData"] as List<object>;
 if (cachedResults == null)
 {
 // lock this section of the code
 // while we populate the list
 lock (_someDataCacheLock)
 {
 //This row was missing in the MSDN sample, I believe...
 cachedResults = ctx.Cache["SomeData"] as List<object>;
 // only populate if list was not populated by
 // another thread while this thread was waiting
 if (cachedResults == null)
 {
 cachedResults = GetSomeData(); //db access inside GetSomeData()
 ctx.Cache.Insert("SomeData",
 cachedResults,
 null, DateTime.Now.AddMinutes(10),
 TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
 }
 }
 }
 return cachedResults;
 }
}
private static object _someDataCacheLock = new object();
private static object _cachedResults;
public List<object> SomeData
{
 get
 {
 HttpContext ctx = HttpContext.Current;
 _cachedObject_cachedResults = ctx.Cache["SomeData"] as List<object>;
 if (_cachedResults == null)
 {
 // lock this section of the code
 // while we populate the list
 lock (_someDataCacheLock)
 {
 // only populate if list was not populated by
 // another thread while this thread was waiting
 if (_cachedResults== null)
 {
 _cachedResults= GetSomeData(); //db access inside GetSomeData()
 ctx.Cache.Insert("SomeData",
 _cachedResults,
 null, DateTime.Now.AddMinutes(10),
 TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
 }
 }
 }
 return _cachedResults;
 }
}
private static object _someDataCacheLock = new object();
private static object _cachedResults;
public List<object> SomeData
{
 get
 {
 HttpContext ctx = HttpContext.Current;
 _cachedObject = ctx.Cache["SomeData"] as List<object>;
 if (_cachedResults == null)
 {
 // lock this section of the code
 // while we populate the list
 lock (_someDataCacheLock)
 {
 // only populate if list was not populated by
 // another thread while this thread was waiting
 if (_cachedResults== null)
 {
 _cachedResults= GetSomeData(); //db access inside GetSomeData()
 ctx.Cache.Insert("SomeData",
 _cachedResults,
 null, DateTime.Now.AddMinutes(10),
 TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
 }
 }
 }
 return _cachedResults;
 }
}
private static object _someDataCacheLock = new object();
private static object _cachedResults;
public List<object> SomeData
{
 get
 {
 HttpContext ctx = HttpContext.Current;
 _cachedResults = ctx.Cache["SomeData"] as List<object>;
 if (_cachedResults == null)
 {
 // lock this section of the code
 // while we populate the list
 lock (_someDataCacheLock)
 {
 // only populate if list was not populated by
 // another thread while this thread was waiting
 if (_cachedResults== null)
 {
 _cachedResults= GetSomeData(); //db access inside GetSomeData()
 ctx.Cache.Insert("SomeData",
 _cachedResults,
 null, DateTime.Now.AddMinutes(10),
 TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
 }
 }
 }
 return _cachedResults;
 }
}
Tweeted twitter.com/#!/StackCodeReview/status/225131664366510081
Corrected code sample
Source Link
Loading
Source Link
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /