From 595caa4aa85f81520a8b68d80afb889237c872ee Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Fri, 26 Jul 2019 00:53:10 -0400 Subject: [PATCH] Switch internal implementation of Cache refresh timing logic --- .../java/com/moparisthebest/jdbc/cache/Cache.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/common/src/main/java/com/moparisthebest/jdbc/cache/Cache.java b/common/src/main/java/com/moparisthebest/jdbc/cache/Cache.java index 045071c..6a7ec2c 100644 --- a/common/src/main/java/com/moparisthebest/jdbc/cache/Cache.java +++ b/common/src/main/java/com/moparisthebest/jdbc/cache/Cache.java @@ -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 implements Supplier { return new Cache<>(refreshInterval, supplier); } - private final Duration refreshInterval; - private Instant lastRefresh; + private final long refreshInterval; + private long nextRefresh; protected final Supplier supplier; @@ -29,18 +28,18 @@ public class Cache implements Supplier { protected Cache(final Duration refreshInterval, final Supplier 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