Implemented a memory storage for json

This will replace JSONCache eventually
This commit is contained in:
Risto Lahtela 2021-02-06 08:45:26 +02:00 committed by Risto Lahtela
parent 49f6b7708f
commit a621b343e8
5 changed files with 178 additions and 17 deletions

View File

@ -26,7 +26,6 @@ import com.djrapitops.plan.utilities.UnitSemaphoreAccessLock;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
@ -68,9 +67,11 @@ public class AsyncJSONResolverService {
String identifier = dataID.of(serverUUID);
// Attempt to find a newer version of the json file from cache
Optional<JSONStorage.StoredJSON> storedJSON = jsonStorage.fetchJsonMadeAfter(identifier, newerThanTimestamp);
if (storedJSON.isPresent()) {
return storedJSON.get();
JSONStorage.StoredJSON storedJSON = jsonStorage.fetchExactJson(identifier, newerThanTimestamp)
.orElseGet(() -> jsonStorage.fetchJsonMadeAfter(identifier, newerThanTimestamp)
.orElse(null));
if (storedJSON != null) {
return storedJSON;
}
// No new enough version, let's refresh and send old version of the file
@ -97,9 +98,9 @@ public class AsyncJSONResolverService {
}
// Get an old version from cache
storedJSON = jsonStorage.fetchJsonMadeBefore(identifier, newerThanTimestamp);
if (storedJSON.isPresent()) {
return storedJSON.get();
storedJSON = jsonStorage.fetchJsonMadeBefore(identifier, newerThanTimestamp).orElse(null);
if (storedJSON != null) {
return storedJSON;
} else {
// If there is no version available, block thread until the new finishes being generated.
try {
@ -119,9 +120,11 @@ public class AsyncJSONResolverService {
String identifier = dataID.name();
// Attempt to find a newer version of the json file from cache
Optional<JSONStorage.StoredJSON> storedJSON = jsonStorage.fetchJsonMadeAfter(identifier, newerThanTimestamp);
if (storedJSON.isPresent()) {
return storedJSON.get();
JSONStorage.StoredJSON storedJSON = jsonStorage.fetchExactJson(identifier, newerThanTimestamp)
.orElseGet(() -> jsonStorage.fetchJsonMadeAfter(identifier, newerThanTimestamp)
.orElse(null));
if (storedJSON != null) {
return storedJSON;
}
// No new enough version, let's refresh and send old version of the file
@ -148,9 +151,9 @@ public class AsyncJSONResolverService {
}
// Get an old version from cache
storedJSON = jsonStorage.fetchJsonMadeBefore(identifier, newerThanTimestamp);
if (storedJSON.isPresent()) {
return storedJSON.get();
storedJSON = jsonStorage.fetchJsonMadeBefore(identifier, newerThanTimestamp).orElse(null);
if (storedJSON != null) {
return storedJSON;
} else {
// If there is no version available, block thread until the new finishes being generated.
try {

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.gathering.cache;
import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.commands.TabCompleteCache;
import com.djrapitops.plan.gathering.geolocation.GeolocationCache;
import com.djrapitops.plan.storage.json.JSONStorage;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -35,18 +36,21 @@ public class CacheSystem implements SubSystem {
private final SessionCache sessionCache;
private final NicknameCache nicknameCache;
private final GeolocationCache geolocationCache;
private final JSONStorage jsonStorage;
@Inject
public CacheSystem(
TabCompleteCache tabCompleteCache,
SessionCache sessionCache,
NicknameCache nicknameCache,
GeolocationCache geolocationCache
GeolocationCache geolocationCache,
JSONStorage jsonStorage
) {
this.tabCompleteCache = tabCompleteCache;
this.sessionCache = sessionCache;
this.nicknameCache = nicknameCache;
this.geolocationCache = geolocationCache;
this.jsonStorage = jsonStorage;
}
@Override
@ -54,6 +58,7 @@ public class CacheSystem implements SubSystem {
nicknameCache.enable();
geolocationCache.enable();
tabCompleteCache.enable();
jsonStorage.enable();
}
@Override

View File

@ -26,6 +26,7 @@ import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.LocaleSystem;
import com.djrapitops.plan.storage.file.JarResource;
import com.djrapitops.plan.storage.json.JSONFileStorage;
import com.djrapitops.plan.storage.json.JSONMemoryStorageShim;
import com.djrapitops.plan.storage.json.JSONStorage;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.djrapitops.plan.utilities.logging.PluginErrorLogger;
@ -100,8 +101,11 @@ public class SystemObjectProvidingModule {
@Provides
@Singleton
JSONStorage provideJSONStorage(JSONFileStorage jsonFileStorage) {
return jsonFileStorage;
JSONStorage provideJSONStorage(
PlanConfig config,
JSONFileStorage jsonFileStorage
) {
return new JSONMemoryStorageShim(config, jsonFileStorage);
}
}

View File

@ -0,0 +1,140 @@
/*
* 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.storage.json;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class JSONMemoryStorageShim implements JSONStorage {
private final PlanConfig config;
private final JSONStorage underlyingStorage;
private Cache<TimestampedIdentifier, StoredJSON> cache;
public JSONMemoryStorageShim(
PlanConfig config,
JSONStorage underlyingStorage
) {
this.config = config;
this.underlyingStorage = underlyingStorage;
}
@Override
public void enable() {
cache = Caffeine.newBuilder()
.expireAfterWrite(config.get(WebserverSettings.INVALIDATE_MEMORY_CACHE), TimeUnit.MILLISECONDS)
.build();
}
@Override
public StoredJSON storeJson(String identifier, String json, long timestamp) {
StoredJSON storedJSON = underlyingStorage.storeJson(identifier, json, timestamp);
cache.put(new TimestampedIdentifier(identifier, timestamp), storedJSON);
return storedJSON;
}
@Override
public Optional<StoredJSON> fetchJSON(String identifier) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : cache.asMap().entrySet()) {
if (entry.getKey().identifier.equalsIgnoreCase(identifier)) {
return Optional.of(entry.getValue());
}
}
Optional<StoredJSON> found = underlyingStorage.fetchJSON(identifier);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
return found;
}
@Override
public Optional<StoredJSON> fetchExactJson(String identifier, long timestamp) {
StoredJSON cached = cache.getIfPresent(new TimestampedIdentifier(identifier, timestamp));
if (cached != null) return Optional.of(cached);
Optional<StoredJSON> found = underlyingStorage.fetchExactJson(identifier, timestamp);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, timestamp), storedJSON));
return found;
}
@Override
public Optional<StoredJSON> fetchJsonMadeBefore(String identifier, long timestamp) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : cache.asMap().entrySet()) {
TimestampedIdentifier key = entry.getKey();
if (key.timestamp < timestamp && key.identifier.equalsIgnoreCase(identifier)) {
return Optional.of(entry.getValue());
}
}
Optional<StoredJSON> found = underlyingStorage.fetchJsonMadeBefore(identifier, timestamp);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
return found;
}
@Override
public Optional<StoredJSON> fetchJsonMadeAfter(String identifier, long timestamp) {
for (Map.Entry<TimestampedIdentifier, StoredJSON> entry : cache.asMap().entrySet()) {
TimestampedIdentifier key = entry.getKey();
if (key.timestamp > timestamp && key.identifier.equalsIgnoreCase(identifier)) {
return Optional.of(entry.getValue());
}
}
Optional<StoredJSON> found = underlyingStorage.fetchJsonMadeAfter(identifier, timestamp);
found.ifPresent(storedJSON -> cache.put(new TimestampedIdentifier(identifier, storedJSON.timestamp), storedJSON));
return found;
}
@Override
public void invalidateOlder(String identifier, long timestamp) {
Set<TimestampedIdentifier> toInvalidate = new HashSet<>();
for (TimestampedIdentifier key : cache.asMap().keySet()) {
if (key.timestamp < timestamp && key.identifier.equalsIgnoreCase(identifier)) {
toInvalidate.add(key);
}
}
toInvalidate.forEach(cache::invalidate);
underlyingStorage.invalidateOlder(identifier, timestamp);
}
static class TimestampedIdentifier {
private final String identifier;
private final long timestamp;
public TimestampedIdentifier(String identifier, long timestamp) {
this.identifier = identifier;
this.timestamp = timestamp;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimestampedIdentifier that = (TimestampedIdentifier) o;
return timestamp == that.timestamp && identifier.equals(that.identifier);
}
@Override
public int hashCode() {
return Objects.hash(identifier, timestamp);
}
}
}

View File

@ -16,6 +16,7 @@
*/
package com.djrapitops.plan.storage.json;
import com.djrapitops.plan.SubSystem;
import com.google.gson.Gson;
import java.util.Objects;
@ -26,7 +27,15 @@ import java.util.Optional;
*
* @author Rsl1122
*/
public interface JSONStorage {
public interface JSONStorage extends SubSystem {
@Override
default void enable() {
}
@Override
default void disable() {
}
default StoredJSON storeJson(String identifier, String json) {
return storeJson(identifier, json, System.currentTimeMillis());