Ayania
Ayania

Reputation: 101

How to get ehcache 3.1 statistics

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 think the 2.6 has the .getStatistics(), which does the similar things, However the same feature i am struggling to find with 3.1 version.

Appreciate your help!!

Upvotes: 10

Views: 12796

Answers (4)

Vladislav Dembskiy
Vladislav Dembskiy

Reputation: 61

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());
            }

Upvotes: 6

loicmathieu
loicmathieu

Reputation: 5562

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

Upvotes: 7

phippsnatch
phippsnatch

Reputation: 186

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 :)

Upvotes: 0

Louis Jacomet
Louis Jacomet

Reputation: 14500

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.

Upvotes: 3

Related Questions