Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all cache in ASP.NET Core 1

The version is rc1. In my old project there are codes like

System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache;
System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator();
while (cacheEnumerator.MoveNext())
{....}

In core 1 I use IMemoryCache and I can get cache by key like

var c = this._memoryCache;
var data = c.Get("data");

I want to make a view to list all cache. How can I get all cache in Core 1?

like image 491
MichaelMao Avatar asked May 26 '16 06:05

MichaelMao


People also ask

How can get data from cache in asp net?

From an ASP.NET page s code-behind class, the data cache can be accessed using the Page class s Cache property, and allows for syntax like Cache["key"] = value , as discussed in Step 2. From a class within the architecture, the data cache can be accessed using either HttpRuntime. Cache or HttpContext. Current.

Where is asp net cache stored?

The cached data is stored in server memory. Class Caching : Web pages or web services are compiled into a page class in the assembly, when run for the first time. Then the assembly is cached in the server.

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

Looking at the source of ASP.NET Core's MemoryCache implementation on github, you can see that the class doesn't expose any functionality to scan or iterate the list of cache entries.

The closest you can do is use reflection to read the inner cache dictionary:

 private readonly Dictionary<object, CacheEntry> _entries;

Wrap the reflection code in an extension method that accepts an IMemoryCache and returns an IEnumerable<CacheEntry> and you're good - but there are several risks involved:

  1. This implementation is specific to the current, pre-release version of ASP.NET CORE. This could change in newer versions, since it's an internal implementation detail and not part of the contract.
  2. This is specific to the implementation of Microsoft.Extensions.Caching.Memory.MemoryCache, which is just one implementation of IMemoryCache. You might replace it with a different implementation one day, which will break.
  3. The implementation contains a set of locks to manage concurrent access to the cache entries. If you iterate over the inner dictionary directly, you can run into concurrency errors when you try to read an entry while it's being updated. The first step is to never update the cache entry directly, and always go through IMemoryCache's methods, but even then, there's a risk.

Knowing the risks, though, you can go forth and get your job done.

like image 108
Avner Shahar-Kashtan Avatar answered Oct 20 '22 03:10

Avner Shahar-Kashtan