When adding cache entry to System.Runtime.Caching.MemoryCache, you can specify CacheItemPolicy with AbsoluteExpiration. According to documentation (https://msdn.microsoft.com/en-us/library/Dd780607(v=VS.110,d=hv.2).aspx), AbsoluteExpiration is "The period of time that must pass before a cache entry is evicted".
Yes that's what I want - to specify "the period of time" after which my cache entry will expire. But AbsoluteExpiration is of type DateTimeOffset not TimeSpan, so it's a "point in time" not "period of time". How should I set it then? E.g. if I want the entry to expire in 60 sec, should I be setting AbsoluteExpiration to DateTimeOffset.UtcNow.AddSeconds(60)
? Will I get the same result if I set it to DateTimeOffset.Now.AddSeconds(60)
? Any reason to use one or the other?
Well yes, the phrase "after a specified duration" in documentation is a little bit confusing but DateTimeOffset
type and AbsoluteExpiration
property name leave no doubt.
As regards the way how to construct instance of a DateTimeOffset
: there is no actually any difference in this case. DateTimeOffset.UtcNow.AddSeconds(60)
and DateTimeOffset.Now.AddSeconds(60)
will point to the same absolute time. These datetimes will differ by time zones but the moment of cache entry expiration will be the same in both cases. So there is no any reason to prefer one or another.