System.Web.HttpContext.Current.Cache 属性 ----->是个"属性值"

 System.Web.HttpRuntime.Cache 属性--------------->是个"属性值"

 System.Web.Caching.Cache  webCache=***-------->是个"类"

比如: protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;

==================================================

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

先来看看Msdn上的注释:
HttpRuntime.Cache:获取当前应用程序的 Cache。
HttpContext.Current.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.Current.Cache保存的Cache,HttpContext.Current.Cache和HttpRuntime.Cache都可以读取。
  2. HttpRuntime.Cache保存的Cache,HttpContext.Current.Cache和HttpRuntime.Cache都可以读取。
  3. 无论是哪个用户通过什么方式对Cache的改变,其他用户无论用什么方式读取的Cache内容也会随之变。

 转自:http://blog.joycode.com/dotey/archive/2005/01/15/43091.aspx

 

留言反馈:

===================================================

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

===================================================

这个测试等于废话!

还是这句话说得对!

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

===================================================

 已经有人说过这个话题,相关链接: HttpRuntime.Cache vs. HttpContext.Current.Cache
http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx

===================================================

 好像搂主说的不对。你的Demo代码无法下载,我也无法验证。

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

Msdn上的注释 是对的。

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

===================================================

 两个东西调用的对象确实是同一个,这个估计是为了方便调用的,但是HttpRuntime.Cache在Web程序里是去对能调用的,而HttpContext.Cache就不一定了,因为.net不是请求级别的程序,有些代码是在HttpContext.Current为空的情况下执行,这个时候就只能用HttpRuntime.Cache了

===================================================

 两个Cache是一样的,只是同一个Cache的读取方法放在不同类中一样
就好像Page.Request 和Context.Request一样
我觉得Cache是Application的增强版,因为它除了具有Application的功能外,还有时间处理,关联等方法的集成

===================================================

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

在另一个应用程序中创建一个WebService.
通过调用该Service可以获得Cache.

===================================================

是啊,在Global.asax里面调用HttpContext.Cache是取不到东西的。这个也费了我半天才查出来,到最后不得不在Global类里加了个静态变量。

===================================================

多数浏览器和控制项装置的URL 长度限制为255 个字符 ,最多也就1K。

===================================================

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

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

URL的长度是有限制的。

===================================================

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..

===================================================

HttpRuntime.Cache 还可用于自定义类访问Cachem,今天为了解决这个问题花了好多时间

===================================================

一台服务器上可不是一个Web Appliction

===================================================

个人看法:相对于现在的服务器内存来说,缓存占用的内存并不算多,最重要看你是怎么用的。

===================================================

在MSDN上查了,这两个Cache都属于
System.Web.Caching.Cache。
结果自然就一样了。


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

看了要好好设置一下缓存时间和偏移量。

===================================================

......

===================================================

 


 

posted on 2009-12-28 23:07  钱途无梁  阅读(3223)  评论(2编辑  收藏  举报