mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-11-03 01:10:17 +01:00
Removed ResponseCache
This commit is contained in:
parent
b78d3b139e
commit
f426552b8c
@ -77,10 +77,7 @@ public class ExportSystem implements SubSystem {
|
||||
processing.submitNonCritical(htmlExport::exportAvailablePlayers);
|
||||
}
|
||||
if (config.isTrue(ExportSettings.SERVER_PAGE)) {
|
||||
processing.submitNonCritical(() -> {
|
||||
htmlExport.cacheNetworkPage();
|
||||
htmlExport.exportAvailableServerPages();
|
||||
});
|
||||
processing.submitNonCritical(htmlExport::exportAvailableServerPages);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,9 +20,6 @@ import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
|
||||
import com.djrapitops.plan.delivery.rendering.pages.NetworkPage;
|
||||
import com.djrapitops.plan.delivery.rendering.pages.PageFactory;
|
||||
import com.djrapitops.plan.delivery.rendering.pages.PlayerPage;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.response.pages.NetworkPageResponse;
|
||||
import com.djrapitops.plan.exceptions.ParseException;
|
||||
import com.djrapitops.plan.exceptions.database.DBOpException;
|
||||
import com.djrapitops.plan.gathering.domain.BaseUser;
|
||||
@ -169,33 +166,13 @@ public class HtmlExport extends SpecificExport {
|
||||
}
|
||||
}
|
||||
|
||||
public void cacheNetworkPage() {
|
||||
if (serverInfo.getServer().isNotProxy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkPage networkPage = pageFactory.networkPage();
|
||||
ResponseCache.cacheResponse(PageId.SERVER.of(serverInfo.getServerUUID()), () -> {
|
||||
try {
|
||||
return new NetworkPageResponse(networkPage);
|
||||
} catch (ParseException e) {
|
||||
errorHandler.log(L.WARN, this.getClass(), e);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void exportNetworkPage() {
|
||||
if (serverInfo.getServer().isNotProxy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cacheNetworkPage();
|
||||
try {
|
||||
exportAvailableServerPage(serverInfo.getServerUUID(), serverInfo.getServer().getName());
|
||||
} catch (IOException e) {
|
||||
errorHandler.log(L.WARN, this.getClass(), e);
|
||||
}
|
||||
NetworkPage networkPage = pageFactory.networkPage();
|
||||
// TODO
|
||||
}
|
||||
|
||||
public void exportAvailableServerPages() {
|
||||
|
@ -17,8 +17,6 @@
|
||||
package com.djrapitops.plan.delivery.export;
|
||||
|
||||
import com.djrapitops.plan.delivery.rendering.json.JSONFactory;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
import com.djrapitops.plan.storage.file.PlanFiles;
|
||||
@ -104,7 +102,7 @@ public abstract class SpecificExport {
|
||||
}
|
||||
|
||||
void exportAvailablePlayerPage(UUID playerUUID, String name) throws IOException {
|
||||
Response response = ResponseCache.loadResponse(PageId.PLAYER.of(playerUUID));
|
||||
Response response = null; // TODO
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
@ -115,7 +113,7 @@ public abstract class SpecificExport {
|
||||
|
||||
void exportAvailableServerPage(UUID serverUUID, String serverName) throws IOException {
|
||||
// TODO Force export in the future
|
||||
Response response = ResponseCache.loadResponse(PageId.SERVER.of(serverUUID));
|
||||
Response response = null;
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
|
@ -23,13 +23,13 @@ import com.djrapitops.plan.delivery.formatting.Formatters;
|
||||
import com.djrapitops.plan.delivery.rendering.html.Html;
|
||||
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
|
||||
import com.djrapitops.plan.delivery.rendering.html.structure.TabsElement;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.gathering.cache.SessionCache;
|
||||
import com.djrapitops.plan.gathering.domain.Session;
|
||||
import com.djrapitops.plan.identification.ServerInfo;
|
||||
import com.djrapitops.plan.identification.properties.ServerProperties;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.file.FileResource;
|
||||
import com.djrapitops.plan.storage.file.ResourceCache;
|
||||
import com.djrapitops.plan.version.VersionCheckSystem;
|
||||
import com.djrapitops.plugin.benchmarking.Benchmark;
|
||||
import com.djrapitops.plugin.benchmarking.Timings;
|
||||
@ -113,12 +113,11 @@ public class DebugPage implements Page {
|
||||
|
||||
private void appendResponseCache(StringBuilder content) {
|
||||
try {
|
||||
content.append("<pre>### Cached Responses:<br><br>");
|
||||
List<String> cacheKeys = new ArrayList<>(ResponseCache.getCacheKeys());
|
||||
content.append("<pre>### Cached Resources (from File or Jar):<br><br>");
|
||||
List<String> cacheKeys = ResourceCache.getCachedResourceNames();
|
||||
if (cacheKeys.isEmpty()) {
|
||||
content.append("Empty");
|
||||
}
|
||||
Collections.sort(cacheKeys);
|
||||
for (String cacheKey : cacheKeys) {
|
||||
content.append("- ").append(cacheKey).append("<br>");
|
||||
}
|
||||
|
@ -17,8 +17,6 @@
|
||||
package com.djrapitops.plan.delivery.webserver;
|
||||
|
||||
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.pages.*;
|
||||
import com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
@ -120,16 +118,16 @@ public class ResponseHandler extends TreePageHandler {
|
||||
String resource = target.getResourceString();
|
||||
|
||||
if (target.endsWith(".css")) {
|
||||
return ResponseCache.loadResponse(PageId.CSS.of(resource), () -> responseFactory.cssResponse(resource));
|
||||
return responseFactory.cssResponse(resource);
|
||||
}
|
||||
if (target.endsWith(".js")) {
|
||||
return ResponseCache.loadResponse(PageId.JS.of(resource), () -> responseFactory.javaScriptResponse(resource));
|
||||
return responseFactory.javaScriptResponse(resource);
|
||||
}
|
||||
if (target.endsWith(".png")) {
|
||||
return responseFactory.imageResponse(resource);
|
||||
}
|
||||
if (target.endsWith("favicon.ico")) {
|
||||
return ResponseCache.loadResponse(PageId.FAVICON.id(), responseFactory::faviconResponse);
|
||||
return responseFactory.faviconResponse();
|
||||
}
|
||||
|
||||
boolean isAuthRequired = webServer.get().isAuthRequired();
|
||||
|
@ -18,7 +18,6 @@ package com.djrapitops.plan.delivery.webserver;
|
||||
|
||||
import com.djrapitops.plan.SubSystem;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.exceptions.EnableException;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -49,7 +48,6 @@ public class WebServerSystem implements SubSystem {
|
||||
@Override
|
||||
public void disable() {
|
||||
webServer.disable();
|
||||
ResponseCache.clearCache();
|
||||
JSONCache.invalidateAll();
|
||||
JSONCache.cleanUp();
|
||||
}
|
||||
|
@ -1,130 +0,0 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.delivery.webserver.cache;
|
||||
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* This class contains the page cache.
|
||||
* <p>
|
||||
* It caches all Responses with their matching identifiers.
|
||||
* This reduces CPU cycles and the time to wait for loading the pages.
|
||||
* This is especially useful in situations where multiple clients are accessing the server.
|
||||
*
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class ResponseCache {
|
||||
|
||||
private static final Cache<String, Response> cache = Caffeine.newBuilder()
|
||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||
.build();
|
||||
|
||||
/**
|
||||
* Constructor used to hide the public constructor
|
||||
*/
|
||||
private ResponseCache() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the response from the response cache.
|
||||
* <p>
|
||||
* If the {@link Response} isn't cached, {@link Supplier#get()} in the {@code loader}
|
||||
* is called to create the Response.
|
||||
* <p>
|
||||
* If the Response is created, it's automatically cached.
|
||||
*
|
||||
* @param identifier The identifier of the page
|
||||
* @param loader The The {@link Response} {@link Supplier} (How should it load the page if it's not cached)
|
||||
* @return The Response that was cached or created by the the {@link Response} {@link Supplier}
|
||||
*/
|
||||
public static Response loadResponse(String identifier, Supplier<Response> loader) {
|
||||
return cache.get(identifier, k -> loader.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the page from the page cache.
|
||||
*
|
||||
* @param identifier The identifier of the page
|
||||
* @return The Response that was cached or {@code null} if it wasn't
|
||||
*/
|
||||
public static Response loadResponse(String identifier) {
|
||||
return cache.getIfPresent(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the page into the page cache.
|
||||
* <p>
|
||||
* If the cache already inherits that {@code identifier}, it's renewed.
|
||||
*
|
||||
* @param identifier The identifier of the page
|
||||
* @param loader The {@link Response} {@link Supplier} (How it should load the page)
|
||||
*/
|
||||
public static void cacheResponse(String identifier, Supplier<Response> loader) {
|
||||
cacheResponse(identifier, loader.get());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the page into the page cache.
|
||||
* <p>
|
||||
* If the cache already inherits that {@code identifier}, it's renewed.
|
||||
*
|
||||
* @param identifier The identifier of the page
|
||||
* @param response The {@link Response}
|
||||
*/
|
||||
public static void cacheResponse(String identifier, Response response) {
|
||||
if (response != null) {
|
||||
cache.put(identifier, response);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the page is cached.
|
||||
*
|
||||
* @param identifier The identifier of the page
|
||||
* @return true if the page is cached
|
||||
*/
|
||||
public static boolean isCached(String identifier) {
|
||||
return cache.getIfPresent(identifier) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the cache from all its contents.
|
||||
*/
|
||||
public static void clearCache() {
|
||||
cache.invalidateAll();
|
||||
}
|
||||
|
||||
public static Set<String> getCacheKeys() {
|
||||
return cache.asMap().keySet();
|
||||
}
|
||||
|
||||
public static long getEstimatedSize() {
|
||||
return cache.estimatedSize();
|
||||
}
|
||||
|
||||
public static void clearResponse(String identifier) {
|
||||
cache.invalidate(identifier);
|
||||
}
|
||||
}
|
@ -20,8 +20,6 @@ import com.djrapitops.plan.delivery.domain.WebUser;
|
||||
import com.djrapitops.plan.delivery.webserver.Request;
|
||||
import com.djrapitops.plan.delivery.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.exceptions.WebUserAuthException;
|
||||
@ -77,10 +75,9 @@ public class PlayerPageHandler implements PageHandler {
|
||||
throw new ForbiddenException("Database is " + dbState.name() + " - Please try again later. You can check database status with /plan info");
|
||||
}
|
||||
if (raw) {
|
||||
return ResponseCache.loadResponse(PageId.RAW_PLAYER.of(playerUUID), () -> responseFactory.rawPlayerPageResponse(playerUUID));
|
||||
return responseFactory.rawPlayerPageResponse(playerUUID);
|
||||
}
|
||||
return ResponseCache.loadResponse(PageId.PLAYER.of(playerUUID),
|
||||
() -> responseFactory.playerPageResponse(playerUUID));
|
||||
return responseFactory.playerPageResponse(playerUUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -19,8 +19,6 @@ package com.djrapitops.plan.delivery.webserver.pages;
|
||||
import com.djrapitops.plan.delivery.webserver.Request;
|
||||
import com.djrapitops.plan.delivery.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.exceptions.WebUserAuthException;
|
||||
@ -58,7 +56,7 @@ public class PlayersPageHandler implements PageHandler {
|
||||
if (dbState != Database.State.OPEN) {
|
||||
throw new ForbiddenException("Database is " + dbState.name() + " - Please try again later. You can check database status with /plan info");
|
||||
}
|
||||
return ResponseCache.loadResponse(PageId.PLAYERS.id(), responseFactory::playersPageResponse);
|
||||
return responseFactory.playersPageResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -19,8 +19,6 @@ package com.djrapitops.plan.delivery.webserver.pages;
|
||||
import com.djrapitops.plan.delivery.webserver.Request;
|
||||
import com.djrapitops.plan.delivery.webserver.RequestTarget;
|
||||
import com.djrapitops.plan.delivery.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.PageId;
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
|
||||
import com.djrapitops.plan.exceptions.WebUserAuthException;
|
||||
@ -64,19 +62,11 @@ public class ServerPageHandler implements PageHandler {
|
||||
public Response getResponse(Request request, RequestTarget target) throws WebException {
|
||||
UUID serverUUID = getServerUUID(target);
|
||||
|
||||
Response response = ResponseCache.loadResponse(PageId.SERVER.of(serverUUID));
|
||||
|
||||
if (response != null) {
|
||||
return response;
|
||||
} else {
|
||||
checkDBState();
|
||||
if (serverInfo.getServer().isProxy() && serverInfo.getServerUUID().equals(serverUUID)) {
|
||||
return ResponseCache.loadResponse(PageId.SERVER.of(serverUUID), responseFactory::networkPageResponse);
|
||||
}
|
||||
Response serverPageResponse = responseFactory.serverPageResponse(serverUUID);
|
||||
ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), serverPageResponse);
|
||||
return serverPageResponse;
|
||||
checkDBState();
|
||||
if (serverInfo.getServer().isProxy() && serverInfo.getServerUUID().equals(serverUUID)) {
|
||||
return responseFactory.networkPageResponse();
|
||||
}
|
||||
return responseFactory.serverPageResponse(serverUUID);
|
||||
}
|
||||
|
||||
private void checkDBState() throws ForbiddenException {
|
||||
|
@ -73,6 +73,8 @@ public class PlanFiles implements SubSystem {
|
||||
|
||||
@Override
|
||||
public void enable() throws EnableException {
|
||||
ResourceCache.invalidateAll();
|
||||
ResourceCache.cleanUp();
|
||||
Verify.isTrue((dataFolder.exists() && dataFolder.isDirectory()) || dataFolder.mkdirs(),
|
||||
() -> new EnableException("Could not create data folder at " + dataFolder.getAbsolutePath()));
|
||||
try {
|
||||
|
@ -19,6 +19,9 @@ package com.djrapitops.plan.storage.file;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@ -48,4 +51,18 @@ public class ResourceCache {
|
||||
public static void cache(String resourceName, String got) {
|
||||
cache.put(resourceName, got);
|
||||
}
|
||||
|
||||
public static void invalidateAll() {
|
||||
cache.invalidateAll();
|
||||
}
|
||||
|
||||
public static void cleanUp() {
|
||||
cache.cleanUp();
|
||||
}
|
||||
|
||||
public static List<String> getCachedResourceNames() {
|
||||
List<String> resourceNames = new ArrayList<>(cache.asMap().keySet());
|
||||
Collections.sort(resourceNames);
|
||||
return resourceNames;
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
* This file is part of Player Analytics (Plan).
|
||||
*
|
||||
* Plan is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License v3 as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Plan is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.delivery.webserver;
|
||||
|
||||
import com.djrapitops.plan.delivery.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.delivery.webserver.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import utilities.RandomData;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
@RunWith(JUnitPlatform.class)
|
||||
class ResponseCacheTest {
|
||||
private final String IDENTIFIER = RandomData.randomString(10);
|
||||
private final String RESPONSE_STRING = RandomData.randomString(10);
|
||||
private final Response RESPONSE = new Response() {
|
||||
@Override
|
||||
public String getResponse() {
|
||||
return RESPONSE_STRING;
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
void responseIsCachedIfNotFoundDuringLoad() {
|
||||
assertFalse(ResponseCache.isCached(IDENTIFIER));
|
||||
|
||||
Response response = ResponseCache.loadResponse(IDENTIFIER, () -> RESPONSE);
|
||||
assertTrue(ResponseCache.isCached(IDENTIFIER));
|
||||
|
||||
assertEquals(RESPONSE, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseIsClearedWhenCacheIsCleared() {
|
||||
ResponseCache.cacheResponse(IDENTIFIER, () -> RESPONSE);
|
||||
assertTrue(ResponseCache.isCached(IDENTIFIER));
|
||||
|
||||
ResponseCache.clearCache();
|
||||
assertFalse(ResponseCache.isCached(IDENTIFIER));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user