Search code examples
c#.net-corememorycache

MemoryCache AbsoluteExpiration


When using MemoryCache it's possible to set

  • AbsoluteExpiration
  • AbsoluteExpirationRelativeToNow

Example:

    cache.GetOrCreate("key", f =>
    {
         f.AbsoluteExpiration = new DateTimeOffset(DateTime.Today.AddDays(1));
         f.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);
        return "item";
    });

Can both property be set at the same time, and the one that expires first will apply? Or will the last property that has been set be the "master" ?


Solution

  • Since those properties are of interface (ICacheEntry) - how they behave depend on concrete implementation. If we take default MemoryCache (and corresponding CacheEntry implementation) - then yes, they both can be set and, since they both represent absolute expiration, whichever happens earlier will be in effect and the other will be ignored.