陋室铭
永远也不要停下学习的脚步(大道至简至易)

posts - 2169,comments - 570,views - 413万

Asp.Net中可以方便的使用缓存,对于Cache,一般有两种方式调用:HttpContext.Cache和HttpRuntime.Cache。那么这两种Cache有什么区别呢?

先来看看Msdn上的注释:
HttpRuntime.Cache:获取当前应用程序的 Cache。
HttpContext.Cache:为当前 HTTP 请求获取 Cache 对象。

那么是不是说对于HttpRuntime.Cache就是应用程序级,而HttpContext.Cache则是针对每个用户的呢?NO,而实际上,两者调用的是同一个对象。他们的区别仅仅在于调用方式不一样(就我所知)。

事实胜过雄辩,写个例子来证实一下:

        /**//// <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;         }
    通过这个例子可以很容易证明: 
  1. HttpContext.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。 
  2. HttpRuntime.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。 
  3. 无论是哪个用户通过什么方式对Cache的改变,其他用户无论用什么方式读取的Cache内容也会随之变。

 

HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的(其它类型也可以用,如控制台等)。
HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。

posted on   宏宇  阅读(544)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2006-12-30 XML技术上传文件
2006-12-30 XmlTextWriter创建XML文件
2006-12-30 XML、DataSet、DataGrid结合
2006-12-30 遍历指定文件夹下所有的xml文件并动态生成HTML页面!
2006-12-30 使用XML创建Excel文档
2006-12-30 XML初学者必读
2006-12-30 基于XML的下拉菜单(源代码)
< 2008年12月 >
30 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示