Made JSONCache static

This commit is contained in:
Rsl1122 2019-08-31 10:25:46 +03:00
parent 58bff282eb
commit efa0eec8a8
10 changed files with 48 additions and 66 deletions

View File

@ -32,15 +32,12 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class WebServerSystem implements SubSystem { public class WebServerSystem implements SubSystem {
private final JSONCache jsonCache;
private final WebServer webServer; private final WebServer webServer;
@Inject @Inject
public WebServerSystem( public WebServerSystem(
JSONCache jsonCache,
WebServer webServer WebServer webServer
) { ) {
this.jsonCache = jsonCache;
this.webServer = webServer; this.webServer = webServer;
} }
@ -53,8 +50,8 @@ public class WebServerSystem implements SubSystem {
public void disable() { public void disable() {
webServer.disable(); webServer.disable();
ResponseCache.clearCache(); ResponseCache.clearCache();
jsonCache.invalidateAll(); JSONCache.invalidateAll();
jsonCache.cleanUp(); JSONCache.cleanUp();
} }
public WebServer getWebServer() { public WebServer getWebServer() {

View File

@ -1,3 +1,19 @@
/*
* 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; package com.djrapitops.plan.delivery.webserver.cache;
import com.djrapitops.plan.delivery.webserver.response.Response; import com.djrapitops.plan.delivery.webserver.response.Response;
@ -6,8 +22,6 @@ import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -17,19 +31,13 @@ import java.util.function.Supplier;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@Singleton
public class JSONCache { public class JSONCache {
private final Cache<String, String> cache; private static final Cache<String, String> cache = Caffeine.newBuilder()
.expireAfterAccess(2, TimeUnit.MINUTES)
.build();
@Inject public static Response getOrCache(String identifier, Supplier<JSONResponse> jsonResponseSupplier) {
public JSONCache() {
cache = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.build();
}
public Response getOrCache(String identifier, Supplier<JSONResponse> jsonResponseSupplier) {
String found = cache.getIfPresent(identifier); String found = cache.getIfPresent(identifier);
if (found == null) { if (found == null) {
JSONResponse response = jsonResponseSupplier.get(); JSONResponse response = jsonResponseSupplier.get();
@ -39,22 +47,23 @@ public class JSONCache {
return new JSONResponse(found); return new JSONResponse(found);
} }
public Response getOrCache(DataID dataID, Supplier<JSONResponse> jsonResponseSupplier) { public static Response getOrCache(DataID dataID, Supplier<JSONResponse> jsonResponseSupplier) {
return getOrCache(dataID.name(), jsonResponseSupplier); return getOrCache(dataID.name(), jsonResponseSupplier);
} }
public Response getOrCache(DataID dataID, UUID serverUUID, Supplier<JSONResponse> jsonResponseSupplier) {
public static Response getOrCache(DataID dataID, UUID serverUUID, Supplier<JSONResponse> jsonResponseSupplier) {
return getOrCache(dataID.of(serverUUID), jsonResponseSupplier); return getOrCache(dataID.of(serverUUID), jsonResponseSupplier);
} }
public void invalidate(String identifier) { public static void invalidate(String identifier) {
cache.invalidate(identifier); cache.invalidate(identifier);
} }
public void invalidate(DataID dataID, UUID serverUUID) { public static void invalidate(DataID dataID, UUID serverUUID) {
cache.invalidate(dataID.of(serverUUID)); cache.invalidate(dataID.of(serverUUID));
} }
public void invalidateMatching(DataID dataID) { public static void invalidateMatching(DataID dataID) {
String toInvalidate = dataID.name(); String toInvalidate = dataID.name();
for (String identifier : cache.asMap().keySet()) { for (String identifier : cache.asMap().keySet()) {
if (StringUtils.startsWith(identifier, toInvalidate)) { if (StringUtils.startsWith(identifier, toInvalidate)) {
@ -63,11 +72,11 @@ public class JSONCache {
} }
} }
public void invalidateAll() { public static void invalidateAll() {
cache.invalidateAll(); cache.invalidateAll();
} }
public void cleanUp() { public static void cleanUp() {
cache.cleanUp(); cache.cleanUp();
} }
} }

View File

@ -45,17 +45,14 @@ public class GraphsJSONHandler implements PageHandler {
private final Identifiers identifiers; private final Identifiers identifiers;
private final GraphJSONParser graphJSON; private final GraphJSONParser graphJSON;
private final JSONCache cache;
@Inject @Inject
public GraphsJSONHandler( public GraphsJSONHandler(
Identifiers identifiers, Identifiers identifiers,
GraphJSONParser graphJSON, GraphJSONParser graphJSON
JSONCache cache
) { ) {
this.identifiers = identifiers; this.identifiers = identifiers;
this.graphJSON = graphJSON; this.graphJSON = graphJSON;
this.cache = cache;
} }
@Override @Override
@ -67,10 +64,10 @@ public class GraphsJSONHandler implements PageHandler {
if (target.getParameter("server").isPresent()) { if (target.getParameter("server").isPresent()) {
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
return cache.getOrCache(dataID, serverUUID, () -> generateGraphDataJSONOfType(dataID, serverUUID)); return JSONCache.getOrCache(dataID, serverUUID, () -> generateGraphDataJSONOfType(dataID, serverUUID));
} }
// Assume network // Assume network
return cache.getOrCache(dataID, () -> generateGraphDataJSONOfType(dataID)); return JSONCache.getOrCache(dataID, () -> generateGraphDataJSONOfType(dataID));
} }
private DataID getDataID(String type) throws BadRequestException { private DataID getDataID(String type) throws BadRequestException {

View File

@ -24,7 +24,6 @@ import com.djrapitops.plan.delivery.rendering.json.network.NetworkTabJSONParser;
import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.RequestTarget;
import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.auth.Authentication;
import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.delivery.webserver.pages.TreePageHandler; import com.djrapitops.plan.delivery.webserver.pages.TreePageHandler;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.exceptions.WebUserAuthException; import com.djrapitops.plan.exceptions.WebUserAuthException;
@ -40,19 +39,15 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class NetworkJSONHandler extends TreePageHandler { public class NetworkJSONHandler extends TreePageHandler {
private final JSONCache cache;
@Inject @Inject
public NetworkJSONHandler( public NetworkJSONHandler(
ResponseFactory responseFactory, ResponseFactory responseFactory,
JSONFactory jsonFactory, JSONFactory jsonFactory,
JSONCache cache,
NetworkOverviewJSONParser networkOverviewJSONParser, NetworkOverviewJSONParser networkOverviewJSONParser,
NetworkPlayerBaseOverviewJSONParser playerBaseOverviewJSONParser, NetworkPlayerBaseOverviewJSONParser playerBaseOverviewJSONParser,
NetworkSessionsOverviewJSONParser sessionsOverviewJSONParser NetworkSessionsOverviewJSONParser sessionsOverviewJSONParser
) { ) {
super(responseFactory); super(responseFactory);
this.cache = cache;
registerPage("overview", DataID.SERVER_OVERVIEW, networkOverviewJSONParser); registerPage("overview", DataID.SERVER_OVERVIEW, networkOverviewJSONParser);
registerPage("playerbaseOverview", DataID.PLAYERBASE_OVERVIEW, playerBaseOverviewJSONParser); registerPage("playerbaseOverview", DataID.PLAYERBASE_OVERVIEW, playerBaseOverviewJSONParser);
@ -62,7 +57,7 @@ public class NetworkJSONHandler extends TreePageHandler {
} }
private <T> void registerPage(String identifier, DataID dataID, NetworkTabJSONParser<T> tabJSONParser) { private <T> void registerPage(String identifier, DataID dataID, NetworkTabJSONParser<T> tabJSONParser) {
registerPage(identifier, new NetworkTabJSONHandler<>(dataID, cache, tabJSONParser)); registerPage(identifier, new NetworkTabJSONHandler<>(dataID, tabJSONParser));
} }
@Override @Override

View File

@ -37,18 +37,16 @@ import java.util.function.Supplier;
public class NetworkTabJSONHandler<T> implements PageHandler { public class NetworkTabJSONHandler<T> implements PageHandler {
private final DataID dataID; private final DataID dataID;
private final JSONCache cache;
private final Supplier<T> jsonParser; private final Supplier<T> jsonParser;
public NetworkTabJSONHandler(DataID dataID, JSONCache cache, NetworkTabJSONParser<T> jsonParser) { public NetworkTabJSONHandler(DataID dataID, NetworkTabJSONParser<T> jsonParser) {
this.dataID = dataID; this.dataID = dataID;
this.cache = cache;
this.jsonParser = jsonParser; this.jsonParser = jsonParser;
} }
@Override @Override
public Response getResponse(Request request, RequestTarget target) { public Response getResponse(Request request, RequestTarget target) {
return cache.getOrCache(dataID, () -> new JSONResponse(jsonParser.get())); return JSONCache.getOrCache(dataID, () -> new JSONResponse(jsonParser.get()));
} }
@Override @Override

View File

@ -44,23 +44,20 @@ public class PlayerKillsJSONHandler implements PageHandler {
private final Identifiers identifiers; private final Identifiers identifiers;
private final JSONFactory jsonFactory; private final JSONFactory jsonFactory;
private final JSONCache cache;
@Inject @Inject
public PlayerKillsJSONHandler( public PlayerKillsJSONHandler(
Identifiers identifiers, Identifiers identifiers,
JSONFactory jsonFactory, JSONFactory jsonFactory
JSONCache cache
) { ) {
this.identifiers = identifiers; this.identifiers = identifiers;
this.jsonFactory = jsonFactory; this.jsonFactory = jsonFactory;
this.cache = cache;
} }
@Override @Override
public Response getResponse(Request request, RequestTarget target) throws WebException { public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID serverUUID = identifiers.getServerUUID(target); UUID serverUUID = identifiers.getServerUUID(target);
return cache.getOrCache(DataID.KILLS, serverUUID, () -> return JSONCache.getOrCache(DataID.KILLS, serverUUID, () ->
new JSONResponse(Collections.singletonMap("player_kills", jsonFactory.serverPlayerKillsAsJSONMap(serverUUID))) new JSONResponse(Collections.singletonMap("player_kills", jsonFactory.serverPlayerKillsAsJSONMap(serverUUID)))
); );
} }

View File

@ -45,27 +45,24 @@ public class PlayersTableJSONHandler implements PageHandler {
private final Identifiers identifiers; private final Identifiers identifiers;
private final JSONFactory jsonFactory; private final JSONFactory jsonFactory;
private final JSONCache cache;
@Inject @Inject
public PlayersTableJSONHandler( public PlayersTableJSONHandler(
Identifiers identifiers, Identifiers identifiers,
JSONFactory jsonFactory, JSONFactory jsonFactory
JSONCache cache
) { ) {
this.identifiers = identifiers; this.identifiers = identifiers;
this.jsonFactory = jsonFactory; this.jsonFactory = jsonFactory;
this.cache = cache;
} }
@Override @Override
public Response getResponse(Request request, RequestTarget target) throws WebException { public Response getResponse(Request request, RequestTarget target) throws WebException {
if (target.getParameter("server").isPresent()) { if (target.getParameter("server").isPresent()) {
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
return cache.getOrCache(DataID.PLAYERS, serverUUID, () -> new JSONResponse(jsonFactory.serverPlayersTableJSON(serverUUID))); return JSONCache.getOrCache(DataID.PLAYERS, serverUUID, () -> new JSONResponse(jsonFactory.serverPlayersTableJSON(serverUUID)));
} }
// Assume network // Assume network
return cache.getOrCache(DataID.PLAYERS, () -> new JSONResponse(jsonFactory.networkPlayersTableJSON())); return JSONCache.getOrCache(DataID.PLAYERS, () -> new JSONResponse(jsonFactory.networkPlayersTableJSON()));
} }
@Override @Override

View File

@ -20,7 +20,6 @@ import com.djrapitops.plan.delivery.rendering.json.*;
import com.djrapitops.plan.delivery.webserver.RequestTarget; import com.djrapitops.plan.delivery.webserver.RequestTarget;
import com.djrapitops.plan.delivery.webserver.auth.Authentication; import com.djrapitops.plan.delivery.webserver.auth.Authentication;
import com.djrapitops.plan.delivery.webserver.cache.DataID; import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONCache;
import com.djrapitops.plan.delivery.webserver.pages.TreePageHandler; import com.djrapitops.plan.delivery.webserver.pages.TreePageHandler;
import com.djrapitops.plan.delivery.webserver.response.ResponseFactory; import com.djrapitops.plan.delivery.webserver.response.ResponseFactory;
import com.djrapitops.plan.exceptions.WebUserAuthException; import com.djrapitops.plan.exceptions.WebUserAuthException;
@ -37,15 +36,14 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class RootJSONHandler extends TreePageHandler { public class RootJSONHandler extends TreePageHandler {
private final JSONCache cache;
private Identifiers identifiers; private Identifiers identifiers;
@Inject @Inject
public RootJSONHandler( public RootJSONHandler(
ResponseFactory responseFactory, ResponseFactory responseFactory,
JSONCache cache,
Identifiers identifiers, Identifiers identifiers,
JSONFactory jsonFactory, JSONFactory jsonFactory,
GraphsJSONHandler graphsJSONHandler, GraphsJSONHandler graphsJSONHandler,
SessionsJSONHandler sessionsJSONHandler, SessionsJSONHandler sessionsJSONHandler,
PlayersTableJSONHandler playersTableJSONHandler, PlayersTableJSONHandler playersTableJSONHandler,
@ -56,11 +54,11 @@ public class RootJSONHandler extends TreePageHandler {
PvPPvEJSONParser pvPPvEJSONParser, PvPPvEJSONParser pvPPvEJSONParser,
PlayerBaseOverviewJSONParser playerBaseOverviewJSONParser, PlayerBaseOverviewJSONParser playerBaseOverviewJSONParser,
PerformanceJSONParser performanceJSONParser, PerformanceJSONParser performanceJSONParser,
PlayerJSONHandler playerJSONHandler, PlayerJSONHandler playerJSONHandler,
NetworkJSONHandler networkJSONHandler NetworkJSONHandler networkJSONHandler
) { ) {
super(responseFactory); super(responseFactory);
this.cache = cache;
this.identifiers = identifiers; this.identifiers = identifiers;
registerPage("players", playersTableJSONHandler, 1); registerPage("players", playersTableJSONHandler, 1);
@ -81,7 +79,7 @@ public class RootJSONHandler extends TreePageHandler {
} }
private <T> void registerPage(String identifier, DataID dataID, ServerTabJSONParser<T> tabJSONParser) { private <T> void registerPage(String identifier, DataID dataID, ServerTabJSONParser<T> tabJSONParser) {
registerPage(identifier, new ServerTabJSONHandler<>(dataID, cache, identifiers, tabJSONParser), 0); registerPage(identifier, new ServerTabJSONHandler<>(dataID, identifiers, tabJSONParser), 0);
} }
@Override @Override

View File

@ -40,18 +40,15 @@ import java.util.function.Function;
public class ServerTabJSONHandler<T> implements PageHandler { public class ServerTabJSONHandler<T> implements PageHandler {
private final DataID dataID; private final DataID dataID;
private final JSONCache cache;
private final Identifiers identifiers; private final Identifiers identifiers;
private final Function<UUID, T> jsonParser; private final Function<UUID, T> jsonParser;
public ServerTabJSONHandler( public ServerTabJSONHandler(
DataID dataID, DataID dataID,
JSONCache cache,
Identifiers identifiers, Identifiers identifiers,
ServerTabJSONParser<T> jsonParser ServerTabJSONParser<T> jsonParser
) { ) {
this.dataID = dataID; this.dataID = dataID;
this.cache = cache;
this.identifiers = identifiers; this.identifiers = identifiers;
this.jsonParser = jsonParser; this.jsonParser = jsonParser;
} }
@ -59,7 +56,7 @@ public class ServerTabJSONHandler<T> implements PageHandler {
@Override @Override
public Response getResponse(Request request, RequestTarget target) throws WebException { public Response getResponse(Request request, RequestTarget target) throws WebException {
UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException UUID serverUUID = identifiers.getServerUUID(target); // Can throw BadRequestException
return cache.getOrCache(dataID, serverUUID, () -> new JSONResponse(jsonParser.apply(serverUUID))); return JSONCache.getOrCache(dataID, serverUUID, () -> new JSONResponse(jsonParser.apply(serverUUID)));
} }
@Override @Override

View File

@ -44,29 +44,26 @@ public class SessionsJSONHandler implements PageHandler {
private final Identifiers identifiers; private final Identifiers identifiers;
private final JSONFactory jsonFactory; private final JSONFactory jsonFactory;
private final JSONCache cache;
@Inject @Inject
public SessionsJSONHandler( public SessionsJSONHandler(
Identifiers identifiers, Identifiers identifiers,
JSONFactory jsonFactory, JSONFactory jsonFactory
JSONCache cache
) { ) {
this.identifiers = identifiers; this.identifiers = identifiers;
this.jsonFactory = jsonFactory; this.jsonFactory = jsonFactory;
this.cache = cache;
} }
@Override @Override
public Response getResponse(Request request, RequestTarget target) throws WebException { public Response getResponse(Request request, RequestTarget target) throws WebException {
if (target.getParameter("server").isPresent()) { if (target.getParameter("server").isPresent()) {
UUID serverUUID = identifiers.getServerUUID(target); UUID serverUUID = identifiers.getServerUUID(target);
return cache.getOrCache(DataID.SESSIONS, serverUUID, () -> return JSONCache.getOrCache(DataID.SESSIONS, serverUUID, () ->
new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID))) new JSONResponse(Collections.singletonMap("sessions", jsonFactory.serverSessionsAsJSONMap(serverUUID)))
); );
} }
// Assume network // Assume network
return cache.getOrCache(DataID.SESSIONS, () -> return JSONCache.getOrCache(DataID.SESSIONS, () ->
new JSONResponse(Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap())) new JSONResponse(Collections.singletonMap("sessions", jsonFactory.networkSessionsAsJSONMap()))
); );
} }