问题
My problem with the code below is that is caches based on the list instead of per items in the list.
so if I send this method a list with 2 items, they both get cached, than make another call to the method with one of those items removed, it should read from the cache but instead it sees that the list has changed and so runs the method rather then using the cache.
What would be the correct annotation so that it caches on the individual items instead of the whole list?
@Override
@Cacheable(value = "cacheName")
public Properties retrieveProperties(List<String> testList) {
回答1:
There is no way to do what you want using Spring Caching annotations.
Also, what would be the semantics of the operation?
- The
Propertiescontent is driven by theListcontent? - or something else?
And how could a generic abstraction such as the one offered by Spring be able to cover these specific use cases?
So in short, you will have to write your own caching logic for such a specific use case.
回答2:
I think the problem here is that you missed declaring a key into @Cacheable annotation:
@Cacheable(value = "cacheName", key = "#root.method.name")
According to documentation, if a key is not especified, it uses all method parameters to create the key, so maybe if your list changes, it will store it separately.
If this is not the problem, it might be something on your configuration on ehcache.xml, maybe a "timeToLive" so short that causes the cache to reload.
Hope this will help.
来源:https://stackoverflow.com/questions/36486620/correct-key-annotation-for-caching-on-items-in-a-list-rather-then-the-whole-list