Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Iterate on a cache entries

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.

like image 769
rayman Avatar asked Aug 06 '12 13:08

rayman


People also ask

How to iterate ehcache in Java?

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.

How to get value from ehcache in Java?

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.

What is ehcache used for?

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.

How do I use spring cache?

We can enable caching in the Spring Boot application by using the annotation @EnableCaching. It is defined in org. springframework. cache.

How to iterate through all Cached items in imemorycache?

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.

What is the best way to use 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. Example 27-2 illustrates a pattern to process query results when using large data sets.

Should I replace my memory cache with a distributed cache?

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.

Can a query be executed through a near Cache?

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.


1 Answers

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;
}
like image 107
Matt Brock Avatar answered Oct 13 '22 18:10

Matt Brock