diff --git a/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/AlwaysAvailableMuteCache.java b/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/AlwaysAvailableMuteCache.java index 8c3452c2f..cf87566d7 100644 --- a/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/AlwaysAvailableMuteCache.java +++ b/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/AlwaysAvailableMuteCache.java @@ -28,7 +28,6 @@ import space.arim.libertybans.api.select.PunishmentSelector; import space.arim.libertybans.core.config.Configs; import space.arim.libertybans.core.config.InternalFormatter; -import space.arim.libertybans.core.config.SqlConfig; import space.arim.libertybans.core.env.EnvUserResolver; import space.arim.libertybans.core.service.Time; import space.arim.omnibus.util.concurrent.CentralisedFuture; @@ -83,15 +82,28 @@ public AlwaysAvailableMuteCache(Configs configs, FactoryOfTheFuture futuresFacto } @Override - void installCache(Duration expirationTime, SqlConfig.MuteCaching.ExpirationSemantic expirationSemantic) { - ConcurrentHashMap map = new ConcurrentHashMap<>(); - Cache cache = new Cache(map, expirationTime); - cache.startPurgeTask(); - this.cache = cache; + public void startup() { + installCache((expirationTime, expirationSemantic) -> { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + Cache cache = new Cache(map, expirationTime); + cache.startPurgeTask(); + this.cache = cache; + }); + } + + @Override + public void restart() { + installCache((expirationTime, expirationSemantic) -> { + Cache oldCache = this.cache; + oldCache.stopPurgeTask(); + Cache newCache = new Cache(oldCache.map, expirationTime); + newCache.startPurgeTask(); + this.cache = newCache; + }); } @Override - void uninstallCache() { + public void shutdown() { cache.stopPurgeTask(); } diff --git a/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/BaseMuteCache.java b/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/BaseMuteCache.java index 04f3fc3d9..1d3404fcc 100644 --- a/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/BaseMuteCache.java +++ b/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/BaseMuteCache.java @@ -31,6 +31,7 @@ import java.time.Duration; import java.util.Optional; import java.util.UUID; +import java.util.function.BiConsumer; import java.util.function.Predicate; abstract class BaseMuteCache implements MuteCache { @@ -45,8 +46,7 @@ abstract class BaseMuteCache implements MuteCache { // Setup - @Override - public final void startup() { + void installCache(BiConsumer settingsConsumer) { SqlConfig sqlConfig = configs.getSqlConfig(); SqlConfig.MuteCaching muteCaching = sqlConfig.muteCaching(); @@ -58,24 +58,9 @@ public final void startup() { } else { expirationSemantic = muteCaching.expirationSemantic(); } - installCache(expirationTime, expirationSemantic); + settingsConsumer.accept(expirationTime, expirationSemantic); } - @Override - public final void restart() { - shutdown(); - startup(); - } - - @Override - public final void shutdown() { - uninstallCache(); - } - - abstract void installCache(Duration expirationTime, ExpirationSemantic expirationSemantic); - - abstract void uninstallCache(); - // Retrieval final CentralisedFuture> queryPunishment(MuteCacheKey key) { diff --git a/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/OnDemandMuteCache.java b/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/OnDemandMuteCache.java index 1f555d70d..5b9508391 100644 --- a/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/OnDemandMuteCache.java +++ b/bans-core/src/main/java/space/arim/libertybans/core/selector/cache/OnDemandMuteCache.java @@ -31,12 +31,10 @@ import space.arim.libertybans.api.select.PunishmentSelector; import space.arim.libertybans.core.config.Configs; import space.arim.libertybans.core.config.InternalFormatter; -import space.arim.libertybans.core.config.SqlConfig; import space.arim.libertybans.core.service.Time; import space.arim.omnibus.util.concurrent.CentralisedFuture; import space.arim.omnibus.util.concurrent.FactoryOfTheFuture; -import java.time.Duration; import java.util.Optional; import java.util.UUID; import java.util.function.Predicate; @@ -65,28 +63,36 @@ public OnDemandMuteCache(Configs configs, FactoryOfTheFuture futuresFactory, } @Override - void installCache(Duration expirationTime, SqlConfig.MuteCaching.ExpirationSemantic expirationSemantic) { - - Caffeine builder = Caffeine.newBuilder(); - builder = switch (expirationSemantic) { - case EXPIRE_AFTER_ACCESS -> builder.expireAfterAccess(expirationTime); - case EXPIRE_AFTER_WRITE -> builder.expireAfterWrite(expirationTime); - }; - cache = builder - .scheduler(Scheduler.disabledScheduler()) - .ticker(time.toCaffeineTicker()) - .buildAsync((key, executor) -> { - return queryPunishment(key).exceptionally((ex) -> { - // If we don't catch these exceptions, Caffeine will - LoggerFactory.getLogger(OnDemandMuteCache.class) - .warn("Exception while computing cached mute", ex); - return Optional.empty(); + public void startup() { + installCache((expirationTime, expirationSemantic) -> { + Caffeine builder = Caffeine.newBuilder(); + builder = switch (expirationSemantic) { + case EXPIRE_AFTER_ACCESS -> builder.expireAfterAccess(expirationTime); + case EXPIRE_AFTER_WRITE -> builder.expireAfterWrite(expirationTime); + }; + cache = builder + .scheduler(Scheduler.disabledScheduler()) + .ticker(time.toCaffeineTicker()) + .buildAsync((key, executor) -> { + return queryPunishment(key).exceptionally((ex) -> { + // If we don't catch these exceptions, Caffeine will + LoggerFactory.getLogger(OnDemandMuteCache.class) + .warn("Exception while computing cached mute", ex); + return Optional.empty(); + }); }); - }); + }); + } + + @Override + public void restart() { + cache.synchronous().invalidateAll(); } @Override - void uninstallCache() {} + public void shutdown() { + + } @Override public CentralisedFuture> getCachedMute(UUID uuid, NetworkAddress address) {