C# – Expiring a cached item via CacheItemPolicy in .NET MemoryCache

ccachingnet

I'm confused about the AbsoluteExpiration property on CacheItemPolicy.

The MSDN documentation for it says "The period of time that must pass before a cache entry is evicted." It uses a System.DateTimeOffset to define the "period of time".

But if you look at DateTimeOffset's MSDN documentation, it says that it "represents a point in time … relative to Coordinated Universal Time (UTC)." Reference also this StackOverflow thread.

Do you see the problem? AbsoluteExpiration expects a "period in time" (like 5 seconds or 2 hours), but it requires an object that represents a "point in time" (like Dec 21, 2012, 06:14:00 EST).

In the code below, I define a single policy for all items. I want every item to expire cacheExpiryInSeconds seconds after they are added. Can someone verify that I'm doing this the correct way?

public class MyCache : IRoutingInfoCache
{
    MemoryCache _routingInfoCache;
    CacheItemPolicy _cachePolicy;


    public MyCache(int cacheExpiryInSeconds)
    {
        _routingInfoCache = new MemoryCache("myCache");
        _cachePolicy = new CacheItemPolicy() {
            AbsoluteExpiration = 
                new DateTimeOffset(
                    DateTime.UtcNow.AddSeconds(cacheExpiryInSeconds))
        };
    }


    public void Put(string key, object cacheItem)
    {
        // based on how I constructed _cachePolicy, will this item expire
        // in cacheExpiryInSeconds seconds?
        _routingInfoCache.Add(new CacheItem(key, cacheItem), _cachePolicy);
    }
}

Best Answer

Caching adheres to UTC time to offer uniform time calculations, so you specify a point in time at which the cached entry should expire, in UTC, and the cache will calculate the appropriate difference from now and expire it as expected.

Your code will not work as expected since your absolute expiration will be before your cache item is entered once cacheExpiryInSeconds seconds pass, resulting in immediate eviction. You cannot share a CacheItemPolicy instance when AbsoluteExpiration is set in the near future, annoying I know. :)