【问题标题】:Getting the most info from .NET Cache从 .NET 缓存中获取最多信息
【发布时间】:2012-12-07 23:42:04
【问题描述】:

我正在尽可能多地检索有关 .NET 缓存使用的信息。

使用Cache 对象,我们可以检索 3 个参数

  • Count
  • EffectivePercentagePhysicalMemoryLimit
  • EffectivePrivateBytesLimit

但是其他的呢?

我在哪里可以获得诸如“服务器可用内存”、“已用缓存内存”等信息...

ASP Allience 中有一个名为 Cache Manager 的旧项目,但它不再可用,我只能找到它的图像,它确实显示如下:

我正在查看文档并阅读 System.Runtime.Cache 中的新 .NET 4 条目,例如 CacheMemoryLimitPhysicalMemoryLimit,但我找不到有关如何使用它的真实示例...

有没有人有缓存信息的包装器?或者知道如何使用这种可用的新方法?

我目前的缓存实现是:

public class InMemoryCache : ICacheService
{
    private int minutes = 15;

    public T Get<T>(string cacheID, Func<T> getItemCallback) where T : class
    {
        T item = HttpRuntime.Cache.Get(cacheID) as T;
        if (item == null)
        {
            item = getItemCallback();
            HttpRuntime.Cache.Insert(
                cacheID,
                item,
                null,
                DateTime.Now.AddMinutes(minutes),
                System.Web.Caching.Cache.NoSlidingExpiration);
        }
        return item;
    }

    public void Clear()
    {
        IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();

        while (enumerator.MoveNext())
            HttpRuntime.Cache.Remove(enumerator.Key.ToString());
    }

    public Dictionary<string, string> Stats()
    {
        var cache = HttpRuntime.Cache;
        var r = new Dictionary<string, string>();

        r.Add("Count", cache.Count.ToString());
        r.Add("EffectivePercentagePhysicalMemoryLimit", cache.EffectivePercentagePhysicalMemoryLimit.ToString());
        r.Add("EffectivePrivateBytesLimit", cache.EffectivePrivateBytesLimit.ToString());

        return r;
    }
}

【问题讨论】:

    标签: c#-4.0 caching


    【解决方案1】:

    看看这个: https://www.youtube.com/watch?v=Dz_7hukyejQ

    这是基于 100% 托管的自定义内存管理器,该管理器在大小为 256 mb 的 byte[] 段中存储缓存项目。这允许存储数以百万计的对象而不会减慢任何速度,因为 GC 看不到“对象”,因为它们驻留在 byte[] 中。

    该视频展示了如何查看缓存的运行情况并查看有多少对象、页面、优先级等......

    代码如下:

    https://github.com/aumcode/nfx/tree/master/Source/NFX/ApplicationModel/Pile

    即缓存的主界面:

    https://github.com/aumcode/nfx/blob/master/Source/NFX/ApplicationModel/Pile/ICache.cs

    您可以使用内存限制和条目优先级来命名表、基于年龄或绝对时间戳到期

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2011-05-13
      • 2012-08-18
      • 1970-01-01
      • 2011-01-05
      • 2012-01-04
      相关资源
      最近更新 更多