首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何获取ehcache 3.1统计数据

如何获取ehcache 3.1统计数据
EN

Stack Overflow用户
提问于 2016-11-07 03:57:18
回答 3查看 6.6K关注 0票数 6

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

感谢您的帮助!!

EN

回答 3

Stack Overflow用户

发布于 2018-08-17 03:51:18

对于Ehcache3.5.2以及任何其他JCache提供程序,您可以使用标准方法在实例的缓存配置期间启用统计信息:

代码语言:javascript
复制
...
MutableConfiguration<Path, String> config = new MutableConfiguration<>();
config.setStatisticsEnabled(true);
...

Cache myCache = cacheManager.createCache(CACHE_NAME, config);

然后,您可以使用以下方法找到寄存器静态MXBean:

代码语言:javascript
复制
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);
    }

如果找到了,请享受您的统计数据:

代码语言:javascript
复制
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());
            }
票数 4
EN

Stack Overflow用户

发布于 2016-11-07 18:20:33

目前还没有公开的统计API。它在路线图上,但我不能给你比这更具体的信息。

另一种方法是使用JCache集成,它提供了一组公开为MBeans的标准统计信息。

票数 3
EN

Stack Overflow用户

发布于 2018-03-15 23:31:46

它还没有文档,但是有一个缓存统计服务可以提供与EhCache 2.X相同的统计信息。我刚刚在最新版本的EhCache (3.5)中对其进行了测试。

下面是如何在缓存管理器上配置它的示例:

代码语言:javascript
复制
    StatisticsService statisticsService = new DefaultStatisticsService();
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
            .using(statisticsService)
            .build();
    cacheManager.init();

然后,在使用此管理器提供的缓存之后,您可以向统计服务请求统计信息。下面是一个小示例:

代码语言:javascript
复制
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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40453859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档