Does Spring @Cacheable block if accessed by more that 1 thread?
If the result of the first execution hasn't been cached, the second invocation will proceed.
You should understand that @Cacheable
is centered around the content of the cache (and not specifically a thread's execution context [well, kind of; the cache still needs to be threadsafe]). On execution of a method, the cache is first checked to see if the key exists: if t1 is taking a while to complete, its result will not be cached therefore, concurrent executions will proceed without regard for t1's execution
There is no blocking on @Cacheable.
But you can use blocking cache strategy in cache implementation. First query found miss has the responsibility to rebuild the cache. Others queries wait until the cache is rebuilt.
- For local cache implementation, use
ReadWriteLock
. See thenet.sf.ehcache.constructs.blocking.BlockingCache
. - For remote cache implementation, use ghetto central lock.