From ecdc1e8639ca930a5fb23732b500894f54914428 Mon Sep 17 00:00:00 2001 From: nea Date: Wed, 8 Feb 2023 23:26:34 +0100 Subject: Lower cache for /pv to 30 seconds and rename cacheDuration to max age --- .../notenoughupdates/profileviewer/ProfileViewer.java | 2 ++ .../io/github/moulberry/notenoughupdates/util/ApiUtil.java | 8 ++++---- .../io/github/moulberry/notenoughupdates/util/ApiCache.kt | 12 ++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java index 17a14d1f..6f8427ae 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java @@ -42,6 +42,7 @@ import net.minecraft.util.EnumChatFormatting; import javax.annotation.Nullable; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; @@ -537,6 +538,7 @@ public class ProfileViewer { manager.apiUtils .newHypixelApiRequest("player") .queryArgument("name", nameF) + .maxCacheAge(Duration.ofSeconds(30)) .requestJson() .thenAccept(jsonObject -> { if ( diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java index 9ace59fc..708f4677 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java @@ -117,7 +117,7 @@ public class ApiUtil { private final List queryArguments = new ArrayList<>(); private String baseUrl = null; private boolean shouldGunzip = false; - private Duration cacheTimeout = Duration.ofSeconds(500); + private Duration maxCacheAge = Duration.ofSeconds(500); private String method = "GET"; private String postData = null; private String postContentType = null; @@ -131,8 +131,8 @@ public class ApiUtil { * Specify a cache timeout of {@code null} to signify an uncacheable request. * Non {@code GET} requests are always uncacheable. */ - public Request cacheTimeout(Duration cacheTimeout) { - this.cacheTimeout = cacheTimeout; + public Request maxCacheAge(Duration maxCacheAge) { + this.maxCacheAge = maxCacheAge; return this; } @@ -244,7 +244,7 @@ public class ApiUtil { } public CompletableFuture requestString() { - return ApiCache.INSTANCE.cacheRequest(this, getCacheKey(), this::requestString0, cacheTimeout); + return ApiCache.INSTANCE.cacheRequest(this, getCacheKey(), this::requestString0, maxCacheAge); } public CompletableFuture requestJson() { diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt index 49a79ae9..d8f3f0f4 100644 --- a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt +++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt @@ -47,17 +47,17 @@ object ApiCache { val firedAt: TimeSource.Monotonic.ValueTimeMark, ) - val cachedRequests = mutableMapOf() + private val cachedRequests = mutableMapOf() val histogramTotalRequests: MutableMap = mutableMapOf() val histogramNonCachedRequests: MutableMap = mutableMapOf() - val timeout = 10.seconds - val maxCacheAge = 1.hours + private val timeout = 10.seconds + private val maxCacheAge = 1.hours - fun log(message: String) { + private fun log(message: String) { NEUDebugFlag.API_CACHE.log(message) } - fun traceApiRequest( + private fun traceApiRequest( request: Request, failReason: String?, ) { @@ -86,7 +86,7 @@ object ApiCache { } - fun evictCache() { + private fun evictCache() { synchronized(this) { val it = cachedRequests.iterator() while (it.hasNext()) { -- cgit