utk
utk

Reputation: 41

JCache CacheStatisticis with Cache Size

I have used JCache with EhCache as provider. Using the MBean I am able to get cache statistics but there is missing attributes like size of the cache. I see LiveCacheStatistics provide that but how to get instance of it from JCache

List<Map<String,Object>> cacheStatasticsList = new ArrayList<>();

try {
    final MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();

    final Set<ObjectInstance> cacheBeans = beanServer.queryMBeans(ObjectName.getInstance("javax.cache:type=CacheStatistics,CacheManager=*,Cache=*"), null);

    for (ObjectInstance cacheBean : cacheBeans) {
        final CacheStatisticsMXBean cacheStatisticsMXBean =
                MBeanServerInvocationHandler.newProxyInstance(beanServer, cacheBean.getObjectName(), CacheStatisticsMXBean.class, false);

        Map<String,Object> cacheStatasticsMap = new HashMap<>();
        cacheStatasticsMap.put("Name",cacheBean.getObjectName().getKeyProperty("Cache"));                
        cacheStatasticsMap.put("Gets",cacheStatisticsMXBean.getCacheGets());
        cacheStatasticsMap.put("Hits",cacheStatisticsMXBean.getCacheHits());
        cacheStatasticsMap.put("Misses",cacheStatisticsMXBean.getCacheMisses());
        cacheStatasticsMap.put("Removals",cacheStatisticsMXBean.getCacheRemovals());
        cacheStatasticsMap.put("Evictions", cacheStatisticsMXBean.getCacheEvictions());
        cacheStatasticsMap.put("AvgGetTime", cacheStatisticsMXBean.getAverageGetTime());
        cacheStatasticsMap.put("AvgPutTime", cacheStatisticsMXBean.getAveragePutTime());
        cacheStatasticsMap.put("AvgRemoveTime" , cacheStatisticsMXBean.getAverageRemoveTime());
        cacheStatasticsList.add(cacheStatasticsMap);
    }

} catch(Exception e){
    log.error("Error in getting cache statistics.");
    return cacheStatasticsList;
}
return cacheStatasticsList;

Upvotes: 4

Views: 2125

Answers (1)

cruftex
cruftex

Reputation: 5723

Unfortunately, the JSR107 standard does not define a size in the statistics object. See: CacheStatisticsMXBean

Some reasons that I recall from discussions with the expert group and spec leads and from my own experience as cache implementer:

  • For a distributed cache it is hard to determine the size. There might be a need to call the other nodes and the value might be somehow only an estimate
  • The JSR107 standard omitted to specify a size limit as well. This is because some implementations specify the the count of entries, some use bytes. So probably the "idea of size" was left out consequently in the statistics as well
  • If expiry is defined, the cache size can mean different things: the number of entries not expired yet, the number of entries occupying memory in the cache. The most logical measure would be the "entries not expired". But that would some cache implementations check for expired entries before the statistics can be returned

Although it is very painful, that the size is missing in the statistics, there are reasons not to include it. The metrics that are available now, are just a read out from counters. Depending on the implementation, to come up with the size, some heavier operations are needed.

Upvotes: 3

Related Questions