如何获取ehcache 3.1统计信息 [英] How to get ehcache 3.1 statistics

查看:191
本文介绍了如何获取ehcache 3.1统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ehcache 3.1中,我有一个案例来了解当前存储在ehcache中的元素的大小,以及到目前为止缓存已达到的命中和未命中的数量。我认为2.6具有.getStatistics(),它具有相似的功能,但是在3.1版本中却很难找到相同的功能。

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.

感谢您的帮助!

推荐答案

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

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

然后,您可以使用以下方法找到MXBean注册静态变量:

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

这篇关于如何获取ehcache 3.1统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆