什么是锁定缓存asp.net的最佳途径?缓存、途径、asp、net

2023-09-02 01:28:42 作者:2.Ronin(浪人)

我知道在某些情况下,如长时间运行的进程,它锁定ASP.NET缓存以避免从再次执行的长期过程,而不是击中缓存的该资源的另一个用户的后续请求是重要的。

I know in certain circumstances, such as long running processes, it is important to lock ASP.NET cache in order to avoid subsequent requests by another user for that resource from executing the long process again instead of hitting the cache.

什么是用C#实现缓存锁定在ASP.NET中最好的方法是什么?

What is the best way in c# to implement cache locking in ASP.NET?

推荐答案

下面是基本的模式:

检查缓存的值,如果其可用返回 如果该值不在高速缓存中,则执行锁定 里面的锁,再次检查缓存,你可能已经被封锁 执行值查找并缓存它 解除锁定

在code,它看起来是这样的:

In code, it looks like this:

private static object ThisLock = new object();

public string GetFoo()
{

  // try to pull from cache here

  lock (ThisLock)
  {
    // cache was empty before we got the lock, check again inside the lock

    // cache is still empty, so retreive the value here

    // store the value in the cache here
  }

  // return the cached value here

}