Cache.Add absolute expiration - UTC based or not?

后端 未结 3 1457
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 19:31

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

 DateTime.Now.AddSeconds(60)

as th

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 20:00

    Cache.Add converts the expiration date to UTC before storing it.

    From Reflector (I've omitted most of the parameters to make it easier to read):

    public object Add(... DateTime absoluteExpiration ...)
    {
        DateTime utcAbsoluteExpiration = DateTimeUtil.ConvertToUniversalTime(absoluteExpiration);
        return this._cacheInternal.DoInsert(... utcAbsoluteExpiration ...);
    }
    

    In CacheExpires.FlushExpiredItems, utcAbsoluteExpiration is compared to DateTime.UtcNow. As Joe notes in his answer, this causes unexpected behavior when a cache item's addition and expiration span the end of daylight saving time.

提交回复
热议问题