MemoryCache 建構函式

定義

多載

MemoryCache(String, NameValueCollection)

MemoryCache 類別的新執行個體初始化。Initializes a new instance of the MemoryCache class.

MemoryCache(String, NameValueCollection, Boolean)

MemoryCache 類別的新執行個體初始化。Initializes a new instance of the MemoryCache class.

MemoryCache(String, NameValueCollection)

MemoryCache 類別的新執行個體初始化。Initializes a new instance of the MemoryCache class.

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 = null)

參數

name
String

用來查詢組態資訊的名稱。The name to use to look up configuration information.

注意:每個名稱不一定都有組態資訊。Note It is not required for configuration information to exist for every name.

如果符合的組態項目存在,則會使用組態資訊來設定 MemoryCache 執行個體。If a matching configuration entry exists, the configuration information is used to configure the MemoryCache instance. 如果符合的組態項目不存在,則可以透過 Name 屬性來存取名稱,因為指定的名稱與 MemoryCache 執行個體相關聯。If a matching configuration entry does not exist, the name can be accessed through the Name property, because the specified name is associated with the MemoryCache instance. 如需記憶體快取組態的詳細資訊,請參閱 MemoryCacheElementFor information about memory cache configuration, see MemoryCacheElement.

config
NameValueCollection

組態資訊的名稱/值組集合,用來設定快取。A collection of name/value pairs of configuration information to use for configuring the cache.

例外狀況

namenullname is null.

無法剖析 config 參數中名稱或值。A name or value in the config parameter could not be parsed.

config 集合中的值無效。A value in the config collection is invalid.

備註

MemoryCache 類別初始化時,它會檢查是否已使用此函式中的選擇性 config 參數覆寫的設定專案。When the MemoryCache class is initialized, it checks for configuration entries that might have been overridden by using the optional config parameter in the constructor. 您可以在 config 參數中傳遞下列參數。You can pass the following parameters in the config parameter. 所有的值都可以當做整數傳遞。All values can be passed as integers.

叫用此函式時,會先從應用程式佈建檔抓取設定。When this constructor is invoked, configuration settings are first retrieved from application configuration files. 如果應用程式佈建檔中沒有任何設定專案存在,則只會套用 config 中提供的設定。If no configuration entries exist in the application configuration file, only the settings provided in config are applied. 如果應用程式設定中的設定專案存在,而且也在 config中傳遞資訊,則 config 中的資訊會覆寫從設定檔案讀取的資訊。If the configuration entries in the application configuration exist, and if information is also passed in config, the information in the config overrides the information that is read from the configuration file.

指派給快取實例的名稱值會以兩種方式使用:The name value that is assigned to a cache instance is used in two ways:

  • 協助您在有一個以上的實例存在時,追蹤多個快取實例。To help you keep track of multiple cache instances when more than one instance exists.

  • 表示在初始化 MemoryCache 實例時,參考設定檔中的設定。To reference settings in the configuration file when the MemoryCache instance is initialized.

    注意

    沒有任何機制可強制執行快取實例的唯一名稱。There is no mechanism to enforce unique names for cache instances. 因此,可以有多個具有相同名稱的快取實例。Therefore, it is possible to have multiple cache instances with the same name.

    警告

    除非必要,否則請勿建立 MemoryCache 實例。Do not create MemoryCache instances unless it is required. 如果您在用戶端和 Web 應用程式中建立快取實例,應該在應用程式生命週期早期建立 MemoryCache 實例。If you create cache instances in client and Web applications, the MemoryCache instances should be created early in the application life cycle. 您必須只建立將在應用程式中使用的快取實例數目,並將快取實例的參考儲存在可全域存取的變數中。You must create only the number of cache instances that will be used in your application, and store references to the cache instances in variables that can be accessed globally. 例如,在 ASP.NET 應用程式中,您可以將參考儲存在應用程式狀態中。For example, in ASP.NET applications, you can store the references in application state. 如果您在應用程式中只建立單一快取實例,當您需要存取快取時,請使用預設快取,並從 Default 屬性取得它的參考。If you create only a single cache instance in your application, use the default cache and get a reference to it from the Default property when you need to access the cache.

MemoryCache(String, NameValueCollection, Boolean)

MemoryCache 類別的新執行個體初始化。Initializes a new instance of the MemoryCache class.

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

用來查詢組態資訊的名稱。The name to use to look up configuration information.

config
NameValueCollection

組態資訊的名稱/值組集合,用來設定快取。A collection of name/value pairs of configuration information to use for configuring the cache.

ignoreConfigSection
Boolean

指出是否應該忽略組態區段。Indicates whether the configuration section should be ignored.

適用於