Adds the removal of the cached player pages on analysis

This commit is contained in:
Fuzzlemann 2017-08-15 13:51:24 +02:00
parent ad2a3d80ae
commit 659bc8d1db
4 changed files with 30 additions and 13 deletions

View File

@ -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.
* <p>
@ -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<String> filter) {
Map<String, Response> 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();
}
}

View File

@ -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);

View File

@ -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));
}
}

View File

@ -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();