Search code examples
c#.netcachingmemorycacheasp.net-caching

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


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 ?


Solution

  • 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;