Reputation: 558
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
insteadEhcache
is also not available in 3.x, so I can't store the result of ((EhCacheCache) cache).getNativeCache();
in thatEhcache
not being available in 3.x means getAll
functionality is not availableAny suggestions/ideas will be greatly appreciated!
Upvotes: 1
Views: 2001
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
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