问题
I want: Remove entity when both conditions are true
- timeout expired
- some external conditions are true
Question: How should I imply additional removal conditions besides timeout? Or how can I restore entity from removal listener (see code below)?
My code (which already remove based on timeout only):
LoadingCache<String, Integer> ints = CacheBuilder.newBuilder()
.maximumSize(10000)
.expireAfterAccess(ACCESS_TIMEOUT, TimeUnit.MILLISECONDS)
.removalListener(
new RemovalListener() {
public void onRemoval(RemovalNotification notification) {
if (notification.getCause() == RemovalCause.EXPIRED) {
if (!isExternalCondition()) {
//IS IT POSSIBLE TO RESTORE?
restore(notification.getValue());
}
} else {
System.out.println("Just removed for some reason");
}
}
}
)
.build(
new CacheLoader<String, Integer>() {
public Integer load(String key) throws Exception {
return new Integer(-1);
}
});
回答1:
Try Static class with isExternalCondition() method.
来源:https://stackoverflow.com/questions/21990133/guava-cachebuilder-imply-additional-conditions-to-entity-removal