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

对于一些不经常改变却经常被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的概念可以参看:http://www.cnblogs.com/stwyhm/archive/2006/08/09/471729.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)."


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值