ASP.NET中各种缓存技术的特点及使用场景
2014-03-28 17:01
519 查看
对于一些不经常改变却经常被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)."
缓存一般实现在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)."
相关文章推荐
- ASP.NET中各种缓存技术的特点及使用场景
- 一起谈.NET技术,ASP.NET缓存初探 使用得当是关键
- ASP.Net使用缓存技术提高效率
- 各种缓存技术及使用场景
- [转]使用asp.net的缓存技术提高站点性能
- ASP.Net使用缓存技术提高效率
- 使用asp.net的缓存技术提高站点性能
- 如何通过使用 VisualC # .NET 在 ASP.NET 缓存
- asp.net页面缓存技术
- CacheDependency、SqlCacheDependency Asp.net 2.0和Sql的缓存管理和使用
- 一起谈.NET技术,ASP.NET缓存分析和实践浅析
- asp.net中缓存的使用介绍一
- ASP.NET之缓存技术点滴
- ASP.NET使用Memcached高缓存实例(初级教程)
- 浅谈ASP.NET 缓存技术
- 浅谈缓存技术在ASP.NET中的运用
- ASP.NET中使用验证码技术
- ASP.NET MVC 缓存使用示例
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- ASP.NET缓存技术学习入门---页面缓存(OutPut Caching)