MemoryCache.Dispose 方法


MemoryCache 類別的目前執行個體所使用的資源全部釋出。Releases all resources that are used by the current instance of the MemoryCache class.

 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()



每個 MemoryCache 實例都會系結至 AppDomainUnloaded 事件。Each MemoryCache instance is bound to the AppDomainUnloaded event. 不過,在應用程式網域關閉期間,如果尚未明確處置以記憶體為基礎的快取,則快取實例會自動呼叫 Dispose 方法。However, during application-domain shutdown, if a memory-based cache has not been explicitly disposed, the cache instance will automatically call the Dispose method.

已處置的快取實例會使用下列步驟關閉:The disposed cache instance is shut down using the following steps:

  1. 快取的狀態會設定為表示已處置快取。The state of the cache is set to indicate that the cache is disposed. 嘗試呼叫會變更快取狀態的公用快取方法(例如新增、移除或抓取快取專案的方法)可能會導致非預期的行為。Any attempt to call public caching methods that change the state of the cache, such as methods that add, remove, or retrieve cache entries, might cause unexpected behavior. 例如,如果您在處置快取後呼叫 Set 方法,則不會發生作業錯誤。For example, if you call the Set method after the cache is disposed, a no-op error occurs. 如果您嘗試從快取中取出專案,Get 方法一律會傳回 nullIf you attempt to retrieve items from the cache, the Get method will always return null.

  2. 不再從目前的快取實例引發效能計數器資訊。Performance counter information is no longer raised from the current cache instance.

  3. 已釋放目前快取實例所持有之快取物件的所有參考。All references to cached objects held by the current cache instance are released.

  4. 目前正在監視快取中專案的任何 CacheEntryChangeMonitor 實例都會收到通知。Any CacheEntryChangeMonitor instances that are currently monitoring entries in the cache are notified.

  5. 系統會呼叫提供這些監視器變更通知的回呼物件。Callback objects that provide notification of changes for those monitors are called.

  6. 系統會呼叫向快取中的專案註冊的任何 CacheItemRemovedCallback 實例。Any CacheItemRemovedCallback instances that are registered with entries in the cache are called. 傳遞至回呼的移除原因是 CacheSpecificEvictionThe removal reason that is passed to the callbacks is CacheSpecificEviction.