ObjectCache的“Set”和“Add”有什么区别?

时间:2021-02-18 13:25:08

From the doc

从文档

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. [1]

Add(CacheItem,CacheItemPolicy):在派生类中重写时,尝试将缓存条目作为CacheItem实例插入缓存中,并添加有关如何逐出该条目的详细信息。 [1]

-

-

Set(CacheItem, CacheItemPolicy) : When overridden in a derived class, inserts the cache entry into the cache as a CacheItem instance, specifying information about how the entry will be evicted. [2]

Set(CacheItem,CacheItemPolicy):在派生类中重写时,将缓存条目作为CacheItem实例插入缓存中,指定有关如何逐出该条目的信息。 [2]

I see little difference in the wording (tries to) and signature (set is a sub, add returns a boolean), but I'm not sure which one I should use and if there is really something different between both.

我看到措辞(尝试)和签名(set是一个sub,add返回一个布尔值)没什么区别,但是我不确定我应该使用哪一个,以及两者之间是否真的有什么不同。

1 个解决方案

#1


26  

The Main difference is that the Add() method tries to insert a cache without overwriting an existing cache entry with the same key.

主要区别在于Add()方法尝试插入缓存而不覆盖具有相同密钥的现有缓存条目。

While the Set() method will overwrite an existing cache entry having the same key. [ However If the key for an item does not exist, insertion will be done as a new cache entry ].

而Set()方法将覆盖具有相同密钥的现有缓存条目。 [但是,如果项目的密钥不存在,则插入将作为新的缓存条目完成]。

Above was the difference in terms of their functionality.

以上是其功能的差异。

Syntactical Difference:

句法差异:

One significant syntactical difference is that the Add() method returns a Boolean which is true if insertion succeeded, or false if there is already an entry in the cache that has the same key as item. The Set() method has a void return type.

一个重要的语法区别是Add()方法返回一个布尔值,如果插入成功则返回true;如果缓存中已有一个与item具有相同键的条目,则返回false。 Set()方法具有void返回类型。

One last point that internal implementation of Add() method actually calls its corresponding version of AddOrGetExisting() method.

最后一点,Add()方法的内部实现实际上调用了相应版本的AddOrGetExisting()方法。

 public virtual bool Add(CacheItem item, CacheItemPolicy policy)
{
    return this.AddOrGetExisting(item, policy) == null;
}

#1


26  

The Main difference is that the Add() method tries to insert a cache without overwriting an existing cache entry with the same key.

主要区别在于Add()方法尝试插入缓存而不覆盖具有相同密钥的现有缓存条目。

While the Set() method will overwrite an existing cache entry having the same key. [ However If the key for an item does not exist, insertion will be done as a new cache entry ].

而Set()方法将覆盖具有相同密钥的现有缓存条目。 [但是,如果项目的密钥不存在,则插入将作为新的缓存条目完成]。

Above was the difference in terms of their functionality.

以上是其功能的差异。

Syntactical Difference:

句法差异:

One significant syntactical difference is that the Add() method returns a Boolean which is true if insertion succeeded, or false if there is already an entry in the cache that has the same key as item. The Set() method has a void return type.

一个重要的语法区别是Add()方法返回一个布尔值,如果插入成功则返回true;如果缓存中已有一个与item具有相同键的条目,则返回false。 Set()方法具有void返回类型。

One last point that internal implementation of Add() method actually calls its corresponding version of AddOrGetExisting() method.

最后一点,Add()方法的内部实现实际上调用了相应版本的AddOrGetExisting()方法。

 public virtual bool Add(CacheItem item, CacheItemPolicy policy)
{
    return this.AddOrGetExisting(item, policy) == null;
}