carl_corder
carl_corder

Reputation: 143

Pulling single object out of SpringFramework Ehcache list

Using the org.springframework.cache.ehcache.EhCacheCacheManager & assuming I have the method:

@Cacheable("imageData") // add key = xyz here?
public List<ImageData> getProductIdAndColorCodeForSkus(List<Integer> skus) {
    thread sleep
    ...do some stuff...
    return listOfImageData;
}

And I have already hit this method with the skus {111111, 111222}. I would later like to pull an ImageData object out of the cache, for example:

Cache imageDataCache = cacheManager.getCache("imageData");
imageDataCache.get(key, ImageData.class) // what do I use for key?

I have tried

new Integer(111111)

but this will return back a null result. I have also tried a passing in a list with that sku as well.

Also, I know that both ImageData(s) are cached and can be retrieved individually because when I call this method a second time with a list having a single sku: 111111 or 111222 the thread sleep doesn't happen.

Upvotes: 0

Views: 154

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

The cache abstraction uses SimpleKeyGenerator by default to generate the key based on method arguments. In your case you have a List<Integer> so that's going to create a SimpleKey with the list.

If you're not happy with that, you can customize the KeyGenerator. Since you are accessing the cache directly somewhere else, I would strongly recommend you to take control on the key generator so that your API is consistent.

More info in the documentation

Upvotes: 2

Related Questions