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!!
Distributed – The data is held in a Terracotta Server Array with a subset of recently used data held in each application cache node. The distributed topology, available with BigMemory Max, supports a very rich set of consistency modes.
Ehcache can be configured in two ways: The first way is through Java POJO where all configuration parameters are configured through Ehcache API. The second way is configuration through XML file where we can configure Ehcache according to provided schema definition.
Data stores supported by Ehcache include: On-Heap Store - Utilizes Java's on-heap RAM memory to store cache entries. This tier utilizes the same heap memory as your Java application, all of which must be scanned by the JVM's garbage collector.
Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It's the most widely-used Java-based cache because it's robust, proven, full-featured, and integrates with other popular libraries and frameworks.
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());
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With