Burny
Burny

Reputation: 31

Get size of entries in ehcache 3

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

Answers (2)

Marian
Marian

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

Henri
Henri

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

Related Questions