guchko-gleb
guchko-gleb

Reputation: 983

ASP.NET cache add vs insert

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

In which situations should I use each one?

Upvotes: 76

Views: 25915

Answers (3)

PhilPursglove
PhilPursglove

Reputation: 12589

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.

Upvotes: 106

Artem A
Artem A

Reputation: 2448

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

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

Upvotes: 3

SHEKHAR SHETE
SHEKHAR SHETE

Reputation: 6066

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.

Upvotes: -3

Related Questions