Ehcache cache size at runtime

前端 未结 3 2100
轻奢々
轻奢々 2021-01-02 19:24

I am running a large VM in production and would like to understand more about my cache size at runtime. My caches are all based upon ehache

What is the best way to s

3条回答
  •  情书的邮戳
    2021-01-02 19:59

    In EhCache 3 (at least in the 3.5 version that I uses) you can access cache size via the cache statistics.

    First, you need to register the statistic service on your cache manager :

    StatisticsService statisticsService = new DefaultStatisticsService();
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .using(statisticsService)
            .build();
    cacheManager.init();
    

    Then, you can retrieve the statistics on your cache and it contains size by tiers (in EhCache 3 you have three different tiers : heap, disk and offheap)

    CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("myCache");
    ehCacheStat.getTierStatistics().get("OnHeap").getMappings();//nb element in heap tier
    ehCacheStat.getTierStatistics().get("OnHeap").getOccupiedByteSize()//size of the tier
    

提交回复
热议问题