加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – ObjectCache的“Set”和“Add”有什么区别?

发布时间:2020-12-15 23:28:50 所属栏目:asp.Net 来源:网络整理
导读:从 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. 07001 – Set(CacheItem,inserts the cac
从 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. 07001

Set(CacheItem,inserts the cache entry into the cache as a CacheItem instance,specifying information about how the entry will be evicted. 07002

我在文字(尝试)和签名(set是一个sub,add返回一个布尔值)中看不到什么区别,但是我不知道我应该使用哪一个,如果两者之间有一些不同的东西.

解决方法

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

而Set()方法将覆盖具有相同键的现有缓存条目. [但是如果一个项目的键不存在,插入将作为一个新的缓存条目进行].

以上是功能上的区别.

语法差异:

一个重要的语法区别是,Add()方法返回一个布尔值,如果插入成功则返回true;如果缓存中已经存在与项目相同的密钥项,则返回false.
Set()方法有一个void返回类型.

最后一点,Add()方法的内部实现实际上调用其对应版本的AddOrGetExisting()方法.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读