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

时间:2015-03-06 16:40:17

标签: c# .net caching memorycache asp.net-caching

我使用简单的ObjectCacheMemoryCache类来实现缓存。

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];
        }
  

我想添加方法来检查空缓存,但不是基于任何密钥   只是想检查整个缓存是否为空?我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用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;

答案 1 :(得分:1)

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