Reputation: 31
I want to use ehcache3. For some junit tests, I need to know the size of entries in the cache. I dont find a method. How can get the size of entries (not memory) in ehcache 3?
Greets
Benjamin
Upvotes: 1
Views: 2376
Reputation: 307
For Ehcache 3.x you have the answer here from 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
You can check the official Ehcache test code here
Upvotes: 1
Reputation: 5721
Since it's only for test, your best bet is
int count = 0;
for(Cache.Entry<Long, String> entry : cache) {
count++;
}
Upvotes: 1