Ali
Ali

Reputation: 558

EhCache 3.9 -- Get All Cache Entries (working in EhCache 2.10)

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());
        ...
    }
}

Any suggestions/ideas will be greatly appreciated!

Upvotes: 1

Views: 2001

Answers (2)

EACUAMBA
EACUAMBA

Reputation: 611

Try this

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

Upvotes: 0

ozankyncu
ozankyncu

Reputation: 154

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())

Upvotes: 1

Related Questions