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

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

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

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

HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        /**//// <summary> HttpContext.Cache和HttpRuntime.Cache(转)        /// 通过HttpRuntime.Cache的方式来保存Cache HttpContext.Cache和HttpRuntime.Cache(转)        /// </summary> HttpContext.Cache和HttpRuntime.Cache(转)        private void btnHttpRuntimeCacheSave_Click(object sender, System.EventArgs e) HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        ...{ HttpContext.Cache和HttpRuntime.Cache(转)            HttpRuntime.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero); HttpContext.Cache和HttpRuntime.Cache(转)        } HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        /**//// <summary> HttpContext.Cache和HttpRuntime.Cache(转)        /// 通过HttpRuntime.Cache的方式来读取Cache HttpContext.Cache和HttpRuntime.Cache(转)        /// </summary> HttpContext.Cache和HttpRuntime.Cache(转)        private void btnHttpRuntimeCacheLoad_Click(object sender, System.EventArgs e) HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        ...{ HttpContext.Cache和HttpRuntime.Cache(转)            if (HttpRuntime.Cache[cacheKey] == null) HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)            ...{ HttpContext.Cache和HttpRuntime.Cache(转)                cacheContent = "No Cache"; HttpContext.Cache和HttpRuntime.Cache(转)            } HttpContext.Cache和HttpRuntime.Cache(转)            else HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)            ...{ HttpContext.Cache和HttpRuntime.Cache(转)                cacheContent = (string)HttpRuntime.Cache[cacheKey]; HttpContext.Cache和HttpRuntime.Cache(转)            } HttpContext.Cache和HttpRuntime.Cache(转)            lblCacheContent.Text = cacheContent; HttpContext.Cache和HttpRuntime.Cache(转)        } HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        /**//// <summary> HttpContext.Cache和HttpRuntime.Cache(转)        /// 通过HttpContext.Cache的方式来保存Cache HttpContext.Cache和HttpRuntime.Cache(转)        /// </summary> HttpContext.Cache和HttpRuntime.Cache(转)        private void btnHttpContextCacheSave_Click(object sender, System.EventArgs e) HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        ...{ HttpContext.Cache和HttpRuntime.Cache(转)            HttpContext.Current.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero); HttpContext.Cache和HttpRuntime.Cache(转)        } HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        /**//// <summary> HttpContext.Cache和HttpRuntime.Cache(转)        /// 通过HttpContext.Cache的方式来读取Cache HttpContext.Cache和HttpRuntime.Cache(转)        /// </summary> HttpContext.Cache和HttpRuntime.Cache(转)        private void btnHttpContextCacheLoad_Click(object sender, System.EventArgs e) HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)        ...{ HttpContext.Cache和HttpRuntime.Cache(转)            if (HttpContext.Current.Cache[cacheKey] == null) HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)            ...{ HttpContext.Cache和HttpRuntime.Cache(转)                cacheContent = "No Cache"; HttpContext.Cache和HttpRuntime.Cache(转)            } HttpContext.Cache和HttpRuntime.Cache(转)            else HttpContext.Cache和HttpRuntime.Cache(转)HttpContext.Cache和HttpRuntime.Cache(转)            ...{ HttpContext.Cache和HttpRuntime.Cache(转)                cacheContent = (string)HttpContext.Current.Cache[cacheKey]; HttpContext.Cache和HttpRuntime.Cache(转)            } HttpContext.Cache和HttpRuntime.Cache(转)            lblCacheContent.Text = cacheContent; HttpContext.Cache和HttpRuntime.Cache(转)        }
    通过这个例子可以很容易证明: 
  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 应用。

相关文章:

  • 2021-06-02
  • 2022-12-23
  • 2021-10-04
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-08
  • 2021-12-30
  • 2021-12-31
  • 2022-12-23
  • 2021-06-13
  • 2021-08-30
  • 2022-01-03
相关资源
相似解决方案