Seemingly inconsistent invocations of eviction and removal listeners. #644
-
It seems that for some operations, like refresh after write and explicit invalidation, only removal listener is invoked, but for other operations, like expire after access and size based eviction, both eviction and removal listeners are invoked. Does that mean that when we have only removal listener invoked there is no way to do something with the old value atomically with, for example, reload action? I would expect to have either both listeners invoked every time or just one of them, specific to an action, but this different behavior of different actions is a bit puzzling. Could you please give a bit more detailes why it was implemented this way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
For explicit invalidation you can use the For refresh this operation is computed asynchronously. It invokes The background is that an issue in Guava was the lack of an atomic listener when an entry was automatically removed due to eviction. That might be needed for stateful resources like a local file, where it could be loaded anew before the async removal listener is run. In some other caches they have a concept of a For complex cases use |
Beta Was this translation helpful? Give feedback.
For explicit invalidation you can use the
asMap().compute
methods, where anull
return value causes the entry to be removed. As the computation is atomic, this can perform custom logic and it is more idiomatic as how you would do so in withConcurrentHashMap
(versus a plainremove(key)
).For refresh this operation is computed asynchronously. It invokes
CacheLoader#reload(key, oldValue)
via itsasyncReload
default method. There is no atomic listener here, but the intent is that you already had the old value to evaluate with. If the entry changes so that the reload is invalid then the replacement is discarded (with the new value notified as replaced). There isn't an atomic listener for this…