运行时的Ehcache缓存大小 [英] Ehcache cache size at runtime

查看:297
本文介绍了运行时的Ehcache缓存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生产中运行大型VM,并且想在运行时了解有关我的缓存大小的更多信息。我的缓存全部基于ehache

I am running a large VM in production and would like to understand more about my cache size at runtime. My caches are all based upon ehache

在运行时查看单个缓存大小的最佳方法是什么。使用JMX或API

What is the best way to see the size of individual caches at runtime. Either using JMX or API

是否可以通过对CacheManager的纯旧Java调用来配置任何选项,或者(暂时忽略JMX)必须构建XML配置大串?

Is there any option to configure by plain-old-java calls to the CacheManager, or (ignoring JMX for the moment) must one build the XML configuration slug up in a big string?

推荐答案

是的,使用Ehcache,您可以配置缓存并仅通过Java代码检索其大小(无XML配置)。集成所有内容的确切方法取决于您的特定体系结构。我将假设Jersey负责执行API任务,Guice进行依赖性注入。

Yes, using Ehcache, you can configure your caches and retrieve their sizes by Java code only (no XML config). The exact way to integrate everything depends on your specific architecture; I'm going to assume Jersey for doing API stuff and Guice for dependency injection.

定义缓存

通过依赖注入使您的缓存管理器可用。可以通过Guice模块完成此操作:

Make your cache manager available via dependency injection. This can be done via a Guice module:

@Provides
@Singleton
CacheManager provideCacheManager() {
  CacheManager cacheManager = CacheManager.create();

  /* very basic cache configuration */
  CacheConfiguration config = new CacheConfiguration("mycache", 100)
    .timeToLiveSeconds(60)
    .timeToIdleSeconds(30)
    .statistics(true);

  Cache myCache = new Cache(config);
  cacheManager.addCacheIfAbsent(myCache);

  return cacheManager;
}

请注意, mycache的统计信息已打开

同样,使用缓存可以完全用Java代码完成,但要取决于您的体系结构和设计。通常,我使用方法拦截(通过AOP)执行此操作,但这是另一个主题。

Again, using your cache can be done entirely in Java code but depends on your architecture and design. Typically I do this using method interception (via AOP) but that's another topic.

通过REST API获取缓存统计信息

鉴于您的 CacheManager 可通过依赖项注入获得,然后可以将其连接到REST端点并允许访问缓存统计信息:

Given your CacheManager is available via dependency injection you can then wire it up to a REST endpoint and allow access to cache statistics:

@Path("stats")
@Produces("text/plain")
public class StatsResource {
  @Inject private CacheManager cacheManager;

  @GET
  public String stats() {
    StringBuffer sb = StringBuffer();

    /* get stats for all known caches */
    for (String name : cacheManager.getCacheNames()) {
      Cache cache = cacheManager.getCache(name);
      Statistics stats = cache.getStatistics();

      sb.append(String.format("%s: %s objects, %s hits, %s misses\n",
        name,
        stats.getObjectCount(),
        stats.getCacheHits(),
        stats.getCacheMisses()
      ));
    }
    return sb.toString();
  }
}

现在,您可以通过REST调用获取有关缓存的信息:

Now you can fetch information about your caches by REST call:

GET /stats

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8

mycache: 8 objects, 59 hits, 12 misses

JMX呢?

Ehcache使在MBean服务器上注册缓存管理器变得容易。可以用Java代码完成。更新您的Guice模块,将您的 cacheManager 注册到系统 MBeanServer

Ehcache makes it easy to register your cache manger with an MBean server. It can be done in Java code. Update your Guice module, registering your cacheManager to the system MBeanServer:

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(cacheManager, mBeanServer, false, false, false, true);

现在您可以将JConsole附加到Java进程并在MBean中找到缓存统计信息 net.sf.ehcache.CacheStatistics

Now you can attach JConsole to your Java process and find your cache statistics in the MBean net.sf.ehcache.CacheStatistics.

这篇关于运行时的Ehcache缓存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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