6

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!!

Ayania
  • 61
  • 1
  • 2

4 Answers4

4

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

loicmathieu
  • 4,032
  • 17
  • 26
  • Ehcache dev here : while this may work, this is not official API, see answer from @LouisJacomet - this API could change in the future – Anthony Dahanne Mar 15 '18 at 17:24
  • There also exist an ehcache management modules but it's very complicated to make it works because of missing transitive dependencies in it's pom. It's documented here http://www.ehcache.org/documentation/3.4/management.html but I don't know if it is supported ... As there didn't seems to be any support from EhCache dev for a clear statistic solutions (waiting since 2 years!) I'll stick with the StatisticService and hope for EhCache dev to make it official some days ... – loicmathieu Mar 16 '18 at 11:07
  • Can you also get the size in bytes using this? That was possible in EhCache 2.X, but I can't find a way to do it in EhCache 3.5.3 – Niby Jul 16 '19 at 13:59
4

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());
            }
Fredrik Widerberg
  • 2,914
  • 10
  • 28
  • 41
  • I used this answer to access JCache stats, works like a charm. But note that if you're running multiple apps on the same Tomcat JVM, the MBeanServer will return *all* caches across all contexts. In this case you'll need to filter. I'm using the 'CacheManager' path on the ObjectName. – Simon Oct 25 '19 at 08:36
3

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.

Louis Jacomet
  • 11,574
  • 2
  • 27
  • 36
  • Is there any news about this? After a couple of hours googling, I'm very confused about the state of statistics/monitoring in ehcache 3. Is this the recommended method?: https://www.ehcache.org/documentation/3.6/management.html – Jose Duarte Dec 20 '18 at 00:39
  • 4
    if we had it in Ehcache 2 why has it been lost in Ehcache 3 ? – Paul Taylor Apr 03 '19 at 13:57
0

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

phippsnatch
  • 194
  • 7