C# HttpContext.Current.Cache.Insert 和 HttpContext.Current.Cache.Add 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9063048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:45:41  来源:igfitidea点击:

What is the difference between HttpContext.Current.Cache.Insert and HttpContext.Current.Cache.Add

c#asp.netcaching

提问by Owidat

I'm working on an ASP.NET web application and I want to implement caching, so I want to know the difference between HttpContext.Current.Cache.Insertand HttpContext.Current.Cache.Addand which one is better?

我工作的ASP.NET Web应用程序,我想实现缓存,所以我想知道之间的差别HttpContext.Current.Cache.InsertHttpContext.Current.Cache.Add与哪一个更好?

采纳答案by Jalayn

The main difference between the two is that if an object with the same name already exists in the cache, the Insertmethod call on your instance of Cachewill replace the object, whereas the Addmethod call will fail (taken from the Remarksparagraph of methods Add and Insert on their respective MSDN reference page):

两者的主要区别在于,如果缓存中已存在同名对象,则Insert对您的实例的方法调用Cache将替换该对象,而Add方法调用将失败(取自方法 Add 和 Insert的备注段落在他们各自的 MSDN 参考页面上):

Add

添加

Calls to this method will fail if an item with the same key parameter is already stored in the Cache. To overwrite an existing Cache item using the same key parameter, use the Insert method.

如果具有相同键参数的项目已存储在缓存中,则调用此方法将失败。要使用相同的键参数覆盖现有的 Cache 项,请使用 Insert 方法。

Insert

插入

This method will overwrite an existing cache item whose key matches the key parameter.

此方法将覆盖其键与键参数匹配的现有缓存项。

The other main difference is also that with the Addmethod some parameters are mandatory, whereas with Insert, various overloaded methods are available, and some parameters will be set to default values like the absolute or sliding expirations.

另一个主要区别还在于该Add方法的一些参数是强制性的,而 withInsert则可以使用各种重载方法,并且一些参数将设置为默认值,例如绝对或滑动到期。

You can see that there is no difference between the Add and the Insertmethod with the exact same parameters except that Insert will not fail (i.e. do nothing) if an object with the same name is in the cache.

您可以看到,具有完全相同参数的 Add 和Insert方法之间没有区别,只是如果缓存中具有相同名称的对象,则 Insert 不会失败(即什么也不做)。

回答by Andrew Steitz

[EDIT] 2015-10-29See the comment by Mark Sowul below. UGH! [/EDIT]

[编辑] 2015-10-29请参阅下面 Mark Sowul 的评论。啊! [/编辑]

I just posted my comments below at the link (Remarks) that Jalayn provided. Anyone here care to comment? Anyone counting on getting an error thrown, please see my last paragraph below and shame on you! :)

我刚刚在 Jalayn 提供的链接(备注)上发布了我的评论。这里有人愿意评论吗?任何指望抛出错误的人,请参阅我下面的最后一段,并为您感到羞耻!:)

Being the obstinate type, I thought I'd give this a try. I wrote some code that looks something like this.

作为顽固的类型,我想我会尝试一下。我写了一些看起来像这样的代码。

var myObject = new MyObjectType() { prop1 = "string 1", prop2 = 1 };
var cacheKey = "mycachekey";
var cTime = DateTime.Now.AddMinutes(11);
var cExp = System.Web.Caching.Cache.NoSlidingExpiration;
var cPri = System.Web.Caching.CacheItemPriority.Normal;

HttpContext.Current.Cache.Add(cacheKey, myObject, null, cTime, cExp, cPri, null);
myObject.prop1 = "string 2"; myObject.prop2 = 2;
HttpContext.Current.Cache.Add(cacheKey, myObject, null, cTime, cExp, cPri, null);
myObject.prop1 = "string 3"; myObject.prop2 = 3;
HttpContext.Current.Cache.Insert(cacheKey, myObject, null, cTime, cExp, cPri, null);
myObject.prop1 = "string 4"; myObject.prop2 = 4;
HttpContext.Current.Cache.Insert(cacheKey, myObject, null, cTime, cExp, cPri, null);
myObject.prop1 = "string 5"; myObject.prop2 = 5;
HttpContext.Current.Cache.Add(cacheKey, myObject, null, cTime, cExp, cPri, null);
myObject.prop1 = "string 6"; myObject.prop2 = 6;
HttpContext.Current.Cache.Insert(cacheKey, myObject, null, cTime, cExp, cPri, null);

var foo = (MyObjectType)HttpContext.Current.Cache[cacheKey];

Guess what. It ran fine. I even put a break point before and after this code block and executed HttpContext.Current.Cache (in the Immediate window of VS) at each point and verified that only one item was in fact added to the cache. And foo.prop1 = "string 6".

你猜怎么着。它运行良好。我什至在这个代码块之前和之后放置了一个断点,并在每个点执行 HttpContext.Current.Cache(在 VS 的立即窗口中)并验证实际上只有一项被添加到缓存中。和 foo.prop1 = "string 6"。

Anyone from Microsoft care to explain why this did NOT throw an exception? Although it is poor practice (and our application does not do it) someone may be counting on getting an error thrown and their application may not behave as expected because the CLR does not throw an error even though your documentation says it will.

微软的任何人都关心解释为什么这没有抛出异常?尽管这是一种糟糕的做法(并且我们的应用程序没有这样做),但有人可能指望抛出错误并且他们的应用程序可能不会按预期运行,因为即使您的文档说它会抛出错误,CLR 也不会抛出错误。

回答by Novice Programmer

It is always misunderstood that Cache.Add will throw an error if it tries add an item with same parameter. It does not throw any error/exception or rather it will not update and does not throw any error. Check the msdn article below where it says

人们总是误解 Cache.Add 尝试添加具有相同参数的项目时会抛出错误。它不会抛出任何错误/异常,或者更确切地说它不会更新并且不会抛出任何错误。检查下面说的msdn文章

Cache.Add

Add method returns the object you added to the cache. Additionally, if you use the Add method and an item with the same name already exists in the cache, the method will not replace the item and will not raise an exception. http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.100).aspx

Cache.Insert

If you use the Insert method to add an item to the cache and an item with the same name already exists, the existing item in the cache is replaced

缓存.添加

Add 方法返回您添加到缓存中的对象。此外,如果您使用 Add 方法并且缓存中已存在同名项目,则该方法不会替换该项目并且不会引发异常。 http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.100).aspx

缓存.插入

如果使用Insert 方法向缓存中添加项并且已存在同名项,则替换缓存中的现有项

Another difference is Cache.Insert has 5 overloads and Add has only one. And Add method returns the object that was in the cache under that key (null if a new item, otherwise the item currently in the cache), whereas Insert returns nothing.

另一个区别是 Cache.Insert 有 5 个重载,而 Add 只有一个。Add 方法返回该键下缓存中的对象(如果是新项目则为 null,否则为当前缓存中的项目),而 Insert 不返回任何内容。

Add method's remarkssays fails to update. I guess it is a typo in add page. I think what they meant to say is "Calls to this method will fail to updateif an item with the same key parameter is already stored in the Cache"

添加方法的备注说更新失败。我想这是添加页面中的错字。我认为他们的意思是“如果具有相同关键参数的项目已存储在缓存中,则对此方法的调用将无法更新

you can try run this code and see yourself whether you get any error/exception.

您可以尝试运行此代码,看看自己是否遇到任何错误/异常。

// returns null
Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

// returns "Value 1"
Cache.Add("Key1", "Value 2", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);