How to get ehcache 3.1 statistics

前端 未结 4 1346
忘掉有多难
忘掉有多难 2020-12-19 07:32

With Ehcache 3.1, I have a case to know the size of elements that are currently stored in the ehcache and also the number of hits and misses that the cache has so far. I thi

相关标签:
4条回答
  • 2020-12-19 07:59

    It's not yet documented but there is a cache statistics service that can give you the same statistics as in EhCache 2.X. I just tested it in the last version of EhCache (3.5).

    Here are a sample of how to configure it on your cache manager :

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

    Then later, after using the caches provided by this manager, you can ask the statistics service for the statistics. Here is a small sample :

    CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("myCache");
    ehCacheStat.getCacheHits();
    

    You can see all this in action in a unit test on the github of ehcache : https://github.com/ehcache/ehcache3/blob/master/integration-test/src/test/java/org/ehcache/integration/statistics/CacheCalculationTest.java

    0 讨论(0)
  • 2020-12-19 08:03

    Edit. This was wrong:

    For anyone else who was wondering, it looks a "public" statistics API will be exposed in version 3.2:

    https://github.com/ehcache/ehcache3/issues/1286

    See: https://groups.google.com/forum/#!category-topic/ehcache-users/ehcache-core/N1wqy6Hug38

    This is still true:

    I'm looking forward to this :)

    0 讨论(0)
  • 2020-12-19 08:05

    For Ehcache 3.5.2 as well as for any other JCache Provider you can use standard methods to enable statistics during cache configuration For instanse:

    ...
    MutableConfiguration<Path, String> config = new MutableConfiguration<>();
    config.setStatisticsEnabled(true);
    ...
    
    Cache myCache = cacheManager.createCache(CACHE_NAME, config);
    

    Then you could find register statics MXBean using the following method:

    public static CacheStatisticsMXBean getCacheStatisticsMXBean(final String cacheName) {
            final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
            ObjectName name = null;
            try {
                name = new ObjectName("*:type=CacheStatistics,*,Cache=" + cacheName);
            } catch (MalformedObjectNameException ex) {
                LOG.error("Someting wrong with ObjectName {}", ex);
            }
            Set<ObjectName> beans = mbeanServer.queryNames(name, null);
            if (beans.isEmpty()) {
                LOG.debug("Cache Statistics Bean not found");
                return null;
            }
            ObjectName[] objArray = beans.toArray(new ObjectName[beans.size()]);
            return JMX.newMBeanProxy(mbeanServer, objArray[0], CacheStatisticsMXBean.class);
        }
    

    And if it is found enjoy your statistics:

    CacheStatisticsMXBean CacheStatBean = getCacheStatisticsMXBean(cacheName);
                if (CacheStatBean != null) {
                    LOG.debug("Cache hits #{} misses #{}", CacheStatBean.getCacheHits(), CacheStatBean.getCacheMisses());
                    LOG.debug("Cache hits %{} misses %{}", CacheStatBean.getCacheHitPercentage(),
                            CacheStatBean.getCacheMissPercentage());
                    LOG.debug("Cache gets #{}", CacheStatBean.getCacheGets());
                    LOG.debug("Cache evictions #{}", CacheStatBean.getCacheEvictions());
                    LOG.debug("Cache average get time {} milliseconds", CacheStatBean.getAverageGetTime());
                }
    
    0 讨论(0)
  • 2020-12-19 08:06

    There is currently no exposed statistics API. It is on the roadmap but I cannot give you more specific information than that.

    An alternative is to use the JCache integration which offers a set of standard statistics exposed as MBeans.

    0 讨论(0)
提交回复
热议问题