Iterate in cache entries

I am using Spring3.1 in standalone Env. I cache my entry using the @Cachable annotation.

Sometimes I need to iterate over a caching list to get a specific value (not a key).

So, I managed to get a cached list, but how can I iterate over its elements.

private ClientDTO getClientDTOByClientId(Integer clientId) { Cache clientCache = null; try { clientCache = ehCacheCacheManager.getCache("client"); //need here to iterate on clientCache. how? } catch (Exception e) { log.error("Couldnt retrieve client from cache. clientId=" + clientId); } return clientDTO; } 

I use the ehcache mechanism.

 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" /> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" /> 

thanks, rays.

+6
source share
3 answers

CacheManager.getCache () returns net.sf.ehcache.Cache, which has a getKeys () method that returns a list of cache keys that you can iterate over. To get the actual object that was saved (as opposed to the wrapped net.sf.ehcache.Element file), use Element.getObjectValue ().

EDIT: According to Spring, it doesn't look like they will ever support Cache.getKeys () , so you have to drop it to the underlying provider.

Something like that:

 public boolean contains(String cacheName, Object o) { net.sf.ehcache.EhCache cache = (net.sf.ehcache.EhCache) org.springframework.cache.CacheManager.getCache(cacheName).getNativeCache(); for (Object key: cache.getKeys()) { Element element = cache.get(key); if (element != null && element.getObjectValue().equals(o)) { return true; } } return false; } 
+14
source

The method below will give a set of keys for cached objects, but here, if you add a cache with keys, it will be easy to get (if you add a list of objects directly to the cache, this will not work).

In addition, I used GuavaCache in the cache manager, as shown below.

 @Bean public CacheManager cacheManager() { SimpleCacheManager simpleCacheManager = new SimpleCacheManager(); GuavaCache userLabCache = new GuavaCache("yourCacheName", CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES).build()); simpleCacheManager.setCaches(Arrays.asList(userLabCache)); return simpleCacheManager; } 

Then it will return a list of keys in the form of objects, then you will get the keys and you can iterate over the keys and get objects one by one from the cache.

Autowire Springboot CacheManager in your class

 @Autowired CacheManager cacheManager; 

Below is the code to get all the keys.

 public Set<Object> getAllCachedUserLabKeys() { Set<Object> keys = null; try { GuavaCache cache = (GuavaCache) cacheManager.getCache("yourCacheName"); if (cache != null) { ConcurrentMap<Object, Object> cachedMap = cache.getNativeCache().asMap(); keys = cachedMap.keySet(); } } catch (Exception e) { LOGGER.error("Unknown exception occured while fetchting all cached User Labs..!", e); } return keys; } 
0
source

Another solution, parsing org.springframework.cache.Cache for javax.cache.Cache using the getNativeCache () method and using java iterator as javax.cache.Cache, already extends Iterable>.

for more details read javax.cache.Cache javadoc

  Cache cache = (Cache) cacheManager.getCache("yourCacheName").getNativeCache(); Iterator<Cache.Entry> iterator = cache.iterator(); while (iterator.hasNext()) { String key = (String) iterator.next().getKey(); System.out.println(key); } 
0
source

Source: https://habr.com/ru/post/922217/


All Articles