问题
I'm using Spring cache abstraction with Guava cache. I have a method with @Cacheable annotation and parameter (that serves as a cache key) to put values into the cache. But this method is accessed in a multi threaded env so there are multiple concurrent calls to the method with the same parameter value. So that means the same logic that creates the value to be cached is done for the same cache key multiple times and put into the cache multiple times concurrently. It'd be much more efficient if for each parameter value (cache key) the method would be called only once and put into the cache once. Can Spring handle such a scenario?
回答1:
As of Spring Framework 4.3 (still in early development phase at the time of writing) a new flag on @Cacheable
called sync
is available. If you enable that flag, you opt-in for basically what you're asking.
Spring Framework 4.3 GA is due around May next year but you should see a first milestone in Q1 2016 still. Please give that a try and let us know if that works for you.
来源:https://stackoverflow.com/questions/32340954/concurrent-calls-to-cached-method