看了这篇文章HttpContext.Cache和HttpRuntime.Cache
然后用.NET Reflector查看了源码:HttpContext类中返回的就是HttpRuntime.Cache
public Cache Cache
{
get
{
return 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; }
- 通过这个例子可以很容易证明:
- HttpContext.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。
- HttpRuntime.Cache保存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以读取。
- 无论是哪个用户通过什么方式对Cache的改变,其他用户无论用什么方式读取的Cache内容也会随之变。
#回复: HttpContext.Cache和HttpRuntime.Cache 编辑
这个测试等于废话!
还是这句话说得对!
在HttpContext里定义cache只是为了使用方便。在某些情况下,HttpContext还没被创建出来,就只能用HttpRuntime.Cache。
还是这句话说得对!
在HttpContext里定义cache只是为了使用方便。在某些情况下,HttpContext还没被创建出来,就只能用HttpRuntime.Cache。
#HttpContext.Cache和HttpRuntime.Cache 编辑
HttpContext.Cache和HttpRuntime.Cache
#HttpRuntime.Cache 与HttpContext.Current.Cache的疑问 编辑
转载自蝈蝈俊.net 已经有人说过这个话题,相关链接: HttpRuntime.Cachevs.HttpContext.Current.Cachehttp://weblogs.asp.n...
#HttpRuntime.Cache vs. HttpContext.Current.Cache 编辑
已经有人说过这个话题,相关链接: HttpRuntime.Cache vs. HttpContext.Current.Cachehttp://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
补充一下,怎样使用不同的Session访问同一个网站。可以使用不同进程的IE,或者不同的浏览器,或者多个机器。
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
好像搂主说的不对。你的Demo代码无法下载,我也无法验证。
先来看看Msdn上的注释:
HttpRuntime.Cache:获取当前应用程序的 Cache。
HttpContext.Cache:为当前 HTTP 请求获取 Cache 对象。
Msdn上的注释 是对的。
我自己测试过,如果使用不同的Session访问HttpContext.Cache,结果不一样;但是HttpRuntime.Cache 是一样的。
先来看看Msdn上的注释:
HttpRuntime.Cache:获取当前应用程序的 Cache。
HttpContext.Cache:为当前 HTTP 请求获取 Cache 对象。
Msdn上的注释 是对的。
我自己测试过,如果使用不同的Session访问HttpContext.Cache,结果不一样;但是HttpRuntime.Cache 是一样的。
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
两个东西调用的对象确实是同一个,这个估计是为了方便调用的,但是HttpRuntime.Cache在Web程序里是去对能调用的,而HttpContext.Cache就不一定了,因为.net不是请求级别的程序,有些代码是在HttpContext.Current为空的情况下执行,这个时候就只能用HttpRuntime.Cache了
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
什么是cache 啊?
#re:HttpContext.Cache和HttpRuntime.Cache 编辑
^_~,pretty good!csharpsseeoo
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
两个Cache是一样的,只是同一个Cache的读取方法放在不同类中一样
就好像Page.Request 和Context.Request一样
我觉得Cache是Application的增强版,因为它除了具有Application的功能外,还有时间处理,关联等方法的集成
就好像Page.Request 和Context.Request一样
我觉得Cache是Application的增强版,因为它除了具有Application的功能外,还有时间处理,关联等方法的集成
#re:HttpContext.Cache和HttpRuntime.Cache 编辑
^_^,Pretty Good!
#re:HttpContext.Cache和HttpRuntime.Cache 编辑
^_^,Pretty Good!
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
一个应用程序如何调用另外一个应用程序的Cache?
在另一个应用程序中创建一个WebService.
通过调用该Service可以获得Cache.
在另一个应用程序中创建一个WebService.
通过调用该Service可以获得Cache.
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
是啊,在Global.asax里面调用HttpContext.Cache是取不到东西的。这个也费了我半天才查出来,到最后不得不在Global类里加了个静态变量。
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
@Wang Ting
多数浏览器和控制项装置的URL 长度限制为255 个字符 ,最多也就1K。
多数浏览器和控制项装置的URL 长度限制为255 个字符 ,最多也就1K。
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
@Wang Ting
session好用,但是依赖浏览器。
默认情况下,ASP.NET 使用 Cookie 来标识哪些请求属于特定的会话。如果 Cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
URL的长度是有限制的。
session好用,但是依赖浏览器。
默认情况下,ASP.NET 使用 Cookie 来标识哪些请求属于特定的会话。如果 Cookie 不可用,则可以通过将会话标识符添加到 URL 来跟踪会话。
URL的长度是有限制的。
#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..
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..
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
HttpRuntime.Cache 还可用于自定义类访问Cachem,今天为了解决这个问题花了好多时间
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
一台服务器上可不是一个Web Appliction
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
个人看法:相对于现在的服务器内存来说,缓存占用的内存并不算多,最重要看你是怎么用的。
:)
:)
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
宝玉,
在MSDN上查了,这两个Cache都属于
System.Web.Caching.Cache。
结果自然就一样了。
但我有一点不明白,为什么要弄成一样呢,而且还是全局的,这样如果不设置缓存时间的话所有的变量都会存起来,直到运用程序被重新启动。
假设一下:如果一个论坛为应用登录用户缓存一些设置,如果没有设置缓存截止的时间,那么它占用的内存会一直变大……
看了要好好设置一下缓存时间和偏移量。
在MSDN上查了,这两个Cache都属于
System.Web.Caching.Cache。
结果自然就一样了。
但我有一点不明白,为什么要弄成一样呢,而且还是全局的,这样如果不设置缓存时间的话所有的变量都会存起来,直到运用程序被重新启动。
假设一下:如果一个论坛为应用登录用户缓存一些设置,如果没有设置缓存截止的时间,那么它占用的内存会一直变大……
看了要好好设置一下缓存时间和偏移量。
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
这个我不会:(
我想只要你取到应用程序的Cache对象即可。
我想只要你取到应用程序的Cache对象即可。
#re: HttpContext.Cache和HttpRuntime.Cache 编辑
一个应用程序如何调用另外一个应用程序的Cache?
2、HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。