问题描述
我们发现在Google Guava中使用AddListener
补充RemovalListener
非常有用.
We'd find it very useful to have an AddListener
to complement RemovalListener
in Google Guava.
AddListener
是否有合适的替代品,可以补充RemovalListener
?
Is there a suitable alternative to an AddListener
that would complement RemovalListener
?
推荐答案
您可以自己提供-只需将自定义侦听器传递给计算Function
,并且每当计算一个值时,都将通知侦听器.
You can provide that yourself - just pass your custom listener to your computing Function
, and whenever you compute a value, notify the listener.
如果您使用的是asMap().put(..)
,则必须将整个内容包装在您的自定义类中,该类既委托原始对象,又调用附加侦听器.
If you are using asMap().put(..)
, then you'd have to wrap the whole thing in in your custom classes that delegate to the originals but also invoke the addition listener.
这篇关于Guava缓存侦听器,用于插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!