org.greencheek.caching.herdcache.lru.CacheRequestFutureComputationCompleteNotifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of herdcache Show documentation
Show all versions of herdcache Show documentation
A cache that uses futures to prevent thundering herds to your backend service
package org.greencheek.caching.herdcache.lru;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.SettableFuture;
import java.util.function.Consumer;
/**
* Created by dominictootell on 28/07/2014.
*/
public class CacheRequestFutureComputationCompleteNotifier implements FutureCallback {
private final String cacheKey;
private final SettableFuture cacheRequestFuture;
private final CacheValueComputationFailureHandler failureHandler;
private final Consumer cacheResultConsumer;
public CacheRequestFutureComputationCompleteNotifier(String key, SettableFuture settableFuture,
CacheValueComputationFailureHandler failureHandler,
Consumer cacheResultConsumer) {
this.cacheKey = key;
this.cacheRequestFuture = settableFuture;
this.failureHandler = failureHandler;
this.cacheResultConsumer = cacheResultConsumer;
}
@Override
public void onSuccess(V result) {
cacheRequestFuture.set(result);
cacheResultConsumer.accept(result);
}
@Override
public void onFailure(Throwable t) {
failureHandler.onFailure(cacheKey,t);
cacheRequestFuture.setException(t);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy