Confused about sync vs async refresh #1935
-
|
Let's say I have a
Based on the above, I have a few questions:
If I'm not doing anything with the old data, is is safe to ONLY implement the What happens when thousands of requests try to I asked these because in our testing we saw the load method being called a lot more than once per minute. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
The
Yes. The old value is provided in the rarer cases when you can perform a cheap version check to validate it is still up to date instead of a more costly full load. For example if fetching the value uses a provider's API quota but they offers a free HEAD ETag for the unique version identifier, then you can use that optimization trick to avoid the financial penalty.
Yes, the cache will memoize this, perform the call once, and ignore redundant attempts to refresh if no other writes occurred for that key. If the mapping was updated or removed while a refresh is in-flight then that reload will be abandoned. This is to try to provide per-key linearizable reload semantics to avoid the reload inserting a stale version of the data into the cache (e.g. Th1: start reload of v1 => Th2: update to v2 => Th1: drop (v1-current => v1-loaded) result and forward the reloaded value to the removal listener). If the test is overwriting the mapping while also performing explicit refreshes then you would see multiple in-flight reloads, but only the last valid one will replace the value (caveat wrt some unreleased fixes). This assumes v3, whereas prior versions did not make as strong a promise and was documented as more naive. If you are performing many concurrent reloads then I'd recommend considering coalescing reloads over a time/space period so that you can batch them more efficiently. Let me know if you have any further questions or clarifications. |
Beta Was this translation helpful? Give feedback.
The
LoadingCache.refreshdocumentation states "Caches loaded by a {@link CacheLoader} will call {@link CacheLoader#reload}..." which is the common case, but it doesn't preclude caches built from anAsyncCacheLoader. Internally the implementations only callasyncReloadand rely on the delegation chain to resolve to the implementation logic.AsyncLoadingCachestores a future into the cache and has asynchronous()view for aLoadingCache. It uses aAsyncCacheLoader, but can be given aCacheLoader(a subtype) for user convenience if their logic is best expressed synchronously and redefines the delegation chain fromasyncReload => asyncLoadtoasyncReload => reload => load. You can implement o…