Can anyone explain why you cannot insert a null object into the ASP.NET cache?
string exampleItem = null;
HttpRuntime.Cache.Insert("EXAMPLE_KEY",
exampleItem,
Nothing,
DateTime.Now.AddHours(1),
System.Web.Caching.Cache.NoSlidingExpiration);
The exception error message states that the “value” object cannot be null. In my application there are valid reasons why we whould want to store a null value in the cache.
Underlying
Cacheis likely aHashtableorDictionary<string, object>, whose getters do not differentiate between no value at that key, or a null value at that key.Consider a using placeholder “null” item, similar to
DbNull.Value.