From 659bc8d1dbe87e4b681607af242717f3f8908daa Mon Sep 17 00:00:00 2001 From: Fuzzlemann Date: Tue, 15 Aug 2017 13:51:24 +0200 Subject: [PATCH] Adds the removal of the cached player pages on analysis --- .../plan/data/cache/PageCacheHandler.java | 20 ++++++++++++++++++- .../plan/utilities/analysis/Analysis.java | 2 ++ .../plan/data/cache/PageCacheHandlerTest.java | 9 +++++++++ .../plan/data/cache/SessionCacheTest.java | 12 ----------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java index b4b283dfe..55af7836a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/PageCacheHandler.java @@ -4,6 +4,9 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import main.java.com.djrapitops.plan.ui.webserver.response.Response; +import java.util.Map; +import java.util.function.Predicate; + /** * This class contains the page cache. *

@@ -77,10 +80,25 @@ public class PageCacheHandler { return pageCache.asMap().containsKey(identifier); } + /** + * Removes all of the elements of this cache that satisfy the given predicate. + * + * @param filter a predicate which returns true for entries to be removed + */ + public static void removeIf(Predicate filter) { + Map pageCacheMap = pageCache.asMap(); + + for (String identifier : pageCacheMap.keySet()) { + if (filter.test(identifier)) { + pageCache.invalidate(identifier); + } + } + } + /** * Clears the cache from all its contents. */ public static void clearCache() { - pageCache.asMap().clear(); + pageCache.invalidateAll(); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java index 182c26606..103be7822 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java @@ -165,8 +165,10 @@ public class Analysis { Log.info(Locale.get(Msg.ANALYSIS_FINISHED).parse(String.valueOf(time), HtmlUtils.getServerAnalysisUrlWithProtocol())); } + PageCacheHandler.removeIf(identifier -> identifier.startsWith("inspectPage: ")); PageCacheHandler.cachePage("analysisPage", () -> new AnalysisPageResponse(plugin.getUiServer().getDataReqHandler())); PageCacheHandler.cachePage("players", () -> new PlayersPageResponse(plugin)); + ExportUtility.export(analysisData, rawData); } catch (Exception e) { Log.toLog(this.getClass().getName(), e); diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java index c38446479..fef18ad14 100644 --- a/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/PageCacheHandlerTest.java @@ -50,4 +50,13 @@ public class PageCacheHandlerTest { PageCacheHandler.clearCache(); assertFalse(PageCacheHandler.isCached(IDENTIFIER)); } + + @Test + public void testRemoveIf() { + PageCacheHandler.cachePage(IDENTIFIER, LOADER); + assertTrue(PageCacheHandler.isCached(IDENTIFIER)); + + PageCacheHandler.removeIf(identifier -> identifier.equals(IDENTIFIER)); + assertFalse(PageCacheHandler.isCached(IDENTIFIER)); + } } diff --git a/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java index 9815f778d..c527869c1 100644 --- a/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java +++ b/Plan/src/test/java/main/java/com/djrapitops/plan/data/cache/SessionCacheTest.java @@ -36,18 +36,12 @@ public class SessionCacheTest { public SessionCacheTest() { } - /** - * - */ @Before public void setUp() throws Exception { TestInit.init(); test = new SessionCache(); } - /** - * - */ @Test public void testStartSession() { UUID uuid = MockUtils.getPlayerUUID(); @@ -55,9 +49,6 @@ public class SessionCacheTest { assertTrue("Didn't contain new session", test.getActiveSessions().containsKey(uuid)); } - /** - * - */ @Test public void testEndSession() { UUID uuid = MockUtils.getPlayerUUID(); @@ -69,9 +60,6 @@ public class SessionCacheTest { assertTrue("Session not valid", testSession.isValid()); } - /** - * - */ @Test public void testAddSession() { UUID uuid = MockUtils.getPlayerUUID();