在 asp.net 中锁定缓存的最佳方法是什么? [英] What is the best way to lock cache in asp.net?

查看:27
本文介绍了在 asp.net 中锁定缓存的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在某些情况下,例如长时间运行的进程,锁定 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# 中实现缓存锁定的最佳方式是什么?

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

推荐答案

这是基本模式:

  • 检查缓存中的值,如果可用则返回
  • 如果该值不在缓存中,则实施锁
  • 在锁里面,再次检查缓存,你可能被阻塞了
  • 执行值查找并缓存它
  • 解除锁定

在代码中,它看起来像这样:

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

}

这篇关于在 asp.net 中锁定缓存的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆