使用 MemoryCache Class 检查空缓存的最佳方法是什么?

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

我正在使用简单的 ObjectCacheMemoryCache class 来实现缓存。

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 ?

使用GetCount()方法。

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;

您可以尝试 GetCount() 方法来查看 MemoryCache 中有多少项。