HttpContext.Cache 和 HttpRuntime.Cache

来源:互联网 发布:mac air 是retina屏吗 编辑:程序博客网 时间:2024/04/30 05:46

        首先先看看MSDN上对HttpContextHttpRuntime 的定义,

  1. HttpContext:   封装有关个别 HTTP 请求的所有 HTTP 特定的信息。
  2. HttpRuntime:  为当前应用程序提供一组 ASP.NET 运行时服务

        从定义看一个是针对一个特定的请求的,一个是对应整个ASP.NET应用程序而言.再看看HttpContext.Cache对象和HttpRuntime.Cache对象的定义:

  1. HttpContext.Cache:  获取当前应用程序的 Cache。
  2. HttpRuntime.Cache: 为当前 HTTP 请求获取 Cache 对象。

        这里获取的Cache会不会一个是针对个人,一个是针对应用程序的呢? 实际情况不是的,无论是HttpContext.Cache 还是 HttpRuntime.Cache实际上调用的都是同一个Cache对象.其实从MSDN中对System.Web.Caching.Cache的说明也可以看出"对于每个应用程序域均创建该类的一个实例,并且只要对应的应用程序域保持活动,该实例便保持有效。",从"应用程序域"这里我们似乎可以猜测可能HttpContext.Cache仅仅就是调用了HttpRuntime.Cache而已,而且这样可以保持HttpContext.Cache和HttpRuntime.Cache的一致性.查看一下HttpContext.Cache 和 HttpRuntime.Cache的实现代码(如下):

  1. HttpContext.Cache
    public Cache Cache{      get      {            return HttpRuntime.Cache;      }}
  2. HttpRuntime.Cache
    public static Cache Cache{      get      {            return HttpRuntime._theRuntime._cache.CachePublic;      }} 
这里可以清楚的看到, HttpContext.Cache 和 HttpRuntime.Cache 实际上调用的是同一个Cache了.
 
 
原创粉丝点击