【问题标题】:Get size of entries in ehcache 3获取 ehcache 3 中条目的大小
【发布时间】:2017-07-06 09:46:19
【问题描述】:

我想使用ehcache3。对于一些 junit 测试,我需要知道缓存中条目的大小。我没有找到方法。 如何获取 ehcache 3 中条目(不是内存)的大小?

问候

本杰明

【问题讨论】:

    标签: java size ehcache ehcache-3


    【解决方案1】:

    因为它只是为了测试,你最好的选择是

        int count = 0;
        for(Cache.Entry<Long, String> entry : cache) {
            count++;
        }
    

    【讨论】:

    • 在while循环中添加it.next()
    • 对。谢谢。我让它变得更可爱了。
    【解决方案2】:

    对于 Ehcache 3.x,您可以从 loicmathieu 获得答案 here

    StatisticsService statisticsService = new DefaultStatisticsService();
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
        .using(statisticsService)
        .build();
    cacheManager.init();
    
    CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("myCache");
    ehCacheStat.getTierStatistics().get("OnHeap").getMappings();//nb element in heap tier
    ehCacheStat.getTierStatistics().get("OnHeap").getOccupiedByteSize()//size of the tier
    

    可以查看官方的Ehcache测试代码here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2013-06-11
      • 2011-06-25
      • 2014-09-29
      相关资源
      最近更新 更多