HttpContext.Cache和HttpRuntime.Cache
来源:互联网 发布:淘宝新品上架下栏文字 编辑:程序博客网 时间:2024/04/30 07:22
ASP.Net中可以方便的使用缓存,对于Cache,一般有两种方式调用:HttpContext.Cache和HttpRuntime.Cache。那么这两种Cache有什么区别呢?
先来看看Msdn上的注释:
HttpRuntime.Cache:获取当前应用程序的 Cache。
HttpContext.Cache:为当前 HTTP 请求获取 Cache 对象。
那么是不是说对于HttpRuntime.Cache就是应用程序级,而HttpContext.Cache则是针对每个用户的呢?NO,而实际上,两者调用的是同一个对象。他们的区别仅仅在于调用方式不一样(就我所知)。
事实胜过雄辩,写个例子来证实一下(限于篇幅仅贴出关键代码,完整代码见附件WebDemo.rar):
/**//// <summary> /// 通过HttpRuntime.Cache的方式来保存Cache /// </summary> private void btnHttpRuntimeCacheSave_Click(object sender, System.EventArgs e) ...{ HttpRuntime.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero); } /**//// <summary> /// 通过HttpRuntime.Cache的方式来读取Cache /// </summary> private void btnHttpRuntimeCacheLoad_Click(object sender, System.EventArgs e) ...{ if (HttpRuntime.Cache[cacheKey] == null) ...{ cacheContent = "No Cache"; } else ...{ cacheContent = (string)HttpRuntime.Cache[cacheKey]; } lblCacheContent.Text = cacheContent; } /**//// <summary> /// 通过HttpContext.Cache的方式来保存Cache /// </summary> private void btnHttpContextCacheSave_Click(object sender, System.EventArgs e) ...{ HttpContext.Current.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero); } /**//// <summary> /// 通过HttpContext.Cache的方式来读取Cache /// </summary> private void btnHttpContextCacheLoad_Click(object sender, System.EventArgs e) ...{ if (HttpContext.Current.Cache[cacheKey] == null) ...{ cacheContent = "No Cache"; } else ...{ cacheContent = (string)HttpContext.Current.Cache[cacheKey]; } lblCacheContent.Text = cacheContent; }
- 通过这个例子可以很容易证明:
- HttpContext.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。
- HttpRuntime.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。
- 无论是哪个用户通过什么方式对Cache的改变,其他用户无论用什么方式读取的Cache内容也会随之变。
- HttpContext.Cache和HttpRuntime.Cache
- HttpContext.Cache 和 HttpRuntime.Cache
- HttpContext.Cache和HttpRuntime.Cache
- HttpContext.Cache和HttpRuntime.Cache
- HttpContext.Cache和HttpRuntime.Cache
- HttpContext.Cache和HttpRuntime.Cache中的Cache
- HttpContext.Current.Cache 和 HttpRuntime.Cache
- HttpContext.Cache和HttpRuntime.Cache的区别
- HttpContext.Cache和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和 HttpRuntime.Cache
- HttpContext.Current.Cache 和 HttpRuntime.Cache
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache和HttpRuntime.Cache的区别
- HttpRuntime.Cache vs. HttpContext.Current.Cache
- HttpRuntime.Cache vs. HttpContext.Current.Cache
- HttpRuntime.Cache vs. HttpContext.Current.Cache
- 配置cisco pix防火墙的syslog
- 在PHP中使用MySQL扩展库处理结果集
- 三小时快速打造百度百科四级核心帐号
- 三种排序方法
- 求多边形或轮廓的凸包(Hull)
- HttpContext.Cache和HttpRuntime.Cache
- 如何将一个已有的vdi文件attach到一个虚拟机上
- predicates (from functional.h)
- SSH整合
- C语言判断闰年问题
- webbrowser 修改页面元素操作
- TLD视觉跟踪技术解析
- 前景检测算法-SACON(SAMPLE CONSENSUS)
- Session丢失的解决办法小结