1

The following code to fetch all caches is working in 2.10 (net.sf.ehcache.*)

public Map<CacheRegion, Map<Object, Element>> getCache() {
    Map<CacheRegion, Map<Object, Element>> cachedData = new HashMap<CacheRegion, Map<Object, Element>>();

    for (CacheRegion cacheRegion : CacheRegion.values()) {
        Cache cache = cacheManager.getCache(cacheRegion.getRegionName());
        Ehcache ehCache = ((EhCacheCache) cache).getNativeCache();
        Map<Object, Element> cacheMap = ehCache.getAll(ehCache.getKeys());
        cachedData.put(cacheRegion, cacheMap);
    }

    return cachedData;
}

// CacheRegion is an enum with cache keys

How can I achieve the same in EhCache 3.9 (org.ehcache.*)? I have at least 2 different types of entities being cached. I tried looking at https://stackoverflow.com/a/50389398/4669619 but that did not work for me. So far, I have replaced Element with Object.

public Map<CacheRegion, Map<Object, Object>> getCache() {
    Map<CacheRegion, Map<Object, Object>> cachedData = new HashMap<CacheRegion, Map<Object, Object>>();

    for (CacheRegion cacheRegion : CacheRegion.values()) {
        Cache cache = cacheManager.getCache(cacheRegion.getRegionName());
        ...
    }
}
  • Element in 2.x example can work for any/all entity. It's not available in 3.x, so I've tried to use Object instead
  • Ehcache is also not available in 3.x, so I can't store the result of ((EhCacheCache) cache).getNativeCache(); in that
  • Ehcache not being available in 3.x means getAll functionality is not available

Any suggestions/ideas will be greatly appreciated!

Ali
  • 558
  • 7
  • 28

2 Answers2

1

As mentioned in this post EHCache 3.5 Get All Cache Keys / Entries this is not a simple API call in Ehcache 3.

But you can get all keys like this.

StreamSupport.stream(cacheManager.getCache(cacheName).spliterator(), false).collect(Collectors.toList())
ozankyncu
  • 24
  • 3
0

Try this

 CacheManager.ALL_CACHE_MANAGERS.stream()
    .forEach(cacheManager -> Arrays.stream(cacheManager.getCacheNames()).forEach(s -> log.info("Cache {} -> {}", s, cacheManager.getCache(s))));
EACUAMBA
  • 461
  • 4
  • 8