Cache.Insert(“”),绝对到期时间为UTC偏移量 [英] Cache.Insert("") with absolute expiration off by UTC offset

查看:121
本文介绍了Cache.Insert(“”),绝对到期时间为UTC偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在生产服务器上有一些asp.net Webforms站点,这些站点突然出现缓存问题。问题是使用Cache.Insert方法时我的缓存值未保留。

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.

例如,当我设置一个这样的值时,在检索它时它为null。

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!";

我需要能够设置缓存值的绝对到期时间,所以我不能使用Cache []方法。感谢所有帮助。谢谢。

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.

编辑:我发现将绝对到期时间设置为UTC日期时间确实是可行的。我相信问题是使用DateTime.Now时服务器没有将绝对到期转换为UTC。

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);

日期/时间和时区都按我在服务器上的预期设置,但是IIS可能没有

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?

推荐答案

这种行为变化可能是由于拥有.NET引起的? 4.7安装在机器上。下面链接的文章说,Microsoft将在下一版.NET和下一修补程序中对此进行修复。

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.

引用Microsoft页面的某些部分:

Quoting parts of the Microsoft page:

症状:


假定您在
上安装了Microsoft .NET Framework 4.7电脑。当您尝试使用
Cache.Insert(字符串,对象,CacheDependency,DateTime,TimeSpan)
将项目插入Cache对象时,您可能会注意到插入的Cache项目
过期早于指定的DateTime(过期
时间)。

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).

原因:


System.Web.Caching.Cache的内部实现使用
协调世界时(UTC)时间戳作为绝对到期时间。
但是此特定的Cache.Insert(字符串,对象,CacheDependecy,
DateTime,TimeSpan)插入重载方法不能确保
的到期时间是否转换为UTC。因此,通过使用此重载
插入Cache对象的
项的过期将比预期的更早或更晚,具体取决于计算机
与格林威治标准时间(GMT)的时区差异。

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).

解决方法:


临时解决此问题的方法是使用Cache.Add方法或其他Cache.Insert重载方法。

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

解决方案:


此问题将在.NET Framework的下一版本中修复,也将在.NET Framework 4.7的下一修补程序中提供。

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.

参考文献:

https:// support.microsoft.com/zh-CN/help/4035412/fix-expiration-time-issue-when-you-insert-items-by-使用缓存缓存

http://vimvq1987.com/2017/08/episerver-caching-issue-net-4-7/

这篇关于Cache.Insert(“”),绝对到期时间为UTC偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆