I have a few asp.net webforms sites, on a production server, that are suddenly having caching problems. The issue is my cache values are not persisting when using the Cache.Insert method. Using Cache["key"] = value does still work though.
For example, when I set a value like this, it is null when I retrieve it.
HttpRuntime.Cache.Insert("CacheTestVal", "Help Me!" null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
When I set the value like this, I can retrieve the expected value
Cache["CacheTestVal"] = "Help Me!";
I need to be able to set an absolute expiration for the cache value, so I can't use the Cache[""] method. All help is appreciated. Thanks.
Edit: I found that setting the absolute expiration as a UTC datetime does work. I believe the problem is that the server is not converting the absolute expiration to UTC when using DateTime.Now.
HttpRuntime.Cache.Insert("CacheTestVal", "Help Me!" null, DateTime.UtcNow.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);
The date/time and timezone are all set as I would expect on the server, but maybe IIS doesn't recognize this, or there is a bad configuration value somewhere?
This change in behavior may be caused by having .NET 4.7 installed on the machine. The article linked below says that Microsoft will fix this in the next version of .NET and in the next hotfix.
Quoting parts of the Microsoft page:
Symptoms:
Cause:
Workaround:
Resolution:
References:
https://support.microsoft.com/en-us/help/4035412/fix-expiration-time-issue-when-you-insert-items-by-using-the-cache-ins
http://vimvq1987.com/2017/08/episerver-caching-issue-net-4-7/