ObjectCache.Add ObjectCache.Add ObjectCache.Add ObjectCache.Add Method

定义

在派生类中重写时,向缓存中插入缓存项,而无需返回具有匹配键的现有缓存项。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) Add(CacheItem, CacheItemPolicy) Add(CacheItem, CacheItemPolicy) 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) Add(String, Object, DateTimeOffset, String) 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) Add(String, Object, CacheItemPolicy, String) 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) Add(CacheItem, CacheItemPolicy) Add(CacheItem, CacheItemPolicy) 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 CacheItem CacheItem CacheItem

要添加的对象。The object to add.

policy
CacheItemPolicy CacheItemPolicy CacheItemPolicy CacheItemPolicy

一个包含该缓存项的逐出详细信息的对象。An object that contains eviction details for the cache entry. 此对象提供比简单绝对过期更多的逐出选项。This object provides more options for eviction than a simple absolute expiration.

返回

如果插入成功,则为 true;如果缓存中已存在具有与 item 相同的键的项,则为 falsetrue 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) Add(String, Object, DateTimeOffset, String) 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 = null);
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 String String String

该缓存项的唯一标识符。A unique identifier for the cache entry.

value
Object Object Object Object

要插入的对象。The object to insert.

absoluteExpiration
DateTimeOffset DateTimeOffset DateTimeOffset 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 String String 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;如果缓存中已存在具有与 key 相同的键的项,则为 falsetrue 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) Add(String, Object, CacheItemPolicy, String) 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 = null);
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 String String String

该缓存项的唯一标识符。A unique identifier for the cache entry.

value
Object Object Object Object

要插入的对象。The object to insert.

policy
CacheItemPolicy CacheItemPolicy CacheItemPolicy 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 String String 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;如果缓存中已存在具有与 key 相同的键的项,则为 falsetrue 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.

适用于