java 运行时的 Ehcache 缓存大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13230003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ehcache cache size at runtime
提问by techarch
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
我正在生产中运行一个大型 VM,并且想了解更多有关运行时缓存大小的信息。我的缓存都是基于ehache
What is the best way to see the size of individual caches at runtime. Either using JMX or API
在运行时查看单个缓存大小的最佳方法是什么。使用 JMX 或 API
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?
是否有任何选项可以通过对 CacheManager 的普通 Java 调用进行配置,或者(暂时忽略 JMX)必须将 XML 配置构建成一个大字符串?
回答by pestrella
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.
是的,使用 Ehcache,您可以仅通过 Java 代码(无 XML 配置)配置缓存并检索它们的大小。集成所有内容的确切方法取决于您的特定架构;我将假设 Jersey 用于处理 API 的内容,而 Guice 用于依赖注入。
Defining your cache
定义缓存
Make your cache manager available via dependency injection. This can be done via a Guice module:
通过依赖注入使您的缓存管理器可用。这可以通过 Guice 模块完成:
@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;
}
Notice that statistics is turned on for mycache
.
请注意,为 开启了统计信息mycache
。
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.
同样,使用缓存可以完全在 Java 代码中完成,但取决于您的架构和设计。通常我使用方法拦截(通过 AOP)来做到这一点,但这是另一个话题。
Fetch cache stats via REST API
通过 REST API 获取缓存统计信息
Given your CacheManager
is available via dependency injection you can then wire it up to a REST endpoint and allow access to cache statistics:
鉴于您CacheManager
可以通过依赖注入获得,然后您可以将其连接到 REST 端点并允许访问缓存统计信息:
@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();
}
}
Now you can fetch information about your caches by REST call:
现在,您可以通过 REST 调用获取有关缓存的信息:
GET /stats
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
mycache: 8 objects, 59 hits, 12 misses
What about JMX?
JMX呢?
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
:
Ehcache 使向 MBean 服务器注册缓存管理器变得容易。它可以在 Java 代码中完成。更新您的 Guice 模块,将您cacheManager
的注册到系统MBeanServer
:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(cacheManager, mBeanServer, false, false, false, true);
Now you can attach JConsole to your Java process and find your cache statistics in the MBean net.sf.ehcache.CacheStatistics
.
现在您可以将 JConsole 附加到您的 Java 进程并在 MBean 中找到您的缓存统计信息net.sf.ehcache.CacheStatistics
。
回答by loicmathieu
In EhCache 3 (at least in the 3.5 version that I uses) you can access cache size via the cache statistics.
在 EhCache 3(至少在我使用的 3.5 版本中)中,您可以通过缓存统计信息访问缓存大小。
First, you need to register the statistic service on your cache manager :
首先,您需要在缓存管理器上注册统计服务:
StatisticsService statisticsService = new DefaultStatisticsService();
CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.using(statisticsService)
.build();
cacheManager.init();
Then, you can retrieve the statistics on your cache and it contains size by tiers (in EhCache 3 you have three different tiers : heap, disk and offheap)
然后,您可以检索缓存的统计信息,它包含按层划分的大小(在 EhCache 3 中,您有三个不同的层:堆、磁盘和堆外)
CacheStatistics ehCacheStat = statisticsService.getCacheStatistics("myCache");
ehCacheStat.getTierStatistics().get("OnHeap").getMappings();//nb element in heap tier
ehCacheStat.getTierStatistics().get("OnHeap").getOccupiedByteSize()//size of the tier
回答by Brian Agnew
JMX is definitely a viable solution. The EhCache doc has a pagespecifically for this.
JMX 绝对是一个可行的解决方案。EhCache 文档有一个专门用于此的页面。
Here's an exampleof configuring EhCache via JMX. The linked article contains a Spring config, but it's easily translatable to native Java if you're not using Spring.
这是通过 JMX 配置 EhCache的示例。链接的文章包含一个 Spring 配置,但如果您不使用 Spring,它很容易转换为原生 Java。