What does google guava loadcache do when a call is made to invalidate at boot time?

I have a Google guava cache that loads data from a database and caches it using the primary key. The objects that I create from the database are immutable, and accessing multiple tables is required to create the object. What happens in the following scenario:

  • Topic 1: calls cache.load (10), and the cache is populated based on the primary key of the database with a value of 10
  • Topic 2: updates the database row with primary key 10 and calls cache.invalidate (10), the invalidate called is called before cache.load (10) completes.

What does the Guava boot cache do when invalidate (x) is called when loading (x) is done?

+6
source share
2 answers

There may be two situations:

  • In thread 1, the actual load was achieved ( LocalCache.Segment.lockedGetOrLoad() in 13.0.1), and the segment lock was acquired : in this case, the load ends, the lock is released and the calculated value is returned to the caller, but it will be invalidated by Thread 2 during run it ( LocalCache.Segment.remove() ) and may get a lock.

  • Thread 2 acquired the lock before Thread 1 actually started loading: invalidation really does nothing, because the record does not exist yet, and Thread 1 loads the updated value.

+3
source

As stated in the Javadoc, "No observable state associated with this cache changes until the download is complete." The loading semantics is further specified as "New loaded values ​​are added to the cache using Cache.asMap().putIfAbsent after the download is complete."

You can also read the code to see where records are loaded, ignored when invalid or deleted are called.

+6
source

Source: https://habr.com/ru/post/927684/


All Articles