Cache.Add绝对过期 - UTC基或不? [英] Cache.Add absolute expiration - UTC based or not?

查看:166
本文介绍了Cache.Add绝对过期 - UTC基或不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关 Cache.Add 用途的例子 DateTime.Now.Add 来计算到期,也就是说,它传递:

The examples for Cache.Add uses DateTime.Now.Add to compute the expiration, i.e. it passes:

 DateTime.Now.AddSeconds(60)

absoluteExpiration 参数的值。

我还以为那它的计算相对 DateTime.UtcNow 会更正确的[因为没有歧义,如果夏令时开始的这段时间,从现在和到期点。

I'd have thought that computing it relative to DateTime.UtcNow would be more correct [as there is no ambiguity if Daylight Savings Time starts in the intervening time between now and the expiration point].

在引进 DateTimeKind 的,我已经猜到了,有就是在高速缓存管理一些丑陋的黑客让它做适当的事情,如果当时是不是UTC时间

Before the introduction of DateTimeKind, I'd have guessed that there's some ugly hacks in the cache management to make it do something appropriate if the time was not a UTC time.

在.NET 2.0及更高版本,我猜它应该处理的DateTime 计算 DateTime.UtcNow.AddSeconds(60)正确地考虑到它具有 DateTime.Kind 来作为输入其推论使用。

In .NET 2.0 and later, I'm guessing that it should handle a DateTime calculated as DateTime.UtcNow.AddSeconds(60) correctly given that it has DateTime.Kind to use as an input in its inferences.

我一直自信地使用 DateTime.UtcNow 为基准年,但wasnt能想出一个理由,这是绝对正确的事情在没有任何东西指出的文件已经严重误导了4年以上。

I've been confidently using DateTime.UtcNow as the base for years, but wasnt able to come up with a rationale that this is definitely the correct thing to do in the absence of anything pointing out the documentation has been highly misleading for 4+ years.

这些问题?

  1. 尽管很多bingage和谷歌上搜索我不是能够找到从MS有这方面的权威论述 - ?任何人都可以找到关于此
  2. 的东西
  3. 有没有为什么使用UtcNow不会是更正确和/或安全的任何原因?

(是的,我可以细读源和/或Reflector'd来源,但要寻找一个完整的打击逐打击内幕!)

(Yes, I could peruse the source and/or the Reflector'd source, but am looking for a full blow-by-blow lowdown!)

推荐答案

我的报这个错误在Microsoft Connect上的前一段时间,但它已经关闭,不会解决。

I reported this bug on Microsoft Connect some time ago, but it's been closed as won't fix.

您还有在.NET 2.0中的问题,如果你指定当地时间你绝对过期。

You still have a problem in .NET 2.0 if you specify your absolute expiration in local time.

在在夏令时结束一个小时的本地时间是模糊的,这样你就可以得到意想不到的结果,即绝对过期可以一小时比预期更长。

During one hour at the end of daylight savings time, your local time is ambiguous, so you can get unexpected results, i.e. the absolute expiration can be one hour longer than expected.

在欧洲,夏令时02:00 2009年10月结束了25下面的示例说明,如果您放置在缓存中的项目,在01:59以2分钟为到期,将保留在缓存中的一小时两分钟。

In Europe, daylight savings time ended at 02:00 on 25 October 2009. The sample below illustrates that if you placed an item in the cache at 01:59 with an expiration of 2 minutes, it would remain in the cache for one hour and two minutes.

DateTime startTime = new DateTime(2009, 10, 25, 1, 59,0);
DateTime endTime = startTime.AddMinutes(2);
// end time is two minutes after start time

DateTime startUtcTime = startTime.ToUniversalTime();
DateTime endUtcTime = endTime.ToUniversalTime();
// end UTC time is one hour and two minutes after start UTC time

Console.WriteLine("Start UTC time = " + startUtcTime.ToString());
Console.WriteLine("End UTC time = " + endUtcTime.ToString());

的变通方法.NET 2.0或更高版本是指定正如刚才鲁本在UTC的绝对到期时间。

The workaround for .NET 2.0 or later is to specify the absolute expiration time in UTC as pointed out by Ruben.

微软或许应该推荐使用UTC为绝对过期的例子,但我想有混淆的可能性,因为这一建议只对.NET 2.0和更高版本。

Microsoft should perhaps be recommending to use UTC in the examples for absolute expiration, but I guess there is the potential for confusion since this recommendation is only valid for .NET 2.0 and later.

修改

从注​​释:

但接触只发生,如果   重叠期间发生转换。   在单次转换实际上正在   地方是,当你提出与项目   Cache.Add

But the exposure only occurs if the conversion happens during the overlap. The single conversion actually taking place is when you lodge the item with Cache.Add

这个问题将只有当你在夏令时结束,一个暧昧小时内插入与本地时间AbsoluteExpiration时间缓存中的项目发生。

The problem will only happen if you insert an item in the cache with an AbsoluteExpiration time in local time during that one ambiguous hour at the end of daylight savings time.

因此​​,举例来说,如果你的本地时区是中欧(GMT + 1,冬天GMT + 2夏季),并执行以下code在1时59分零零秒于2009年10月25日:

So for example, if your local time zone is Central European (GMT+1 in winter, GMT+2 in summer), and you execute the following code at 01:59:00 on 25 October 2009:

DateTime absoluteExpiration = DateTime.Now.AddMinutes(2);
Cache.Add(... absoluteExpiration ...)

然后该项目将保留在高速缓存一小时,两分钟,而不是你通常希望的两分钟。这对于一些高度实时应用的问题(如股票行情,航班离港板)。

then the item will remain in the cache for one hour and two minutes, rather than the two minutes you would normally expect. This can be a problem for some highly time-critical applications (e.g. stock ticker, airline departures board).

这是怎么回事这里(假设欧洲时段,但原理是相同的任何时区):

What's happening here is (assuming European time, but the principle is the same for any time zone):

  • DateTime.Now = 2009-10-25 1时59分○○秒地方。当地= GMT + 2,所以UTC = 2009-10-24 23:59:00

  • DateTime.Now = 2009-10-25 01:59:00 local. local=GMT+2, so UTC = 2009-10-24 23:59:00

.AddMinutes(2)= 2009-10-25 2时零一分00秒地方。当地= GMT + 1,所以UTC = 2009-11-25 1时零一分00秒

.AddMinutes(2) = 2009-10-25 02:01:00 local. local = GMT+1, so UTC = 2009-11-25 01:01:00

Cache.Add在内部将所述到期时间与UTC(2009-11-25 1时○一分00秒),因此截止为一小时两分钟超前于当前的UTC时间(23时59分零零秒)

Cache.Add internally converts the expiration time to UTC (2009-11-25 01:01:00) so the expiration is one hour and two minutes ahead of the current UTC time (23:59:00).

如果你的地方DateTime.Now的使用DateTime.UtcNow,缓存过期将二分钟(.NET 2.0或更高版本):

If you use DateTime.UtcNow in place of DateTime.Now, the cache expiration will be two minutes (.NET 2.0 or later):

DateTime absoluteExpiration = DateTime.UtcNow.AddMinutes(2);
Cache.Add(... absoluteExpiration ...)

从注​​释:

From the comments:

还是我失去了一些东西?

Or am I missing something?

没有你不是。你的分析是对现货,如果您的应用程序的时间关键的,并且在此期间,在DST的结束运行时,你有权使用DateTime.UtcNow。

No you're not. Your analysis is spot on and if your application is time-critical and runs during that period at the end of DST, you're right to be using DateTime.UtcNow.

在鲁本的答案声明说:

你安全使用或者只要样你提供设定的时间

you're safe to use either as long as the Kind on the time you supply is set

不正确。

这篇关于Cache.Add绝对过期 - UTC基或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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