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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)