博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
阅读量:7080 次
发布时间:2019-06-28

本文共 2361 字,大约阅读时间需要 7 分钟。

原文地址:

 .NET中Cache有两种调用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,这两种方式有什么区别呢?我们先看MSDN上的解释:

      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
      HttpRuntime.Cache:获取当前应用程序的Cache。
      我们再用.NET Reflector工具看看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应用。
      下面的例子可以很好的说明这一点:

    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,既能减少出错,也减少了一次函数调用。
      参考资料:,

你可能感兴趣的文章
(转)c#实现WinRAR解压缩
查看>>
MIME
查看>>
NetworkInterface的使用
查看>>
http://daffodil.codeplex.com/
查看>>
反码符号-128有8位二进制表示的原码、反码和补码
查看>>
编程输出完美之星编程大赛 复赛第二场 24点
查看>>
最大连续子序列
查看>>
iPhone loadView 和 viewDidLoad的区别
查看>>
Netfilter/iptables的一些新进展
查看>>
程序员的人生规划
查看>>
PHP中抽象类与接口的应用场景
查看>>
ASP.NET中操作SQL数据库
查看>>
Android性能优化
查看>>
Ehcache BigMemory: 摆脱GC困扰
查看>>
windows下,下载pip安装
查看>>
nginx反向代理中proxy_set_header 运维笔记
查看>>
jQuery操作元素的class属性
查看>>
关于idea新建子目录时往父目录名字后叠加而不是树形结构的解决方法(转)
查看>>
HttpURLConnection和HttpClient的区别2(转)
查看>>
MYSQL-GroupCommit
查看>>