Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between HttpContext.Current.Cache.Insert and HttpContext.Current.Cache.Add

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?

like image 261
Owidat Avatar asked Jan 30 '12 10:01

Owidat


People also ask

Is HttpContext current Cache thread safe?

HttpContext access from a background threadHttpContext isn't thread-safe.


1 Answers

The main difference between the two is that if an object with the same name already exists in the cache, the Insert method call on your instance of Cache will replace the object, whereas the Add method call will fail (taken from the Remarks paragraph of methods Add and Insert on their respective MSDN reference page):

Add

Calls to this method will fail if an item with the same key parameter is already stored in the Cache. To overwrite an existing Cache item using the same key parameter, use the Insert method.

Insert

This method will overwrite an existing cache item whose key matches the key parameter.

The other main difference is also that with the Add method some parameters are mandatory, whereas with Insert , 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.

like image 179
Jalayn Avatar answered Oct 05 '22 19:10

Jalayn