ASP。NET缓存增加vs插入

时间:2021-07-07 03:52:15

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

Cache.Add()和Cache.Insert()方法之间的区别是什么?

In which situations should I use each one?

在什么情况下我应该使用每一个?

3 个解决方案

#1


84  

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.

Insert将使用相同的键覆盖现有的缓存值;如果存在具有相同键的现有缓存值,则添加失败(什么也不做)。有一个例子说你应该一直使用Insert,因为代码第一次运行时,它会将你的对象放入缓存中,当它随后运行时,它会更新缓存的值。

#2


4  

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

添加Cache. add()还从缓存中返回缓存后的对象:

string cachedItem = Cache.Add("cachedItem", ....);

#3


0  

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.Add()或Cache.Insert()方法来缓存数据。两者之间的惟一区别是,cache. add()方法返回要缓存的对象。假设你想要使用这个对象并缓存它。您可以在一个代码行中使用Cache.Add()。

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

insert()方法有4种不同类型的重载方法,而Cache.Add()只有一个。

#1


84  

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.

Insert将使用相同的键覆盖现有的缓存值;如果存在具有相同键的现有缓存值,则添加失败(什么也不做)。有一个例子说你应该一直使用Insert,因为代码第一次运行时,它会将你的对象放入缓存中,当它随后运行时,它会更新缓存的值。

#2


4  

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

添加Cache. add()还从缓存中返回缓存后的对象:

string cachedItem = Cache.Add("cachedItem", ....);

#3


0  

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.Add()或Cache.Insert()方法来缓存数据。两者之间的惟一区别是,cache. add()方法返回要缓存的对象。假设你想要使用这个对象并缓存它。您可以在一个代码行中使用Cache.Add()。

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

insert()方法有4种不同类型的重载方法,而Cache.Add()只有一个。