过去,我一直在锁定访问HttpRuntime.Cache机制。
我不确定过去是否真的研究过这个问题,盲目地用锁将其包围。

您认为这真的有必要吗?

最佳答案

本文建议应使用锁:

http://msdn.microsoft.com/en-us/magazine/cc500561.aspx

引用:



这是他们的代码段:

// check for cached results
object cachedResults = ctx.Cache["PersonList"];
ArrayList results = new ArrayList();

if  (cachedResults == null)
{
  // lock this section of the code
  // while we populate the list
  lock(lockObject)
  {
    cachedResults = ctx.Cache["PersonList"];
    // only populate if list was not populated by
    // another thread while this thread was waiting
    if (cachedResults == null)
    {
      cachedResults = ...
      ctx.Cache["PersonList"] = cachedResults;
    }
  }
}

我尚未测试过此代码,但是我很想听到有人在生产环境中评估过这种方法。

09-30 17:18