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

查看:258
本文介绍了什么是锁定缓存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#实现缓存锁定在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

}

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

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