HttpContext.Cache和HttpRuntime.Cache

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


 

#回复: HttpContext.Cache和HttpRuntime.Cache 编辑
1、HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的。
2、HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。
2007-12-29 17:08:00 | [匿名:it民工]
#回复: HttpContext.Cache和HttpRuntime.Cache 编辑
这个测试等于废话!

还是这句话说得对!

在HttpContext里定义cache只是为了使用方便。在某些情况下,HttpContext还没被创建出来,就只能用HttpRuntime.Cache。
2007-08-20 11:21:00 | [匿名:sb]
#HttpContext.Cache和HttpRuntime.Cache 编辑
HttpContext.Cache和HttpRuntime.Cache
2007-06-21 23:55:00 | [匿名:mbskys]
#HttpRuntime.Cache 与HttpContext.Current.Cache的疑问 编辑
转载自蝈蝈俊.net 已经有人说过这个话题,相关链接: HttpRuntime.Cachevs.HttpContext.Current.Cachehttp://weblogs.asp.n...
2006-12-31 11:10:00 | [匿名:e旋风]
#HttpRuntime.Cache vs. HttpContext.Current.Cache 编辑
已经有人说过这个话题,相关链接: HttpRuntime.Cache vs. HttpContext.Current.Cachehttp://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx
2006-09-24 05:16:00 | [匿名:ASP.NET Chinese Blogs]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
补充一下,怎样使用不同的Session访问同一个网站。可以使用不同进程的IE,或者不同的浏览器,或者多个机器。
2006-07-25 10:29:00 | [匿名:ah]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
好像搂主说的不对。你的Demo代码无法下载,我也无法验证。

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

Msdn上的注释 是对的。

我自己测试过,如果使用不同的Session访问HttpContext.Cache,结果不一样;但是HttpRuntime.Cache 是一样的。



2006-07-25 10:26:00 | [匿名:ah]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
两个东西调用的对象确实是同一个,这个估计是为了方便调用的,但是HttpRuntime.Cache在Web程序里是去对能调用的,而HttpContext.Cache就不一定了,因为.net不是请求级别的程序,有些代码是在HttpContext.Current为空的情况下执行,这个时候就只能用HttpRuntime.Cache了
2006-01-12 09:22:00 | [匿名:Jeffrey]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
什么是cache 啊?
2005-09-02 21:49:00 | [匿名:qq]
#re:HttpContext.Cache和HttpRuntime.Cache 编辑
^_~,pretty good!csharpsseeoo
2005-05-18 12:53:00 | [匿名:照度计]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
两个Cache是一样的,只是同一个Cache的读取方法放在不同类中一样
就好像Page.Request 和Context.Request一样
我觉得Cache是Application的增强版,因为它除了具有Application的功能外,还有时间处理,关联等方法的集成
2005-04-19 10:10:00 | [匿名:netsoul]
#re:HttpContext.Cache和HttpRuntime.Cache 编辑
^_^,Pretty Good!
2005-04-16 04:54:00 | [匿名:细胞破碎仪]
#re:HttpContext.Cache和HttpRuntime.Cache 编辑
^_^,Pretty Good!
2005-04-10 20:10:00 | [匿名:乳匀机]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
一个应用程序如何调用另外一个应用程序的Cache?

在另一个应用程序中创建一个WebService.
通过调用该Service可以获得Cache.
2005-03-31 15:04:00 | [匿名:LiQiang]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
是啊,在Global.asax里面调用HttpContext.Cache是取不到东西的。这个也费了我半天才查出来,到最后不得不在Global类里加了个静态变量。
2005-03-07 10:14:00 | [匿名:urtracker]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
@Wang Ting
多数浏览器和控制项装置的URL 长度限制为255 个字符 ,最多也就1K。
2005-01-18 11:12:00 | [匿名:abin]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
@Wang Ting

session好用,但是依赖浏览器。

默认情况下,ASP.NET 使用 Cookie 来标识哪些请求属于特定的会话。如果 Cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。

URL的长度是有限制的。
2005-01-18 11:03:00 | [匿名:abin]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
System.Web.HttpContext:

public Cache get_Cache()
{
return HttpRuntime.Cache;
}

...

在HttpContext里定义cache只是为了使用方便。在某些情况下,HttpContext还没被创建出来,就只能用HttpRuntime.Cache。

@abin

“假设一下:如果一个论坛为应用登录用户缓存一些设置,如果没有设置缓存截止的时间,那么它占用的内存会一直变大…… ”

那些东西最好存session。developer要对自己写的代码负责,如果想troubleshooting memory leak,欢迎联系Microsoft Product Support Service..经常看到有人在cache里放几百m的dataset..

@lone

“一个应用程序如何调用另外一个应用程序的Cache? ”

impossible,除非你用caching application block..
2005-01-17 22:02:00 | [匿名:Wang Ting]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
HttpRuntime.Cache 还可用于自定义类访问Cachem,今天为了解决这个问题花了好多时间
2005-01-17 18:23:00 | [匿名:阿不]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
一台服务器上可不是一个Web Appliction
2005-01-17 12:39:00 | [匿名:abin]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
个人看法:相对于现在的服务器内存来说,缓存占用的内存并不算多,最重要看你是怎么用的。
:)
2005-01-16 18:41:00 | [匿名:宝玉]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
宝玉,
在MSDN上查了,这两个Cache都属于
System.Web.Caching.Cache。
结果自然就一样了。

但我有一点不明白,为什么要弄成一样呢,而且还是全局的,这样如果不设置缓存时间的话所有的变量都会存起来,直到运用程序被重新启动。
假设一下:如果一个论坛为应用登录用户缓存一些设置,如果没有设置缓存截止的时间,那么它占用的内存会一直变大……

看了要好好设置一下缓存时间和偏移量。
2005-01-16 15:28:00 | [匿名:abin]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
这个我不会:(
我想只要你取到应用程序的Cache对象即可。
2005-01-15 14:54:00 | [匿名:宝玉]
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
一个应用程序如何调用另外一个应用程序的Cache?
原文地址:https://www.cnblogs.com/daohuen/p/1575201.html