4

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?

IUJPJ
  • 87
  • 6
  • 1
    What's the server time zone? Caching in ASP.NET using local time has been, historically, a pain. Use `DateTime.UtcNow` instead. – Adriano Repetti Jun 28 '17 at 18:00
  • Thanks Adriano. You're correct that changing it to UtcNow does fix the issue for me, but why would the behavior change suddenly? There were 4 Windows Updates applied the morning the Cache stopped working. – IUJPJ Jun 28 '17 at 18:08
  • Honestly I have no idea, maybe they fixed something else assuming (wrongly!) everyone uses UTC time. Wrongly because last time I saw msdn about it...they used local time in examples and they didn't mention this issue. – Adriano Repetti Jun 28 '17 at 18:11
  • I have a site that has been running for Years and now was dogging because of a possible patch? I changed my code --FROM-- Cache.Insert(CacheKey, dt, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero); --TO-- Cache.Insert(CacheKey, dt, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0)); – Elim Garak Jun 29 '17 at 12:54
  • Just encountered the same problem on my desktop after a few windows updates – JNappi Jul 07 '17 at 15:29
  • Then it might well be related to an update, breaking changes are rare but in general we should never use local time for caching (it will break also, for example, when time is updated). It just reinforces a good well established practice – Adriano Repetti Jul 09 '17 at 09:44
  • We recently experienced this as well, using absolute expiration with DateTime.Now.AddMinutes(2) was always failing. Switching to UtcNow made it work... – Gabe Aug 08 '17 at 23:06

1 Answers1

4

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:

Assume that you have Microsoft .NET Framework 4.7 installed on a computer. When you try to insert items into the Cache object by using the Cache.Insert (string, object, CacheDependency, DateTime, TimeSpan) Insert overload method, you may notice that the inserted Cache items expire much earlier or later than the specified DateTime (expiration time).

Cause:

The internal implementation of System.Web.Caching.Cache uses Coordinated Universal Time (UTC) time-stamp for absolute expiration. But this particular Cache.Insert (string, object, CacheDependecy, DateTime, TimeSpan) Insert overload method does not make sure whether the expiration time is converted to UTC. Therefore, expiration for items that are inserted into the Cache object by using this overload will occur earlier or later than expected, depending on the computer time zone difference from Greenwich Mean Time (GMT).

Workaround:

The temporary workaround for this issue is to use either the Cache.Add method or a different Cache.Insert overload method.

Resolution:

This issue will be fixed in the next version of the .NET Framework, and will also be available in the next hotfix for the .NET Framework 4.7.

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/