gpt4 book ai didi

ehcache - 如何获取 ehcache 3.1 统计信息

转载 作者:行者123 更新时间:2023-12-01 09:17:31 30 4
gpt4 key购买 nike

使用 Ehcache 3.1,我可以知道当前存储在 ehcache 中的元素的大小以及缓存到目前为止的命中和未命中数。我认为 2.6 有 .getStatistics(),它做类似的事情,但是我在 3.1 版本中很难找到相同的功能。

感谢你的帮助!!

最佳答案

对于 Ehcache 3.5.2 以及任何其他 JCache Provider,您可以使用标准方法在缓存配置期间启用统计信息 例如:

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

关于ehcache - 如何获取 ehcache 3.1 统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453859/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com