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.Insert and HttpContext.Current.Cache.Add and which one is better?
I’m working on an ASP.NET web application and I want to implement caching, so
Share
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 ofCachewill replace the object, whereas theAddmethod call will fail (taken from the Remarks paragraph of methods Add and Insert on their respective MSDN reference page):Add
Insert
The other main difference is also that with the
Addmethod some parameters are mandatory, whereas withInsert, various overloaded methods are available, and some parameters will be set to default values like the absolute or sliding expirations.You can see that there is no difference between the Add and the Insert method 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.