Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ehcache cache size at runtime

Tags:

java

ehcache

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

What is the best way to see the size of individual caches at runtime. Either using JMX or 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?

like image 420
techarch Avatar asked Nov 05 '12 10:11

techarch


People also ask

Is Ehcache in memory cache?

In this article, we will introduce Ehcache, a widely used, open-source Java-based cache. It features memory and disk stores, listeners, cache loaders, RESTful and SOAP APIs and other very useful features.

What happens when Ehcache is full?

In Ehcache, the MemoryStore may be limited in size (see How to Size Caches for more information). When the store gets full, elements are evicted.

What is difference between Ehcache and Redis cache?

You can think Redis as a shared data structure, while Ehcache is a memory block storing serialized data objects. This is the main difference. Redis as a shared data structure means you can put some predefined data structure (such as String, List, Set etc) in one language and retrieve it in another language.

Does Ehcache use heap?

Data stores supported by Ehcache include: On-Heap Store - Utilizes Java's on-heap RAM memory to store cache entries. This tier utilizes the same heap memory as your Java application, all of which must be scanned by the JVM's garbage collector.


1 Answers

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.

Defining your cache

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

Notice that statistics is turned on for 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.

Fetch cache stats via 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:

@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:

GET /stats

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

mycache: 8 objects, 59 hits, 12 misses

What about 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:

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.

like image 58
pestrella Avatar answered Sep 22 '22 23:09

pestrella