Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

In-memory cache GetOrCreate with MemoryCacheEntryOptions #392

@razvalex

Description

@razvalex

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;
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions