Implemented a JSONCache

This commit is contained in:
Rsl1122 2019-08-30 23:48:36 +03:00
parent 66a19d7de1
commit 20ef9df63d
3 changed files with 133 additions and 7 deletions

View File

@ -17,9 +17,9 @@
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 com.djrapitops.plugin.benchmarking.Timings;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -32,26 +32,29 @@ import javax.inject.Singleton;
@Singleton
public class WebServerSystem implements SubSystem {
private final JSONCache jsonCache;
private final WebServer webServer;
private Timings timings;
@Inject
public WebServerSystem(WebServer webServer, Timings timings) {
public WebServerSystem(
JSONCache jsonCache,
WebServer webServer
) {
this.jsonCache = jsonCache;
this.webServer = webServer;
this.timings = timings;
}
@Override
public void enable() throws EnableException {
timings.start("WebServer Initialization");
webServer.enable();
timings.end("WebServer Initialization");
}
@Override
public void disable() {
ResponseCache.clearCache();
webServer.disable();
ResponseCache.clearCache();
jsonCache.invalidateAll();
jsonCache.cleanUp();
}
public WebServer getWebServer() {

View File

@ -0,0 +1,53 @@
/*
* 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 java.util.UUID;
/**
* Enum for different JSON data entries that can be stored in {@link JSONCache}.
*
* @author Rsl1122
*/
public enum DataID {
PLAYERS,
SESSIONS,
SERVERS,
KILLS,
PING_TABLE,
GRAPH_PERFORMANCE,
GRAPH_ONLINE,
GRAPH_UNIQUE_NEW,
GRAPH_CALENDAR,
GRAPH_WORLD_PIE,
GRAPH_WORLD_MAP,
GRAPH_ACTIVITY,
GRAPH_PING,
GRAPH_PUNCHCARD,
SERVER_OVERVIEW,
ONLINE_OVERVIEW,
SESSIONS_OVERVIEW,
PVP_PVE,
PLAYERBASE_OVERVIEW,
PERFORMANCE_OVERVIEW,
;
public String of(UUID serverUUID) {
return name() + '-' + serverUUID;
}
}

View File

@ -0,0 +1,70 @@
package com.djrapitops.plan.delivery.webserver.cache;
import com.djrapitops.plan.delivery.webserver.response.Response;
import com.djrapitops.plan.delivery.webserver.response.data.JSONResponse;
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;
/**
* Cache for any JSON data sent via {@link com.djrapitops.plan.delivery.webserver.pages.json.RootJSONHandler}.
*
* @author Rsl1122
*/
@Singleton
public class JSONCache {
private final Cache<String, String> cache;
@Inject
public JSONCache() {
cache = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.build();
}
public Response getOrCache(String identifier, Supplier<JSONResponse> jsonResponseSupplier) {
String found = cache.getIfPresent(identifier);
if (found == null) {
JSONResponse response = jsonResponseSupplier.get();
cache.put(identifier, response.getContent());
return response;
}
return new JSONResponse(found);
}
public Response getOrCache(DataID dataID, UUID serverUUID, Supplier<JSONResponse> jsonResponseSupplier) {
return getOrCache(dataID.of(serverUUID), jsonResponseSupplier);
}
public void invalidate(String identifier) {
cache.invalidate(identifier);
}
public void invalidate(DataID dataID, UUID serverUUID) {
cache.invalidate(dataID.of(serverUUID));
}
public void invalidateMatching(DataID dataID) {
String toInvalidate = dataID.name();
for (String identifier : cache.asMap().keySet()) {
if (StringUtils.startsWith(identifier, toInvalidate)) {
invalidate(identifier);
}
}
}
public void invalidateAll() {
cache.invalidateAll();
}
public void cleanUp() {
cache.cleanUp();
}
}