Using guava - why do we need LoadingCache.refresh
?
Can't we just use Cache.invalidate
and on our next get
the value would be refreshed?
The semantics are:
LoadingCache.refresh
: The existing mapping is still present and the cache continues to return the stored value until the new value is availableCache.invalidate
: The mapping discarded. A new value would be loaded upon the next LoadingCache.get()
If your application uses LoadingCache.get()
there may be no "logic" difference between both. If (maybe lots) of concurrent requests happen, then the application will behave differently:
LoadingCache.refresh
the application will constantly serve requests. The loader response time does not effect the application response time.LoadingCache.get()
, requests might block until the loading is completed. This might get dangerous, for example, lets say there are 1000 requests per second and your loading takes 5 seconds. That would mean that 5000 requests would be blocked and are waiting for the response. If one application request depends on multiple caches and loaders, this might be even worse.LoadingCache.refresh
you are still serving an possibly outdated value. You cannot use it if you need consistencyMain take away:
If not refreshed concurrently the load times add to your application response time and worsen the user experience.