先看MSDN上的解释:
HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
HttpRuntime.Cache:获取当前应用程序的Cache。 
我们再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现:
//HttpContext.Cache和HttpRuntime.Cache实现
    //System.Web.HttpContext.Cache属性实现
    public sealed class HttpContext
    {
        public Cache Cache
        {
            get
            {
                return HttpRuntime.Cache;
            }
        }
    }     //System.Web.HttpRuntime.Cache属性实现
    public sealed class HttpRuntime
    {
        public static Cache Cache
        {
            get
            {
                if (AspInstallDirectoryInternal == null)
                {
                    throw new HttpException(SR.GetString("Aspnet_not_installed", new object[] { VersionInfo.SystemWebVersion }));
                }
                Cache cache = _theRuntime._cachePublic;
                if (cache == null)
                {
                    CacheInternal cacheInternal = CacheInternal;
                    CacheSection cacheSection = RuntimeConfig.GetAppConfig().Cache;
                    cacheInternal.ReadCacheInternalConfig(cacheSection);
                    _theRuntime._cachePublic = cacheInternal.CachePublic;
                    cache = _theRuntime._cachePublic;
                }
                return cache;
            }
        }
    }     通过上面的代码我们可以看出:HttpContext.Current.Cache是调用HttpRuntime.Cache实现的,两者指向同一Cache对象。那么两者到底有没有区别的?既然两个指向的是同一Cache对象,两者的差别只能出现在HttpContext和HttpRuntime上了。我们再来看看MSDN中HttpContext和HttpRuntime的定义。
    HttpContext:封装有关个别HTTP请求的所有HTTP特定的信息,HttpContext.Current为当前的HTTP请求获取HttpContext对象。
    HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。     由上面的定义可以看出:HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用;HttpContext.Current.Cache是对上述缓存类的封装,由于封装到了HttpContext类中,局限于只能在知道HttpContext下使用,即只能用于Web应用。     下面的例子可以很好的说明这一点:     //HttpContext.Cache和HttpRuntime.Cache的示例
    class CacheTest
    {
        static void Main(string[] args)
        {       
            System.Web.Caching.Cache httpRuntimeCache = System.Web.HttpRuntime.Cache;
            httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in HttpRuntime.Cache");             if (httpRuntimeCache != null)
            {
                Console.WriteLine("httpRuntimeCache:" + httpRuntimeCache["httpRuntimeCache"]);
            }             System.Web.HttpContext httpContext = System.Web.HttpContext.Current;
            if (httpContext == null)
            {
                Console.WriteLine("HttpContext object is null in Console Project");
            }
            else
            {
                System.Web.Caching.Cache httpContextCache = httpContext.Cache;
                httpContextCache.Insert("httpContextCache", "I am stored in HttpRuntime.Cache");
                if (httpContextCache == null)
                {
                    Console.WriteLine("httpContextCache is null");
                }
            }
             
            Console.ReadLine();
        }
    }       输出结果:httpRuntimeCache:I am stored in HttpRuntime.Cache
      HttpContext object is null in Console Project       综上:我们在使用Cache时,尽量使用HttpRuntime.Cache,既能减少出错,也减少了一次函数调用。       参考资料:HttpRuntime.Cache 与HttpContext.Current.Cache的疑问,HttpRuntime.Cache vs. HttpContext.Current.Cache

缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别的更多相关文章

  1. HttpContext.Current.Cache 和HttpRuntime.Cache的区别

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  2. HttpContext.Current.Cache 和 HttpRuntime.Cache 区别

    原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...

  3. HttpContext.Current.Cache 和 HttpRuntime.Cache

    HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...

  4. Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...

  5. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  6. Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache

    两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...

  7. ASP.NET HttpRuntime.Cache缓存类使用总结

    1.高性能文件缓存key-value存储—Redis 2.高性能文件缓存key-value存储—Memcached 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...

  8. HttpRuntime.Cache .Net自带的缓存类

    .Net自带的缓存有两个,一个是Asp.Net的缓存 HttpContext.Cache,一个是.Net应用程序级别的缓存,HttpRuntime.Cache. MSDN上有解释说: HttpCont ...

  9. Asp.net中的Cache--HttpRuntim.Cache 和 HttpContext.Current.Cache

    在ASP.NET中有两个类都提供缓存支持, 一个是HttpRuntime类的Cache属性, 另一个是HttpContext类的Cache属性. 通过查看这两个属性的类型可以发现其实这两个属性都是Sy ...

随机推荐

  1. wordpress模板各文件函数解析

    修改主题时发现好多WordPress主题函数都不了解,因此网上摘抄了一份放在自己博客上,便于以后好找. 在WordPress中如何按你的意愿显示页面,关键看你是否了解WordPress主题模板页面.这 ...

  2. 【HDU 5810多校】Balls and Boxes(打表/数学)

    1.打表找规律,下面是打表程序: #include <iostream> #include <cstdio> #define ll long long #define N 10 ...

  3. perl array, scalar and hash

    #!/usr/bin/perl use strict; use warnings; my @aa=("aa", "bb", "cc"); p ...

  4. exit(0)、exit(1)、exit(-1)的区别

    exit(0) - 正常退出 exit(1) - 异常退出(除0外,其他值均为异常退出)

  5. Spark MLib 基本统计汇总 2

    4. 假设检验 基础回顾: 假设检验,用于判断一个结果是否在统计上是显著的.这个结果是否有机会发生. 显著性检验 原假设与备择假设 常把一个要检验的假设记作 H0,称为原假设(或零假设) (null ...

  6. ng-if else的使用

    <!DOCTYPE html> <html> <head> <script src="http://code.angularjs.org/1.2.0 ...

  7. SVN中(trunk tags branches)的使用理解

    trunk--主干(永远都是最新的,每发布一个版本会在tags和branches上进行分支) tags-标签(只读,用于存放发布后的文件冻结,以及对应发布后版本的源文件:可以是来自主干或者分支的发布: ...

  8. Linux解压命令(tar)

    tar zxvf phddns_raspberry.tgz tar默认解压到当前目录 参数: 运行tar时必须要有下列参数中的至少一个才可运行 -A, --catenate, --concatenat ...

  9. linux下命令运行目录上程序前面要加./

    linux命令行上默认运行path系统变量的路径的程序,所以我们在运行时命令时会在系统变量中查找,而发现查找完系统变量之后是不会再查找当前目录的. 有以下方式解决: 1.在运行当前目录的程序时,前面加 ...

  10. 先装.net后装iis的问题

    如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:(即就是先装的是visual stuido 2010的话,在安装IIS 7) 32位的Windows:----------- ...