แก้ไข

แชร์ผ่าน


ObjectCache.Add Method

Definition

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.

Overloads

Add(CacheItem, CacheItemPolicy)

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.

Remarks

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. The cache entry can be a typed CacheItem object or a generic object.

The AddOrGetExisting method overloads and the Add method overloads have one significant difference. 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. 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)

Source:
ObjectCache.cs
Source:
ObjectCache.cs
Source:
ObjectCache.cs

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

Parameters

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.

Returns

true if insertion succeeded, or false if there is an already an entry in the cache that has the same key as item.

Remarks

The 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. If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.

Applies to

Add(String, Object, DateTimeOffset, String)

Source:
ObjectCache.cs
Source:
ObjectCache.cs
Source:
ObjectCache.cs

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 = Nothing) As Boolean

Parameters

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. 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. Because regions are not implemented in .NET Framework 4, the default value is null.

Returns

true if insertion succeeded, or false if there is an already an entry in the cache that has the same key as key.

Remarks

The 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. If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.

Applies to

Add(String, Object, CacheItemPolicy, String)

Source:
ObjectCache.cs
Source:
ObjectCache.cs
Source:
ObjectCache.cs

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 = Nothing) As Boolean

Parameters

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. The default value for the optional parameter is null.

Returns

true if the insertion try succeeds, or false if there is an already an entry in the cache with the same key as key.

Remarks

The 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. If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.

Applies to