aspnet/Caching

How to get cache size in .net core?

cex-pnaik opened this issue · 4 comments

My application is in .net core pointing to framework 1.1.1. I am using in-memory caching. After setting data in cache, I need to know the cache size. Can someone please tell me, how can this be done?

The count is not part of the public contract because it is very unstable, it can change at any moment even without a user action. Count is available for testing purposes if you downcast IMemoryCache to MemoryCache.

Or were you referring to total memory size?

Stable or not, it would be great to have option to check how many memory cache uses. Together with SignalRCore developers can build admin panel, where administrator could see memory usage graph.
If all the time 100% memory is used, system administrator can increase memory size limit. (By the way is this size in bytes?)

The Count property is exposed on the downcasted MemoryCache and represents the number of entries in the cache. Size is a concept that was more recently added but the total size of the cache is not publicly exposed so you do not have access to it. The definition of size (whether it's bytes or any other form of measurement) is determined by you. You indicate a size limit on the cache as well as the size of each entry during insertion.

The best way of determining whether the cache is using too much memory is to register eviction callbacks on the cache entries via

public static ICacheEntry RegisterPostEvictionCallback(
and examining the EvictionReason.

We periodically close 'discussion' issues that have not been updated in a long period of time.

We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.