使用Ehcache3.1,我有理由知道当前存储在Ehcache中的元素的大小,以及缓存到目前为止的命中和未命中的数量。我认为2.6版本有.getStatistics(),它做了类似的事情,但是我正在努力在3.1版本中找到相同的功能。
感谢您的帮助!!
发布于 2018-08-17 03:51:18
对于Ehcache3.5.2以及任何其他JCache提供程序,您可以使用标准方法在实例的缓存配置期间启用统计信息:
...
MutableConfiguration<Path, String> config = new MutableConfiguration<>();
config.setStatisticsEnabled(true);
...
Cache myCache = cacheManager.createCache(CACHE_NAME, config);
然后,您可以使用以下方法找到寄存器静态MXBean:
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);
}
如果找到了,请享受您的统计数据:
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());
}
发布于 2016-11-07 18:20:33
目前还没有公开的统计API。它在路线图上,但我不能给你比这更具体的信息。
另一种方法是使用JCache集成,它提供了一组公开为MBeans的标准统计信息。
发布于 2018-03-15 23:31:46
它还没有文档,但是有一个缓存统计服务可以提供与EhCache 2.X相同的统计信息。我刚刚在最新版本的EhCache (3.5)中对其进行了测试。
下面是如何在缓存管理器上配置它的示例:
StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statisticsService)
.build();
cacheManager.init();
然后,在使用此管理器提供的缓存之后,您可以向统计服务请求统计信息。下面是一个小示例:
CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("myCache");
ehCacheStat.getCacheHits();
您可以在ehcache的github上的单元测试中看到所有这些内容:https://github.com/ehcache/ehcache3/blob/master/integration-test/src/test/java/org/ehcache/integration/statistics/CacheCalculationTest.java
https://stackoverflow.com/questions/40453859
复制相似问题