1

I'm working with cache in c# (Microsoft.Extensions.Caching.Memory) as i'm storing multiple data into cache like below.

IMemoryCache cache;

cache.Set("xxxx_1", "xxxx");
cache.Set("xxxx_2", "xxxx");
cache.Set("xxxx_3", "xxxx");

and can also get data one by one.

Getcachedata = (string)cache.Get("xxxx_1")

but i want to get the data complete data which contains Key like xxxx into it. i gone through many articles but didn't get help.

dev
  • 163
  • 1
  • 11
  • Your question is hard to understand - can you explain more, perhaps with some pseudo code what you want to achieve? what would your output look like? – sommmen Jan 03 '20 at 09:27
  • Does this answer your question? https://stackoverflow.com/a/43677373 – Nguyễn Văn Phong Jan 03 '20 at 09:28
  • See if second answer in [here](https://stackoverflow.com/questions/43673833/how-to-iterate-through-memorycache-in-asp-net-core?noredirect=1&lq=1) helps. – Rajesh G Jan 03 '20 at 09:43
  • @RajeshG with the solution mentioned i'm getting null key var keys = cache.Get>("abc.xyz"); but i can see key count with positive number – dev Jan 03 '20 at 11:04

1 Answers1

0

Try write CacheExtensions class to customize a GetCacheData method like below:

public static class CacheExtensions
{
    public static Dictionary<TKey,TValue> GetCacheData<TKey, TValue>(this IMemoryCache cache, TKey key)
    {
        var data = new Dictionary<TKey, TValue>();
        var value = (TValue)(cache.Get(key) ?? default(TValue));
        data.Add(key, value);
        return data;
    }
}

Then get the data result as shown: enter image description here

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36