I am using Spring3.1 in standalone Env. I am caching my entry using @Cachable annotation.
Sometimes I need to iterate on the caching list in order to get specific value(not key).
So I managed to retrieve the cached list but how could I iterate on it's 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 using 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, ray.
ehcache. Cache, which has a getKeys() method that returns a list of cache keys that you can iterate over. To retrieve the actual object that's been stored (as opposed to the wrapped net.
Get a NonSerializable value from an element in a cache with a key of “key1”. Cache cache = manager. getCache("sampleCache1"); Element element = cache. get("key1"); Object value = element.
Ehcache is a standards-based caching API that is used by Integration Server. Caching enables an application to fetch frequently used data from memory (or other nearby resource) rather than having to retrieve it from a database or other back-end system each time the data is needed.
We can enable caching in the Spring Boot application by using the annotation @EnableCaching. It is defined in org. springframework. cache.
There's no available method in IMemoryCache that allows to iterate through each cached item. My project is small, I don't want to use other options like Redis.
If using a near cache with queries, the best approach is to use the following sequence: You can query large data sets in batches to guard against running out of heap space. Example 27-2 illustrates a pattern to process query results when using large data sets.
In a distributed cache you can't iterate over all keys as there are potentially millions of keys and this would significantly reduce the performance of the cached service if you could/would iterate over it. So the above solution is also friendly and ready for the case when you replace your memory cache with a distributed cache, such as Redis.
Although queries can be executed through a near cache, the query does not use the front portion of a near cache. If using a near cache with queries, the best approach is to use the following sequence: You can query large data sets in batches to guard against running out of heap space.
CacheManager.getCache() returns a net.sf.ehcache.Cache, which has a getKeys() method that returns a list of cache keys that you can iterate over. To retrieve the actual object that's been stored (as opposed to the wrapped net.sf.ehcache.Element), use the Element.getObjectValue().
EDIT: According to Spring it doesn't look like they will ever support Cache.getKeys(), so you'll have to cast to the underlying provider.
Something like this:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With