Switch internal implementation of Cache refresh timing logic

This commit is contained in:
Travis Burtrum 2019-07-26 00:53:10 -04:00
parent f290187ee0
commit 595caa4aa8
1 changed files with 6 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package com.moparisthebest.jdbc.cache;
import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import java.util.function.Supplier;
@ -19,8 +18,8 @@ public class Cache<T> implements Supplier<T> {
return new Cache<>(refreshInterval, supplier);
}
private final Duration refreshInterval;
private Instant lastRefresh;
private final long refreshInterval;
private long nextRefresh;
protected final Supplier<T> supplier;
@ -29,18 +28,18 @@ public class Cache<T> implements Supplier<T> {
protected Cache(final Duration refreshInterval, final Supplier<T> supplier) {
Objects.requireNonNull(refreshInterval);
// we are explicitly allowing supplier to be null, in which case get() will fail
this.refreshInterval = refreshInterval;
this.refreshInterval = refreshInterval.toMillis();
this.supplier = supplier;
// so refresh will be called immediately
this.lastRefresh = Instant.MIN;
this.nextRefresh = Long.MIN_VALUE;
}
protected boolean shouldRefresh() {
return Instant.now().isAfter(lastRefresh.plus(refreshInterval));
return System.currentTimeMillis() > nextRefresh;
}
protected void markRefreshed() {
this.lastRefresh = Instant.now();
this.nextRefresh = System.currentTimeMillis() + refreshInterval;
}
@Override