.NET中Cache有兩種調用方式

來源:互聯網
上載者:User

.NET中Cache有兩種調用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,這兩種方式有什麼區別呢?我們先看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,既能減少出錯,也減少了一次函數調用。

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.