ASP.NET cache add vs insert

2019-01-23 10:32发布

What is the difference between the Cache.Add() and Cache.Insert() methods?

In which situations should I use each one?

3条回答
神经病院院长
2楼-- · 2019-01-23 10:43

Cache.Add() also returns a cached object from Cache after it was added:

string cachedItem = Cache.Add("cachedItem", ....);
查看更多
可以哭但决不认输i
3楼-- · 2019-01-23 10:46

Insert will overwrite an existing cached value with the same Key; Add fails (does nothing) if there is an existing cached value with the same key. So there's a case for saying you should always use Insert since the first time the code runs it will put your object into the cache and when it runs subsequently it will update the cached value.

查看更多
一夜七次
4楼-- · 2019-01-23 10:46

You can use either Cache.Add() or Cache.Insert() methods for caching your data. The only difference between the two is, Cache.Add() method returns the object which you want to cache. So let’s say if you want to use the object and cache it as well. You can do so in a single line of code with the help of Cache.Add().

Cache.Insert() methods has 4 different types of overloaded methods while Cache.Add() has only one.

查看更多
登录 后发表回答