开发者

ASP.NET cache add vs insert

开发者 https://www.devze.com 2023-03-15 07:12 出处:网络
What is the diffe开发者_JS百科rence between the Cache.Add() and Cache.Insert() methods? In which situations should I use each one?Insert will overwrite an existing cached value with the same Key; Add

What is the diffe开发者_JS百科rence between the Cache.Add() and Cache.Insert() methods?

In which situations should I use each one?


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.


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消