Ehcache Keyword Statistics

I'm interested in getting the Ehcache statistics I'm working with.

I would like to see the number of hits / passes for a given key over a certain period of time. Perhaps in the form of a map. For example.

Over the past hour (or no matter how long it worked)

Key A had 30 hits and 2 misses
Key B had 400 hits and 100 misses
Key C had 2 hits and 1 miss
Key D had 150 hits and 10 misses

I looked through the documentation (SampledCacheStatistics, SampledCacheStatisticsImpl, SampledCacheStatisticsWrapper, etc.) and it is terribly difficult for me to understand.

Anyone else have experience implementing this?

Any help or ideas regarding this would be greatly appreciated!

+6
caching statistics ehcache
source share
2 answers

EhCache Monitor gives you this type of information ... http://ehcache.org/documentation/monitor.html

Access to the software is available as follows:

CacheManager cacheManager = CacheManager.getInstance(); String[] cacheNames = cacheManager.getCacheNames(); for (int i = 0; i < cacheNames.length; i++) { String cacheName = cacheNames[i]; System.out.println(cacheName+" - "+ cacheManager.getCache(cacheName).getStatistics().toString()); } 
+9
source share

You cannot track misses on the basis of each key, because statistics are stored in the object in the cache, and if there was a miss, there would be no element to track in the cache. But if you want to get the hit count for all keys in the cache, you need to do something like:

 public Map<Object,long> getKeyHits(Ehcache cache) { Map<Object,long> hitMap = new HashMap<Object,long>(); Map<Object,Element> allElements = cache.getAll(cache.getKeys()); for (Object key : allElements.keySet()) { hitMap.put(key, allElements.get(key).hitCount()); } return hitMap; } 

If you want to view statistics grouped across the cache (or you want to track misses), you can call getStatistics () in the cache. See http://ehcache.org/apidocs/net/sf/ehcache/Ehcache.html .

+1
source share

All Articles