Minor value cache tweaks (#12493)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-03-19 08:43:12 +01:00 committed by GitHub
parent 1918afc3a9
commit 963a73ee74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,7 +12,8 @@
*/ */
package org.openhab.binding.danfossairunit.internal; package org.openhab.binding.danfossairunit.internal;
import java.util.Calendar; import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -21,7 +22,7 @@ import org.openhab.core.types.State;
/** /**
* The {@link ValueCache} is responsible for holding the last value of the channels for a * The {@link ValueCache} is responsible for holding the last value of the channels for a
* certain amount of time {@link ValueCache#durationMs} to prevent unnecessary event bus updates if the value didn't * certain amount of time {@link ValueCache#durationMillis} to prevent unnecessary event bus updates if the value didn't
* change. * change.
* *
* @author Robert Bach - Initial contribution * @author Robert Bach - Initial contribution
@ -29,39 +30,38 @@ import org.openhab.core.types.State;
@NonNullByDefault @NonNullByDefault
public class ValueCache { public class ValueCache {
private Map<String, StateWithTimestamp> stateByValue = new HashMap<>(); private final Map<String, StateWithTimestamp> stateByValue = new HashMap<>();
private final long durationMs; private final long durationMillis;
public ValueCache(long durationMs) { public ValueCache(long durationMillis) {
this.durationMs = durationMs; this.durationMillis = durationMillis;
} }
/** /**
* Updates or inserts the given value into the value cache. Returns true if there was no value in the cache * Updates or inserts the given value into the value cache. Returns true if there was no value in the cache
* for the given channelId or if the value has updated to a different value or if the value is older than * for the given channelId or if the value has updated to a different value or if the value is older than
* the cache duration * the cache duration.
*
* @param channelId the channel's id
* @param state new state
*/ */
public boolean updateValue(String channelId, State newState) { public boolean updateValue(String channelId, State state) {
long currentTimeMs = Calendar.getInstance().getTimeInMillis(); Instant now = Instant.now();
StateWithTimestamp oldState = stateByValue.get(channelId); StateWithTimestamp cachedValue = stateByValue.get(channelId);
boolean writeToCache; if (cachedValue == null || !state.equals(cachedValue.state)
if (oldState == null) { || cachedValue.timestamp.isBefore(now.minus(durationMillis, ChronoUnit.MILLIS))) {
writeToCache = true; stateByValue.put(channelId, new StateWithTimestamp(state, now));
} else { return true;
writeToCache = !oldState.state.equals(newState) || oldState.timestamp < (currentTimeMs - durationMs);
} }
if (writeToCache) { return false;
stateByValue.put(channelId, new StateWithTimestamp(newState, currentTimeMs));
}
return writeToCache;
} }
private static class StateWithTimestamp { private static class StateWithTimestamp {
State state; State state;
long timestamp; Instant timestamp;
public StateWithTimestamp(State state, long timestamp) { public StateWithTimestamp(State state, Instant timestamp) {
this.state = state; this.state = state;
this.timestamp = timestamp; this.timestamp = timestamp;
} }