您的位置:首页 > 编程语言 > ASP

ASP.NET中各种缓存技术的特点及使用场景

2014-03-28 17:01 381 查看
  对于一些不经常改变却经常被request的数据,我们喜欢将它们缓存在内存。这样用户请求时先到缓存中去取,如果缓存中没有,再去数据库拿,提高响应速度。缓存一般实现在BLL,这样可以与DAL分离,更换数据库源时也无需改变缓存逻辑。

 ASP.NET中常用的缓存技术有以下几种:

  1. A per request cache using HttpContext.Items

  2. A session cache using HttpContext.Session

  3. An application cache using HttpContext.Cache or HttpRuntime.Cache

  HttpContext.Current.Items的生命周期极短,尽在一次request中有效,当此次request结束后,Items被清空。所以可以用在HttpModel和HttpHandler之间通信上。关于HttpModel和HttpHandler的概念可以参看:/article/5073212.html

  HttpContext.Current.Session在一次会话总有效,JSP中Session的生命周期时间可以在web.xml配置. 默认30分钟 ,也有很多人说Session的生命周期即浏览器打开到关闭的时间。所以,session最适合用来保存用户登录名和密码等。

  HttpContxt.Current.Cache和HttpRuntime.Cache生命周期最长,和整个应用的生命周期等长。可以用来存一些公共数据,比如上文中提到的这种应用场景就适合使用。

  值得注意的一点是HttpContext.Current.Cache 和 HttpRuntime.Cache 的区别,以下英文摘自某网站:

  "HttpRuntime.Cache is the recommended technique.

  Calling the HttpContext does some additional look-ups as it has to resolve the current context relative to the running thread.

  I use HttpContext.Current in a lot of the code I write too; but touching it as little as possible. Rather than calling HttpContext.Current repeatedly
it's best to hang onto a reference and pass it around (when possible)."
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: