Simple question - How do I get a list of objects in the cache?

3,109 views
Skip to first unread message

Matt Lindsay

unread,
Dec 13, 2016, 2:39:12 PM12/13/16
to ehcache-users
  1. What version of Ehcache you are currently using; 3.1.1

Sorry all - very simple question (not sure why I'm finding it tough) - How do I return a list (I really just need keys) of everything in the cache?

There's a .getKeys in the javadoc but that's not available on either my cache or my cacheManager. Any reason for this?

I try 
Log.debug("Retrieving List of Keys Cache");
ArrayList<String> availableCatalogues = new ArrayList<String>();
availableCatalogues = cache.getAll();
but I get:
The method getAll(Set<? extends String>) in the type Cache<String,Catalogue> is not applicable for the arguments ()

basically... I don't know what what I should be passing it?

Many thanks

Matt Lindsay

unread,
Dec 13, 2016, 2:42:44 PM12/13/16
to ehcache-users
Sorry - I have found the 

getKeys() 
          Returns a list of all elements in the cache, whether or not they are expired.

but this is not available for me on cache.getKeys

I use:
Log.debug("Creating cache manager");
CacheManager cacheManager = myCacheManager.getCacheManager();
Cache<String, Catalogue> cache = cacheManager.getCache("myCatalogueCache", String.class, Catalogue.class);

Louis Jacomet

unread,
Dec 13, 2016, 3:31:07 PM12/13/16
to ehcache-users
Hi Matt,

That javadoc entry is for Ehcache 2.x not 3.x.

And since needing to know the set of keys in a cache is most often an anti-pattern, this method has been dropped from the Ehcache 3.x API. Note that it is also not present in the JCache API.

In last resort, a cache is iterable. But again, we consider such needs to be caching anti-patterns. The reason is that with expiry and eviction, there is not way to return something that can be "verified" anyway.

What are you trying to achieve?

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/d0108ff2-ce4d-4883-aabe-d484eede7724%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Lindsay

unread,
Dec 13, 2016, 3:37:47 PM12/13/16
to ehcach...@googlegroups.com
Thanks very much, that sort of makes sense. 

Perhaps I'm using caching in the wrong capacity. I'm basically using it like an in-memory database. I have a listing of catalogues that get uploaded by users via an api, and I want to return a listing of what's in the cache. For them to download them again. 

Ehcache was useful in this capacity, because one of the qualities I want is to be flushed when power off, and it's much easier to setup than a databases. 

I'll have a think about whether I'm doing the right thing. 

Many thanks. 

To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-users+unsubscribe@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/d0108ff2-ce4d-4883-aabe-d484eede7724%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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-users+unsubscribe@googlegroups.com.

To post to this group, send email to ehcach...@googlegroups.com.

Louis Jacomet

unread,
Dec 13, 2016, 3:48:22 PM12/13/16
to ehcach...@googlegroups.com
Hi Matt,

Indeed, Ehcache 2.x was often used as an in-memory store.
One of the principle we took when designing Ehcache 3 was to focus on the caching use case and do that better.

So indeed there probably is a disconnect there.

Regards,
Louis

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/d0108ff2-ce4d-4883-aabe-d484eede7724%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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.
--
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.
Reply all
Reply to author
Forward
0 new messages