From cc5adec5c5928ffe4ba179587fc9934d63983fe6 Mon Sep 17 00:00:00 2001 From: Antony1060 Date: Fri, 15 Jul 2022 21:20:13 +0200 Subject: [PATCH 1/2] Remove useless logic --- .../src/main/java/me/lucko/helper/cache/Expiring.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/helper/src/main/java/me/lucko/helper/cache/Expiring.java b/helper/src/main/java/me/lucko/helper/cache/Expiring.java index e1591306..4d5c3499 100644 --- a/helper/src/main/java/me/lucko/helper/cache/Expiring.java +++ b/helper/src/main/java/me/lucko/helper/cache/Expiring.java @@ -54,8 +54,7 @@ public static Expiring suppliedBy(Supplier supplier, long duration, Ti private volatile T value; - // when to expire. 0 means "not yet initialized". - private volatile long expirationNanos; + private volatile long expirationNanos = System.nanoTime(); private Expiring(Supplier supplier, long duration, TimeUnit unit) { this.supplier = supplier; @@ -67,7 +66,7 @@ public T get() { long nanos = this.expirationNanos; long now = System.nanoTime(); - if (nanos == 0 || now - nanos >= 0) { + if (now - nanos >= 0) { synchronized (this) { if (nanos == this.expirationNanos) { // recheck for lost race // compute the value using the delegate @@ -76,9 +75,8 @@ public T get() { // reset expiration timer nanos = now + this.durationNanos; - // In the very unlikely event that nanos is 0, set it to 1; - // no one will notice 1 ns of tardiness. - this.expirationNanos = (nanos == 0) ? 1 : nanos; + + this.expirationNanos = nanos; return t; } } From 85ffd72a3bd18866b65decba53d3c07d85177149 Mon Sep 17 00:00:00 2001 From: Antony1060 Date: Fri, 15 Jul 2022 22:10:59 +0200 Subject: [PATCH 2/2] Inline nanos assignment --- helper/src/main/java/me/lucko/helper/cache/Expiring.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helper/src/main/java/me/lucko/helper/cache/Expiring.java b/helper/src/main/java/me/lucko/helper/cache/Expiring.java index 4d5c3499..a2ddb0b7 100644 --- a/helper/src/main/java/me/lucko/helper/cache/Expiring.java +++ b/helper/src/main/java/me/lucko/helper/cache/Expiring.java @@ -74,9 +74,7 @@ public T get() { this.value = t; // reset expiration timer - nanos = now + this.durationNanos; - - this.expirationNanos = nanos; + this.expirationNanos = now + this.durationNanos; return t; } }