1
mirror of https://github.com/Shiewk/SModeration.git synced 2026-04-29 06:34:17 +02:00

Cache punishments for online players

This commit is contained in:
Shy
2026-04-12 13:32:26 +02:00
parent 6c76066d8b
commit ea54f83909
3 changed files with 61 additions and 0 deletions
@@ -22,6 +22,7 @@ import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -31,6 +32,7 @@ public final class PunishmentManager {
private static final Logger log = LoggerFactory.getLogger(PunishmentManager.class);
private final Object2ObjectArrayMap<String, PunishmentFactory<?>> typeRegistry = new Object2ObjectArrayMap<>(1);
private final ConcurrentHashMap<UUID, List<Punishment>> cache = new ConcurrentHashMap<>(1);
private final Object ioLock = new Object();
private final Path dataDir;
@@ -58,8 +60,13 @@ public final class PunishmentManager {
}
public List<Punishment> byTargetUUID(UUID target) {
List<Punishment> cached = cache.get(target);
if (cached != null) {
return cached;
}
synchronized (ioLock) {
Path file = getTargetFile(target);
if (!Files.exists(file)) {
return List.of();
}
@@ -129,6 +136,7 @@ public final class PunishmentManager {
writer.append('\n');
}
}
addToCachedList(punishment);
}
public @NotNull List<Punishment> getAll() throws IOException {
@@ -165,4 +173,21 @@ public final class PunishmentManager {
}
}
public void loadToCache(UUID uuid) {
removeFromCache(uuid);
cache.put(uuid, byTargetUUID(uuid));
}
public void removeFromCache(UUID uuid) {
cache.remove(uuid);
}
private void addToCachedList(Punishment punishment) {
cache.computeIfPresent(punishment.getTargetID(), (k, v) -> {
ObjectArrayList<Punishment> newList = new ObjectArrayList<>(v);
newList.add(punishment);
return List.copyOf(newList);
});
}
}