Java 获取ehcache 3中条目的大小

Java 获取ehcache 3中条目的大小,java,size,ehcache,ehcache-3,Java,Size,Ehcache,Ehcache 3,我想使用ehcache3。对于一些junit测试,我需要知道缓存中条目的大小。我找不到办法。 如何在ehcache 3中获取条目(而不是内存)的大小 问候 本杰明因为这只是测试,所以你最好的选择是 int count = 0; for(Cache.Entry<Long, String> entry : cache) { count++; } int count=0; for(Cache.Entry:Cache){ 计数++; } 因为它只用于

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

问候


本杰明

因为这只是测试,所以你最好的选择是

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

因为它只用于测试,所以最好的选择是

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

对于Ehcache 3.x,您可以从loicmathieu那里得到答案:

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 3.x的官方Ehcache测试代码,您可以从loicmathieu获得答案:

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测试代码

add it.next()。谢谢我把它做得更可爱了。把它加进去。下一个()放在里面。谢谢我让它更可爱了。