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?
HttpContext access from a background threadHttpContext isn't thread-safe.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With