【问题标题】:what is the best way to check empty cache with MemoryCache Class?使用 MemoryCache 类检查空缓存的最佳方法是什么?
【发布时间】:2015-03-06 16:40:17
【问题描述】:

我正在使用简单的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];
        }

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

【问题讨论】:

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


【解决方案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;

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 2015-11-19
      • 2020-12-08
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      相关资源
      最近更新 更多