aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-02-08 23:26:34 +0100
committernea <nea@nea.moe>2023-02-08 23:26:34 +0100
commitecdc1e8639ca930a5fb23732b500894f54914428 (patch)
treefa8615fe7dea12f296975483335e04f7c25a0d7b
parent26686a8b202d453893889d2c35efe1988ba11277 (diff)
downloadNotEnoughUpdates-ecdc1e8639ca930a5fb23732b500894f54914428.tar.gz
NotEnoughUpdates-ecdc1e8639ca930a5fb23732b500894f54914428.tar.bz2
NotEnoughUpdates-ecdc1e8639ca930a5fb23732b500894f54914428.zip
Lower cache for /pv to 30 seconds and rename cacheDuration to max age
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/profileviewer/ProfileViewer.java2
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java8
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/util/ApiCache.kt12
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<NameValuePair> 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<String> requestString() {
- return ApiCache.INSTANCE.cacheRequest(this, getCacheKey(), this::requestString0, cacheTimeout);
+ return ApiCache.INSTANCE.cacheRequest(this, getCacheKey(), this::requestString0, maxCacheAge);
}
public CompletableFuture<JsonObject> 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<CacheKey, CacheResult>()
+ private val cachedRequests = mutableMapOf<CacheKey, CacheResult>()
val histogramTotalRequests: MutableMap<String, Int> = mutableMapOf()
val histogramNonCachedRequests: MutableMap<String, Int> = 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()) {