Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Cached Objects which are cached using MemoryCache class c#

I want to retrieve all the cache objects that are added using MemoryCache.

I tried the below but it is not retrieving them

System.Web.HttpContext.Current.Cache.GetEnumerator();
System.Web.HttpRuntime.Cache.GetEnumerator();

Note: Retreive all means not just what i know and created, i mean every cache object that gets created in the application.

like image 509
YouKnowMe Avatar asked Jun 17 '15 18:06

YouKnowMe


People also ask

What is MemoryCache C#?

In-Memory Cache is used for when you want to implement cache in a single process. When the process dies, the cache dies with it. If you're running the same process on several servers, you will have a separate cache for each server. Persistent in-process Cache is when you back up your cache outside of process memory.

How do I check MemoryCache?

Right-click on the Start button and click on Task Manager. 2. On the Task Manager screen, click on the Performance tab > click on CPU in the left pane. In the right-pane, you will see L1, L2 and L3 Cache sizes listed under “Virtualization” section.

How do I retrieve a list of memory cache keys in ASP.NET Core?

1. use _cache. ToArray() or _cache. ToList() instead of _cache then you can start use LINQ.

Where is MemoryCache stored?

Memory caching (often simply referred to as caching) is a technique in which computer applications temporarily store data in a computer's main memory (i.e., random access memory, or RAM) to enable fast retrievals of that data. The RAM that is used for the temporary storage is known as the cache.


1 Answers

Here is a better way to enumerate and get the result:

public virtual List<T> GetCache<T>()
{
    List<T> list = new List<T>();
    IDictionaryEnumerator cacheEnumerator = (IDictionaryEnumerator)((IEnumerable)Cache).GetEnumerator();

    while (cacheEnumerator.MoveNext())
        list.Add((T) cacheEnumerator.Value);

    return list;
}
like image 56
Deepesh Bajracharya Avatar answered Sep 20 '22 16:09

Deepesh Bajracharya