how to iterate on cache keys present in cache in ehcache 3.5.2 version?

2,501 views
Skip to first unread message

Haritha

unread,
Jun 15, 2018, 2:05:54 AM6/15/18
to ehcache-users
  1. What version of Ehcache you are currently using;
  2. Paste the configuration for the Cache/CacheManager you have an issue with;
  3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);
  4. Providing JDK and OS versions maybe useful as well.

  1. What version of Ehcache you are currently using;  3.5.2
  2. Paste the configuration for the Cache/CacheManager you have an issue with; 
  3. I am in the process of migrating my web application from ehcache 2.6.11 version to 3.5.2. 
  4. Existing configuration : 
I have following code to remove all cached queries that starts with some string like "query-c" in ehcache 2.6.11 version.

public void removeAllCachedQueries() {
       
        Map<Object, Element> elements = cache.getAll(cache.getKeys());--> method present in 2.6.11 version.
        Set<Object> keys = elements.keySet();
        for (Object key : keys) {
            String cacheKey = (String) key;
            if (cacheKey.startsWith("query-c")) {
                cache.remove(key);
            }
        }
    }
As getKeys() method is not available in 3.5.2 version of ehcache,how to iterate over the cache keys in ehaccahe3 version?

  1. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);NA(ehcache3 API only )
  2. Providing JDK and OS versions maybe useful as well. JDK8 and Windows10

Louis Jacomet

unread,
Jun 15, 2018, 4:13:47 AM6/15/18
to ehcache-users
Hello,

The Cache interface extends java.lang.Iterable<Cache.Entry<K,V>> and so you can simply do something like:

for (Cache.Entry entry : cache) {
    // Do something
}

However, doing such an operation on a cache is always suspicious and you should strive for handling this situation differently.

Regards,
Louis

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/b1d4e889-ca54-45e3-a82d-6570921f0813%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Danish Gondal

unread,
Sep 18, 2019, 9:09:17 AM9/18/19
to ehcache-users

try this
  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);
    }

Chris Dennis

unread,
Sep 18, 2019, 11:00:58 AM9/18/19
to ehcach...@googlegroups.com

I’m guessing you’re using clustered caches? If so clustered iterators where not implemented until 3.7.1 (if I recall correctly). If this is the case then you should try upgrading to the latest version of Ehcache (3.8.1) and try again. If my clustered guess is wrong could you reply with your cache configuration and I’ll attempt to dig further.

 

Thanks,

 

Chris

--

You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages