All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.greencheek.caching.herdcache.lru.CacheRequestFutureComputationCompleteNotifier Maven / Gradle / Ivy

Go to download

A cache that uses futures to prevent thundering herds to your backend service

There is a newer version: 2.0.19
Show newest version
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