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)
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;如果缓存中已存在具有与 item 相同的键的项,则为 false。true 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 内部调用 AddOrGetExisting 。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. 如果缓存实现不需要方法的任何特殊行为 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 = Nothing) 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 中未实现区域,因此默认值为 null 。Because regions are not implemented in .NET Framework 4, the default value is null.
返回
如果插入成功,则为 true;如果缓存中已存在具有与 key 相同的键的项,则为 false。true 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 内部调用 AddOrGetExisting 。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. 如果缓存实现不需要方法的任何特殊行为 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 = Nothing) 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. 可选参数的默认值为 null。The default value for the optional parameter is null.
返回
如果尝试插入成功,则为 true;如果缓存中已存在具有与 key 相同的键的项,则为 false。true 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 内部调用 AddOrGetExisting 。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. 如果缓存实现不需要方法的任何特殊行为 Add ,则可以仅实现 AddOrGetExisting 方法重载。If a cache implementation does not require any special behavior for the Add method, it can just implement the AddOrGetExisting method overloads.