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
public class WebServerSystem implements SubSystem {
private final JSONCache jsonCache;
private final WebServer webServer;
@Inject
public WebServerSystem(
JSONCache jsonCache,
WebServer webServer
) {
this.jsonCache = jsonCache;
this.webServer = webServer;
}
@ -53,8 +50,8 @@ public class WebServerSystem implements SubSystem {
public void disable() {
webServer.disable();
ResponseCache.clearCache();
jsonCache.invalidateAll();
jsonCache.cleanUp();
JSONCache.invalidateAll();
JSONCache.cleanUp();
}
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;
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 org.apache.commons.lang3.StringUtils;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
@ -17,19 +31,13 @@ import java.util.function.Supplier;
*
* @author Rsl1122
*/
@Singleton
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 JSONCache() {
cache = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.build();
}
public Response getOrCache(String identifier, Supplier<JSONResponse> jsonResponseSupplier) {
public static Response getOrCache(String identifier, Supplier<JSONResponse> jsonResponseSupplier) {
String found = cache.getIfPresent(identifier);
if (found == null) {
JSONResponse response = jsonResponseSupplier.get();
@ -39,22 +47,23 @@ public class JSONCache {
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);
}
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);
}
public void invalidate(String identifier) {
public static void invalidate(String identifier) {
cache.invalidate(identifier);
}
public void invalidate(DataID dataID, UUID serverUUID) {
public static void invalidate(DataID dataID, UUID serverUUID) {
cache.invalidate(dataID.of(serverUUID));
}
public void invalidateMatching(DataID dataID) {
public static void invalidateMatching(DataID dataID) {
String toInvalidate = dataID.name();
for (String identifier : cache.asMap().keySet()) {
if (StringUtils.startsWith(identifier, toInvalidate)) {
@ -63,11 +72,11 @@ public class JSONCache {
}
}
public void invalidateAll() {
public static void invalidateAll() {
cache.invalidateAll();
}
public void cleanUp() {
public static void cleanUp() {
cache.cleanUp();
}
}

View File

@ -45,17 +45,14 @@ public class GraphsJSONHandler implements PageHandler {
private final Identifiers identifiers;
private final GraphJSONParser graphJSON;
private final JSONCache cache;
@Inject
public GraphsJSONHandler(
Identifiers identifiers,
GraphJSONParser graphJSON,
JSONCache cache
GraphJSONParser graphJSON
) {
this.identifiers = identifiers;
this.graphJSON = graphJSON;
this.cache = cache;
}
@Override
@ -67,10 +64,10 @@ public class GraphsJSONHandler implements PageHandler {
if (target.getParameter("server").isPresent()) {
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
return cache.getOrCache(dataID, () -> generateGraphDataJSONOfType(dataID));
return JSONCache.getOrCache(dataID, () -> generateGraphDataJSONOfType(dataID));
}
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.auth.Authentication;
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.response.ResponseFactory;
import com.djrapitops.plan.exceptions.WebUserAuthException;
@ -40,19 +39,15 @@ import javax.inject.Singleton;
@Singleton
public class NetworkJSONHandler extends TreePageHandler {
private final JSONCache cache;
@Inject
public NetworkJSONHandler(
ResponseFactory responseFactory,
JSONFactory jsonFactory,
JSONCache cache,
NetworkOverviewJSONParser networkOverviewJSONParser,
NetworkPlayerBaseOverviewJSONParser playerBaseOverviewJSONParser,
NetworkSessionsOverviewJSONParser sessionsOverviewJSONParser
) {
super(responseFactory);
this.cache = cache;
registerPage("overview", DataID.SERVER_OVERVIEW, networkOverviewJSONParser);
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) {
registerPage(identifier, new NetworkTabJSONHandler<>(dataID, cache, tabJSONParser));
registerPage(identifier, new NetworkTabJSONHandler<>(dataID, tabJSONParser));
}
@Override

View File

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

View File

@ -44,23 +44,20 @@ public class PlayerKillsJSONHandler implements PageHandler {
private final Identifiers identifiers;
private final JSONFactory jsonFactory;
private final JSONCache cache;
@Inject
public PlayerKillsJSONHandler(
Identifiers identifiers,
JSONFactory jsonFactory,
JSONCache cache
JSONFactory jsonFactory
) {
this.identifiers = identifiers;
this.jsonFactory = jsonFactory;
this.cache = cache;
}
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
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)))
);
}

View File

@ -45,27 +45,24 @@ public class PlayersTableJSONHandler implements PageHandler {
private final Identifiers identifiers;
private final JSONFactory jsonFactory;
private final JSONCache cache;
@Inject
public PlayersTableJSONHandler(
Identifiers identifiers,
JSONFactory jsonFactory,
JSONCache cache
JSONFactory jsonFactory
) {
this.identifiers = identifiers;
this.jsonFactory = jsonFactory;
this.cache = cache;
}
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
if (target.getParameter("server").isPresent()) {
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
return cache.getOrCache(DataID.PLAYERS, () -> new JSONResponse(jsonFactory.networkPlayersTableJSON()));
return JSONCache.getOrCache(DataID.PLAYERS, () -> new JSONResponse(jsonFactory.networkPlayersTableJSON()));
}
@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.auth.Authentication;
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.response.ResponseFactory;
import com.djrapitops.plan.exceptions.WebUserAuthException;
@ -37,15 +36,14 @@ import javax.inject.Singleton;
@Singleton
public class RootJSONHandler extends TreePageHandler {
private final JSONCache cache;
private Identifiers identifiers;
@Inject
public RootJSONHandler(
ResponseFactory responseFactory,
JSONCache cache,
Identifiers identifiers,
JSONFactory jsonFactory,
GraphsJSONHandler graphsJSONHandler,
SessionsJSONHandler sessionsJSONHandler,
PlayersTableJSONHandler playersTableJSONHandler,
@ -56,11 +54,11 @@ public class RootJSONHandler extends TreePageHandler {
PvPPvEJSONParser pvPPvEJSONParser,
PlayerBaseOverviewJSONParser playerBaseOverviewJSONParser,
PerformanceJSONParser performanceJSONParser,
PlayerJSONHandler playerJSONHandler,
NetworkJSONHandler networkJSONHandler
) {
super(responseFactory);
this.cache = cache;
this.identifiers = identifiers;
registerPage("players", playersTableJSONHandler, 1);
@ -81,7 +79,7 @@ public class RootJSONHandler extends TreePageHandler {
}
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

View File

@ -40,18 +40,15 @@ import java.util.function.Function;
public class ServerTabJSONHandler<T> implements PageHandler {
private final DataID dataID;
private final JSONCache cache;
private final Identifiers identifiers;
private final Function<UUID, T> jsonParser;
public ServerTabJSONHandler(
DataID dataID,
JSONCache cache,
Identifiers identifiers,
ServerTabJSONParser<T> jsonParser
) {
this.dataID = dataID;
this.cache = cache;
this.identifiers = identifiers;
this.jsonParser = jsonParser;
}
@ -59,7 +56,7 @@ public class ServerTabJSONHandler<T> implements PageHandler {
@Override
public Response getResponse(Request request, RequestTarget target) throws WebException {
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

View File

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