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?
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.
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.
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.
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:
IMemoryCache
. You might replace it with a different implementation one day, which will break.IMemoryCache
's methods, but even then, there's a risk.Knowing the risks, though, you can go forth and get your job done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With