Concurrent calls to cached method

假装没事ソ 提交于 2019-12-25 07:05:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!