what is the best way to check empty cache with MemoryCache Class?

2.4k views Asked by At

I'm using simple ObjectCache and MemoryCache class to implement cache.

public class MemoryCacheManager
    {
        protected ObjectCache Cache
        {
            get
            {
                return MemoryCache.Default;
            }
        }

        /// <summary>
        /// Gets or sets the value associated with the specified key.
        public virtual T Get<T>(string key)
        {
            return (T)Cache[key];
        }

I want to add method to check empty cache but not based on any key only wanted to check whether whole cache is empty or not how can I do so ?

2

There are 2 answers

0
EkoostikMartin On BEST ANSWER

Use the GetCount() method.

https://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.getcount(v=vs.110).aspx

var cache = MemoryCache.Default;

bool isEmpty = cache.GetCount() == 0;
0
Paul Griffin On

You could try the GetCount() method to see how many items are in the MemoryCache.