I have a Google guava cache that is loading data from the database and caching it using the primary key. The objects I end up creating from the database are immutable and building an object requires access to several tables. What happens in the following scenario:
What does Guava Loading Cache do when invalidate(x) is called while load(x) is executing?
As currently specified in the Javadoc "No observable state associated with this cache is modified until loading completes". The semantics of loading are further specified as "Newly loaded values are added to the cache using Cache.asMap().putIfAbsent
after loading has completed."
You can also read the code to see where loading entries are ignored when invalidate or remove are called.
You can have two situations there:
Thread 1 reached the point of the actual loading (LocalCache.Segment.lockedGetOrLoad()
in 13.0.1) first, and the segment lock was acquired: in that case, the loading finishes, the lock is released and the computed value is returned to the caller, but it will be invalidated by Thread 2 when it runs (LocalCache.Segment.remove()
) and can acquire the lock.
Thread 2 acquired the lock before Thread 1 actually started loading: the invalidation doesn't really do anything, since the entry's not there yet, and Thread 1 then loads the up-to-date value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With