MemoryCache 建構函式

定義

多載

MemoryCache(String, NameValueCollection)

初始化 MemoryCache 類別的新執行個體。

MemoryCache(String, NameValueCollection, Boolean)

初始化 MemoryCache 類別的新執行個體。

MemoryCache(String, NameValueCollection)

來源:
MemoryCache.cs
來源:
MemoryCache.cs
來源:
MemoryCache.cs

初始化 MemoryCache 類別的新執行個體。

public MemoryCache (string name, System.Collections.Specialized.NameValueCollection config = default);
new System.Runtime.Caching.MemoryCache : string * System.Collections.Specialized.NameValueCollection -> System.Runtime.Caching.MemoryCache
Public Sub New (name As String, Optional config As NameValueCollection = Nothing)

參數

name
String

用來查詢組態資訊的名稱。

Note 每個名稱的組態資訊都不需要存在。

如果符合的組態項目存在,則會使用組態資訊來設定 MemoryCache 執行個體。 如果符合的組態項目不存在,則可以透過 Name 屬性來存取名稱,因為指定的名稱與 MemoryCache 執行個體相關聯。 如需記憶體快取組態的相關資訊,請參閱 MemoryCacheElement

config
NameValueCollection

組態資訊的名稱/值組集合,用來設定快取。

例外狀況

namenull

無法剖析 config 參數中名稱或值。

config 集合中的值無效。

備註

MemoryCache初始化 類別時,它會檢查建構函式中使用選擇性config參數來覆寫的組態專案。 您可以在 參數中 config 傳遞下列參數。 所有值都可以當做整數傳遞。

叫用此建構函式時,會先從應用程式組態檔擷取組態設定。 如果應用程式組態檔中沒有任何組態專案存在,則只會套用 中 config 提供的設定。 如果應用程式組態中的組態專案存在,而且資訊也會傳入 config,則 中的 config 資訊會覆寫從組態檔讀取的資訊。

指派給快取實例的名稱值會以兩種方式使用:

  • 協助您在有多個實例存在時追蹤多個快取實例。

  • 若要在初始化實例時 MemoryCache 參考組態檔中的設定。

    注意

    沒有機制可強制執行快取實例的唯一名稱。 因此,可以有多個具有相同名稱的快取實例。

    警告

    除非必要,否則請勿建立 MemoryCache 實例。 如果您在用戶端和 Web 應用程式中建立快取實例, MemoryCache 則應該在應用程式生命週期初期建立實例。 您只能建立將在應用程式中使用的快取實例數目,並將快取實例的參考儲存在可全域存取的變數中。 例如,在 ASP.NET 應用程式中,您可以將參考儲存在應用程式狀態中。 如果您只在您的應用程式中建立單一快取實例,請使用預設快取,並在需要存取快取時從 Default 屬性取得其參考。

適用於

MemoryCache(String, NameValueCollection, Boolean)

來源:
MemoryCache.cs
來源:
MemoryCache.cs
來源:
MemoryCache.cs

初始化 MemoryCache 類別的新執行個體。

public:
 MemoryCache(System::String ^ name, System::Collections::Specialized::NameValueCollection ^ config, bool ignoreConfigSection);
public MemoryCache (string name, System.Collections.Specialized.NameValueCollection config, bool ignoreConfigSection);
new System.Runtime.Caching.MemoryCache : string * System.Collections.Specialized.NameValueCollection * bool -> System.Runtime.Caching.MemoryCache
Public Sub New (name As String, config As NameValueCollection, ignoreConfigSection As Boolean)

參數

name
String

用來查詢組態資訊的名稱。

config
NameValueCollection

組態資訊的名稱/值組集合,用來設定快取。

ignoreConfigSection
Boolean

指出是否應該忽略組態區段。

適用於