【问题标题】:Ehcache Statistics by keyEhcache 按键统计
【发布时间】:2011-01-01 17:14:31
【问题描述】:

我有兴趣获取有关我正在运行的 Ehcache 的统计信息。

我想查看给定键在一段时间内的命中/未命中次数。也许以地图的形式。例如。

过去的一个小时内(或者不管它已经运行了多久)

键 A 有 30 次命中和 2 次未命中
键 B 有 400 次命中和 100 次未命中
Key C 有 2 次安打和 1 次未命中
Key D 有 150 次命中和 10 次未命中

我查看了文档(SampledCacheStatistics、SampledCacheStatisticsImpl、SampledCacheStatisticsWrapper 等),但我很难弄清楚这一点。

有没有其他人有实现此功能的经验?

对此的任何帮助或想法将不胜感激!

【问题讨论】:

    标签: caching statistics ehcache


    【解决方案1】:

    EhCache Monitor 为您提供此类信息...http://ehcache.org/documentation/monitor.html

    编程访问方式如下:

        CacheManager cacheManager = CacheManager.getInstance();
        String[] cacheNames = cacheManager.getCacheNames();
        for (int i = 0; i < cacheNames.length; i++) {
            String cacheName = cacheNames[i];
            System.out.println(cacheName+" - "+ cacheManager.getCache(cacheName).getStatistics().toString());
        }
    

    【讨论】:

    • 太糟糕了,Terracotta 不再免费提供这个了。您必须使用他们的商业产品才能使用 ehCache 监视器。无赖
    • 这不会为您提供每个键的统计信息。全球性的
    【解决方案2】:

    您不能在每个键的基础上跟踪未命中,因为统计信息存储在缓存中的对象上,如果有未命中,缓存中将没有元素来跟踪它。但是,如果您想要缓存中所有键的命中计数,则需要执行以下操作:

    public Map<Object,long> getKeyHits(Ehcache cache)
    {
      Map<Object,long> hitMap = new HashMap<Object,long>();
      Map<Object,Element> allElements = cache.getAll(cache.getKeys());
      for (Object key : allElements.keySet())
      {
        hitMap.put(key, allElements.get(key).hitCount());
      }
      return hitMap;
    }
    

    如果您希望查看整个缓存的汇总统计信息(或者您想跟踪未命中),您可以在缓存上调用 getStatistics()。见http://ehcache.org/apidocs/net/sf/ehcache/Ehcache.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 2015-08-18
      • 1970-01-01
      相关资源
      最近更新 更多