pro.panopticon.client.sensor.impl.CachedSensor Maven / Gradle / Ivy
package pro.panopticon.client.sensor.impl;
import pro.panopticon.client.model.Measurement;
import pro.panopticon.client.sensor.Sensor;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAmount;
import java.util.List;
public abstract class CachedSensor implements Sensor {
private TemporalAmount cacheTime;
private LocalDateTime lastFetchTime;
private List cachedValue;
public CachedSensor(TemporalAmount cacheTime) {
this.cacheTime = cacheTime;
}
public abstract List calculateMeasurementsForCaching();
@Override
public List measure() {
if (lastFetchTime == null || cachedValue == null || lastFetchTime.isBefore(LocalDateTime.now().minus(cacheTime))) {
cachedValue = calculateMeasurementsForCaching();
lastFetchTime = LocalDateTime.now();
}
return cachedValue;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy