aspnet/Caching

In-memory cache GetOrCreate with MemoryCacheEntryOptions

Closed this issue · 1 comments

In current implementation IMemoryCache interface has the following methods:

bool TryGetValue(object key, out object value);
ICacheEntry CreateEntry(object key);
void Remove(object key);

We have the possibility to query the cache for an entry in the following way:

//first way
if (string.IsNullOrEmpty
(cache.Get<string>("timestamp")))
{
  cache.Set<string>("timestamp", DateTime.Now.ToString());
}

//second way
if (!cache.TryGetValue<string>
("timestamp", out string timestamp))
{
    //
    cache.Set<string>("timestamp", DateTime.Now.ToString());
}

Code from: http://binaryintellect.net/articles/a7d9edfd-1f86-45f8-a668-64cc86d8e248.aspx

But there is another method that should do what a cache should do (GetOrCreate) with a factory parameter:

public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, TItem> factory)
{
   object obj;
   if (!cache.TryGetValue(key, out obj))
   {
     ICacheEntry entry = cache.CreateEntry(key);
     obj = (object) factory(entry);
     entry.SetValue(obj);
     entry.Dispose();
   }
   return (TItem) obj;
}

As you can see above, the Set method accepts MemoryCacheEntryOptions or any absoluteExpirationRelativeToNow, absoluteExpiration, etc param dates (https://github.com/aspnet/Caching/blob/12f998d69703fb0f62b5cb1c123b76d63e0d04f0/src/Microsoft.Extensions.Caching.Abstractions/MemoryCacheExtensions.cs), but GetOrCreate method doesn't support that type of 'per-entry-expiration-date' for when we create a new entry.

I'm trying to figure out if i'm missing something or if i should do a PR to add those methods.

Annex:

public static ICacheEntry SetValue(this ICacheEntry entry, object value)
{
   entry.Value = value;
   return entry;
 }

Set methods will only set an entry's value + any 'per-entry-expiration-date' that you specify and you have access to those options inside that factory as they are part of ICacheEntry.

Example:

string timestamp = cache.GetOrCreate("timestamp", entry =>
{
    entry.SlidingExpiration = TimeSpan.FromSeconds(5);
    return DateTime.Now.ToString();
});

Stack related question: https://stackoverflow.com/a/50249981/2204877