ObjectCache.Add 方法

定義

在衍生類別中遭覆寫時,在快取中插入快取項目,而不要求傳回具有相符索引鍵的現有快取項目。When overridden in a derived class, inserts a cache entry into the cache, without requiring that an existing cache entry with a matching key be returned.

多載

Add(CacheItem, CacheItemPolicy)

在衍生類別中遭覆寫時,嘗試在快取區中插入做為 CacheItem 執行個體的快取項目,並加入項目清除方式的詳細資訊。When overridden in a derived class, tries to insert a cache entry into the cache as a CacheItem instance, and adds details about how the entry should be evicted.

Add(String, Object, DateTimeOffset, String)

在衍生類別中覆寫時,在快取中插入快取項目,但不覆寫任何現有的快取項目。When overridden in a derived class, inserts a cache entry into the cache without overwriting any existing cache entry.

Add(String, Object, CacheItemPolicy, String)

在衍生類別中遭覆寫時,在快取中插入快取項目,並指定關於要如何收回項目的資訊。When overridden in a derived class, inserts a cache entry into the cache, specifying information about how the entry will be evicted.

備註

Add 方法多載會嘗試將快取專案插入快取中,而不會覆寫或移除具有相同索引鍵的現有快取專案。The Add method overloads try to insert a cache entry into the cache, without overwriting or removing an existing cache entry that has the same key. 快取專案可以是具類型的 CacheItem 物件或泛型物件。The cache entry can be a typed CacheItem object or a generic object.

AddOrGetExisting 的方法多載和 Add 方法多載有一個顯著的差異。The AddOrGetExisting method overloads and the Add method overloads have one significant difference. 當這些方法插入快取專案時,如果在快取中找到相符的專案,AddOrGetExisting 方法多載會傳回現有的快取專案,但 Add 的方法多載則不會。When these methods insert a cache entry, if a matching entry is found in the cache, the AddOrGetExisting method overloads return the existing cache entry, but the Add method overloads do not. 有了這些不同的方法多載,可讓呼叫者根據是否需要現有的快取專案來優化其程式碼。Having these different method overloads enables callers to optimize their code based on whether they need the existing cache entry. 在分散式快取中,使用 AddOrGetExisting 方法來傳回現有的值可能會比使用 Add 方法傳回布林值更耗費資源。In distributed caches, returning an existing value by using the AddOrGetExisting method might be more expensive than returning a Boolean value by using Add method.

Add(CacheItem, CacheItemPolicy)

在衍生類別中遭覆寫時,嘗試在快取區中插入做為 CacheItem 執行個體的快取項目,並加入項目清除方式的詳細資訊。When overridden in a derived class, tries to insert a cache entry into the cache as a CacheItem instance, and adds details about how the entry should be evicted.

public:
 virtual bool Add(System::Runtime::Caching::CacheItem ^ item, System::Runtime::Caching::CacheItemPolicy ^ policy);
public virtual bool Add (System.Runtime.Caching.CacheItem item, System.Runtime.Caching.CacheItemPolicy policy);
abstract member Add : System.Runtime.Caching.CacheItem * System.Runtime.Caching.CacheItemPolicy -> bool
override this.Add : System.Runtime.Caching.CacheItem * System.Runtime.Caching.CacheItemPolicy -> bool
Public Overridable Function Add (item As CacheItem, policy As CacheItemPolicy) As Boolean

參數

item
CacheItem

要新增的物件。The object to add.

policy
CacheItemPolicy

物件,包含快取項目的收回詳細資料。An object that contains eviction details for the cache entry. 這個物件提供的收回選項比簡單絕對期限要多。This object provides more options for eviction than a simple absolute expiration.

傳回

如果插入成功則為 true,如果快取中已經有項目具有與 false 相同的索引鍵,則為 itemtrue if insertion succeeded, or false if there is an already an entry in the cache that has the same key as item.

備註

Add 的方法多載在 ObjectCache 類別上是虛擬(而非抽象),因為 Add 方法會在內部呼叫 AddOrGetExistingThe Add method overloads are virtual (not abstract) on the ObjectCache class, because the Add method internally calls AddOrGetExisting. 這可減少快取實施者必須提供的方法多載數目。This reduces the number of method overloads that a cache implementer has to provide. 如果快取實作為 Add 方法不需要任何特殊行為,它可以只執行 AddOrGetExisting 的方法多載。If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.

Add(String, Object, DateTimeOffset, String)

在衍生類別中覆寫時,在快取中插入快取項目,但不覆寫任何現有的快取項目。When overridden in a derived class, inserts a cache entry into the cache without overwriting any existing cache entry.

public virtual bool Add (string key, object value, DateTimeOffset absoluteExpiration, string regionName = default);
abstract member Add : string * obj * DateTimeOffset * string -> bool
override this.Add : string * obj * DateTimeOffset * string -> bool
Public Overridable Function Add (key As String, value As Object, absoluteExpiration As DateTimeOffset, Optional regionName As String = null) As Boolean

參數

key
String

快取項目的唯一識別項。A unique identifier for the cache entry.

value
Object

要插入的物件。The object to insert.

absoluteExpiration
DateTimeOffset

快取項目到期的固定日期和時間。The fixed date and time at which the cache entry will expire. 呼叫 Add 方法時必須使用此參數。This parameter is required when the Add method is called.

regionName
String

選擇性。Optional. 如果已實作區域,則為快取區中的具名區域,快取項目可以加入至這個區域。A named region in the cache to which the cache entry can be added, if regions are implemented. 由於未在 .NET Framework 4.NET Framework 4 中實作區域,因此預設值為 nullBecause regions are not implemented in .NET Framework 4.NET Framework 4, the default value is null.

傳回

如果插入成功則為 true,如果快取中已經有項目具有與 false 相同的索引鍵,則為 keytrue if insertion succeeded, or false if there is an already an entry in the cache that has the same key as key.

備註

Add 的方法多載在 ObjectCache 類別上是虛擬(而非抽象),因為 Add 方法會在內部呼叫 AddOrGetExistingThe Add method overloads are virtual (not abstract) on the ObjectCache class, because the Add method internally calls AddOrGetExisting. 這可減少快取實施者必須提供的方法多載數目。This reduces the number of method overloads that a cache implementer has to provide. 如果快取實作為 Add 方法不需要任何特殊行為,它可以只執行 AddOrGetExisting 的方法多載。If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.

Add(String, Object, CacheItemPolicy, String)

在衍生類別中遭覆寫時,在快取中插入快取項目,並指定關於要如何收回項目的資訊。When overridden in a derived class, inserts a cache entry into the cache, specifying information about how the entry will be evicted.

public virtual bool Add (string key, object value, System.Runtime.Caching.CacheItemPolicy policy, string regionName = default);
abstract member Add : string * obj * System.Runtime.Caching.CacheItemPolicy * string -> bool
override this.Add : string * obj * System.Runtime.Caching.CacheItemPolicy * string -> bool
Public Overridable Function Add (key As String, value As Object, policy As CacheItemPolicy, Optional regionName As String = null) As Boolean

參數

key
String

快取項目的唯一識別項。A unique identifier for the cache entry.

value
Object

要插入的物件。The object to insert.

policy
CacheItemPolicy

物件,包含快取項目的收回詳細資料。An object that contains eviction details for the cache entry. 這個物件提供的收回選項比簡單絕對期限要多。This object provides more options for eviction than a simple absolute expiration.

regionName
String

選擇性。Optional. 如果已實作區域,則為快取區中的具名區域,快取項目可以加入至這個區域。A named region in the cache to which the cache entry can be added, if regions are implemented. 選擇性參數的預設值為 nullThe default value for the optional parameter is null.

傳回

在成功插入項目的情況下為 true,如果快取區中已經包含項目,而且該項目的索引鍵與 false 相同,則為 keytrue if the insertion try succeeds, or false if there is an already an entry in the cache with the same key as key.

備註

Add 的方法多載在 ObjectCache 類別上是虛擬(而非抽象),因為 Add 方法會在內部呼叫 AddOrGetExistingThe Add method overloads are virtual (not abstract) on the ObjectCache class, because the Add method internally calls AddOrGetExisting. 這可減少快取實施者必須提供的方法多載數目。This reduces the number of method overloads that a cache implementer has to provide. 如果快取實作為 Add 方法不需要任何特殊行為,它可以只執行 AddOrGetExisting 的方法多載。If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.

適用於