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

nl.hsac.fitnesse.fixture.util.selenium.caching.ObjectCache Maven / Gradle / Ivy

There is a newer version: 5.3.17
Show newest version
package nl.hsac.fitnesse.fixture.util.selenium.caching;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.util.function.Supplier;

/**
 * A wrapper around an object that is valid for an amount of time before its supplier must be called again.
 */
public class ObjectCache implements Cache {
    private final static Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    private final Supplier supplier;
    private T cachedValue;
    private long validUntil;

    public ObjectCache(Supplier supplier) {
        this.supplier = supplier;
    }

    @Override
    public T getValue() {
        long start = ElementCache.getTime();
        if (validUntil < start) {
            LOGGER.trace("Cache miss");
            T newValue = getNewValue();
            setValue(newValue, ElementCache.getValidityEnd(start));
        } else {
            LOGGER.debug("Cache hit");
        }
        return cachedValue;
    }
    
    @Override
    public T getNewValue() {
        return supplier.get();
    }

    @Override
    public void setValue(T value, long validUntil) {
        this.cachedValue = value;
        this.validUntil = validUntil;
    }

    @Override
    public long getValidUntil() {
        return validUntil;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy