From c30650c0ee5ada9f9aa5d8e5114c05cad66f8861 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 30 Aug 2018 16:36:14 +0300 Subject: [PATCH] Changed ResponseCache to use caffeine (5 minute invalidation) #685 --- Plan/dependency-reduced-pom.xml | 4 --- Plan/pom.xml | 7 ++++ .../system/webserver/cache/ResponseCache.java | 35 +++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Plan/dependency-reduced-pom.xml b/Plan/dependency-reduced-pom.xml index 7b3d46293..4a214d000 100644 --- a/Plan/dependency-reduced-pom.xml +++ b/Plan/dependency-reduced-pom.xml @@ -306,10 +306,6 @@ guice com.google.inject - - caffeine - com.github.ben-manes.caffeine - guava com.github.ben-manes.caffeine diff --git a/Plan/pom.xml b/Plan/pom.xml index ea29ad87b..316381126 100644 --- a/Plan/pom.xml +++ b/Plan/pom.xml @@ -122,6 +122,13 @@ 2.9.0 + + + com.github.ben-manes.caffeine + caffeine + 2.6.2 + + org.bstats diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/cache/ResponseCache.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/cache/ResponseCache.java index 772e04180..0834953af 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/cache/ResponseCache.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/cache/ResponseCache.java @@ -1,10 +1,11 @@ package com.djrapitops.plan.system.webserver.cache; import com.djrapitops.plan.system.webserver.response.Response; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; -import java.util.HashMap; -import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.function.Supplier; /** @@ -19,7 +20,9 @@ import java.util.function.Supplier; */ public class ResponseCache { - private static final Map cache = new HashMap<>(); + private static final Cache cache = Caffeine.newBuilder() + .expireAfterWrite(5, TimeUnit.MINUTES) + .build(); /** * Constructor used to hide the public constructor @@ -41,17 +44,7 @@ public class ResponseCache { * @return The Response that was cached or created by the the {@link Response} {@link Supplier} */ public static Response loadResponse(String identifier, Supplier loader) { - Response response = loadResponse(identifier); - - if (response != null) { - return response; - } - - response = loader.get(); - - cache.put(identifier, response); - - return response; + return cache.get(identifier, k -> loader.get()); } /** @@ -61,7 +54,7 @@ public class ResponseCache { * @return The Response that was cached or {@code null} if it wasn't */ public static Response loadResponse(String identifier) { - return cache.get(identifier); + return cache.getIfPresent(identifier); } /** @@ -84,21 +77,25 @@ public class ResponseCache { * @return true if the page is cached */ public static boolean isCached(String identifier) { - return cache.containsKey(identifier); + return cache.getIfPresent(identifier) != null; } /** * Clears the cache from all its contents. */ public static void clearCache() { - cache.clear(); + cache.invalidateAll(); } public static Set getCacheKeys() { - return cache.keySet(); + return cache.asMap().keySet(); + } + + public static long getEstimatedSize() { + return cache.estimatedSize(); } public static void clearResponse(String identifier) { - cache.remove(identifier); + cache.invalidate(identifier); } }