.NET Framework 應用程式中的快取Caching in .NET Framework Applications

快取可讓您將資料儲存在記憶體中,以進行快速存取。Caching enables you to store data in memory for rapid access. 重新存取資料時,應用程式可以從快取中取得資料,而不是從原始來源進行擷取。When the data is accessed again, applications can get the data from the cache instead of retrieving it from the original source. 這可以改善效能和延展性。This can improve performance and scalability. 此外,暫時無法使用資料來源時,快取可讓資料可用。In addition, caching makes data available when the data source is temporarily unavailable.

.NET Framework 所提供的快取功能可用來改善 Windows 用戶端和伺服器應用程式的效能和延展性,包含 ASP.NET。The .NET Framework provides caching functionality that you can use to improve the performance and scalability of both Windows client and server applications, including ASP.NET.


在 .NET Framework 3.5 和更早版本中,ASP.NET 在 System.Web.Caching 命名空間中提供記憶體內部快取實作為。In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. 在舊版的 .NET Framework 中,快取僅適用于 System.Web 命名空間,因此需要 ASP.NET 類別的相依性。In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. 在 .NET Framework 4 中,System.Runtime.Caching 命名空間包含針對 Web 和非 Web 應用程式所設計的 API。In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications.

快取資料Caching Data

您可以使用 System.Runtime.Caching 命名空間中的類別來快取資訊。You can cache information by using classes in the System.Runtime.Caching namespace. 這個命名空間中的快取類別提供下列功能:The caching classes in this namespace provide the following features:

  • 提供建立自訂快取實作之基礎的抽象類型。Abstract types that provide the foundation for creating custom cache implementations.

  • 具體記憶體內部物件快取實作。A concrete in-memory object cache implementation.

抽象基底快取類別 (ObjectCache) 會定義下列快取工作:The abstract base caching class (ObjectCache) defines the following caching tasks:

  • 建立和管理快取項目。Creating and managing cache entries.

  • 指定到期和收回資訊。Specifying expiration and eviction information.

  • 觸發回應快取項目變更所引發的事件。Triggering events that are raised in response to changes in cache entries.

MemoryCache 類別是 ObjectCache 類別的記憶體內部物件快取實作。The MemoryCache class is an in-memory object cache implementation of the ObjectCache class. 您可以使用大部分快取工作的 MemoryCache 類別。You can use the MemoryCache class for most caching tasks.


MemoryCache 命名空間中所定義的 ASP.NET 快取物件上,建立 System.Web.Caching 類別的模型。The MemoryCache class is modeled on the ASP.NET cache object that is defined in the System.Web.Caching namespace. 因此,內部快取邏輯類似於舊版 ASP.NET 中所提供的邏輯。Therefore, the internal caching logic similar to the logic that was provided in earlier versions of ASP.NET.

如需如何在 WPF 應用程式中使用快取的範例,請參閱逐步解說:在 WPF 應用程式中快取應用程式資料For an example of how to use to caching in a WPF application, see Walkthrough: Caching Application Data in a WPF Application.

ASP.NET 應用程式中的快取Caching in ASP.NET Applications

System.Runtime.Caching 命名空間中的快取類別提供在 ASP.NET 中快取資料的功能。The caching classes in the System.Runtime.Caching namespace provide functionality for caching data in ASP.NET.


如果您的應用程式以 .NET Framework 3.5 或更早版本為目標,則必須使用 System.Web.Caching 命名空間中定義的快取類別。If your application targets the .NET Framework 3.5 or earlier, you must use the caching classes that are defined in the System.Web.Caching namespace. 如需詳細資訊,請參閱 ASP.NET 快取概觀For more information, see ASP.NET Caching Overview.


當您開發新的應用程式時,建議您使用 MemoryCache 類別。When you develop new applications, we recommend that you use the MemoryCache class. System.Runtime.Caching 命名空間中所提供的 API 就像 Cache 命名空間中提供的 API。The API that is provided in the System.Runtime.Caching namespace is like the API that is provided in the Cache namespace. 因此,如果您已在舊版 ASP.NET 中使用快取,則會熟悉 API。Therefore, the API will be familiar if you used caching in earlier versions of ASP.NET. 如需如何在 ASP.NET 應用程式中使用快取的範例,請參閱逐步解說:在 ASP.NET 中快取應用程式資料For an example of how to use caching in ASP.NET applications, see Walkthrough: Caching Application Data in ASP.NET.

輸出快取Output Caching

若要手動快取應用程式資料,您可以在 ASP.NET 中使用 MemoryCache 類別。To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET 也支援輸出快取,以將所產生的頁面、控制項和 HTTP 回應輸出儲存至記憶體中。ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. 您可以在 ASP.NET 網頁中透過宣告方式設定輸出快取,或使用 Web.config 檔案中的設定來設定輸出快取。You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web.config file. 如需詳細資訊,請參閱快取的 outputCache 項目 (ASP.NET 設定結構描述)For more information, see outputCache Element for caching (ASP.NET Settings Schema).

ASP.NET 可讓您建立自訂輸出快取提供者來擴充輸出快取。ASP.NET lets you extend output caching by creating custom output-cache providers. 使用自訂提供者,即可使用其他存放裝置 (例如磁碟、雲端存放裝置和分散式快取引擎) 來儲存快取的內容。By using custom providers, you can store cached content using other storage devices such as disks, cloud storage, and distributed cache engines. 為了建立自訂輸出快取提供者,您可以建立衍生自 OutputCacheProvider 類別的類別,並設定應用程式使用自訂輸出快取提供者。To create a custom output cache provider, you create a class that derives from the OutputCacheProvider class and configure the application to use the custom output cache provider.

WCF REST 服務中的快取Caching in WCF REST Services

針對 WCF REST 服務,.NET Framework 可讓您充分利用 ASP.NET 中可用的宣告式輸出快取。For WCF REST services, the .NET Framework enables you to take advantage of the declarative output caching that is available in ASP.NET. 這可讓您快取來自 WCF REST 服務作業的回應。This enables you to cache responses from your WCF REST service operations. 使用者將 HTTP GET 要求傳送至設定進行快取的服務時,ASP.NET 會傳回快取的回應,且不會呼叫服務方法。When a user sends an HTTP GET request to a service that is configured for caching, ASP.NET sends back the cached response, and the service method is not called. 快取到期之後,下次使用者傳送 HTTP GET 要求時,會呼叫您的服務方法,並重新快取回應。After the cache expires, the next time that a user sends an HTTP GET request, your service method is called and the response is again cached.

.NET Framework 也可讓您實作條件式 HTTP GET 快取。The .NET Framework also enables you to implement conditional HTTP GET caching. 在 REST 案例中,通常服務會使用條件式 HTTP GET 要求實作智慧型 HTTP 快取,如 HTTP 規格中所述。In REST scenarios, a conditional HTTP GET request is often used by services to implement intelligent HTTP caching as described in the HTTP Specification. 如需詳細資訊,請參閱 WCF Web HTTP 服務的快取支援For more information, see Caching Support for WCF Web HTTP Services.

擴充 .NET Framework 中的快取Extending Caching in the .NET Framework

.NET Framework 中的快取是設計成可擴充。Caching in the .NET Framework is designed to be extensible. ObjectCache 類別可讓您建立自訂快取實作。The ObjectCache class enables you to create a custom cache implementation. 這個類別提供可用於所有 Managed 應用程式的成員,包含 Windows Form、Windows Presentation Foundation (WPF) 和 Windows Communications Foundation (WCF)。This class provides members that are available to all managed applications, including Windows Forms, Windows Presentation Foundation (WPF), and Windows Communications Foundation (WCF). 若要建立使用不同儲存機制的快取類別,或您想要更精確地控制快取作業,則可能要執行這項作業。You might do this in order to create a cache class that uses a different storage mechanism, or if you want granular control over cache operations.

若要擴充快取,您可執行下列動作:To extend caching you can do the following:

如需詳細資訊,請參閱 Scott Guthrie 部落格上的 Extensible Output Caching with ASP.NET 4 (VS 2010 and .NET 4.0 Series)(ASP.NET 4 (VS 2010 和 .NET 4.0 系列) 中的可擴充輸出快取) 一文。For more information, see the entry Extensible Output Caching with ASP.NET 4 (VS 2010 and .NET 4.0 Series) on Scott Guthrie's blog.

另請參閱See also