From 8edee620b1bdc939a7ddec6c30ac7dcb981b092d Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Sat, 18 May 2024 16:37:31 +0400 Subject: [PATCH] properly cache last online and its invalidation --- .../redisbungee/api/PlayerDataManager.java | 19 +++++++++++++++++-- .../redisbungee/api/RedisBungeePlugin.java | 2 +- .../redisbungee/BungeePlayerDataManager.java | 9 ++++++++- .../minecraft/redisbungee/RedisBungee.java | 2 +- .../RedisBungeeVelocityPlugin.java | 2 +- .../VelocityPlayerDataManager.java | 8 +++++++- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java index 653ffcc2..15e86b79 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java +++ b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java @@ -16,6 +16,7 @@ import com.google.common.collect.Multimap; import com.google.common.net.InetAddresses; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent; +import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisPipelineTask; @@ -31,7 +32,7 @@ import java.util.*; import java.util.concurrent.TimeUnit; -public abstract class PlayerDataManager { +public abstract class PlayerDataManager { protected final RedisBungeePlugin

plugin; private final Object SERVERS_TO_PLAYERS_KEY = new Object(); @@ -42,6 +43,7 @@ public abstract class PlayerDataManager lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastServerFromRedis); private final LoadingCache proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getProxyFromRedis); private final LoadingCache ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getIpAddressFromRedis); + private final LoadingCache lastOnlineCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastOnlineFromRedis); private final LoadingCache> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder); private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json(); @@ -58,6 +60,8 @@ public PlayerDataManager(RedisBungeePlugin

plugin) { // public abstract void onNetworkPlayerQuit(NJE event); + // public abstract void onNetworkPlayerJoin(PJN event); + // local events //public abstract void onPubSubMessageEvent(PS event); @@ -79,6 +83,17 @@ protected void handleNetworkPlayerQuit(IPlayerLeftNetworkEvent event) { this.proxyCache.invalidate(event.getUuid()); this.serverCache.invalidate(event.getUuid()); this.ipCache.invalidate(event.getUuid()); + this.lastOnlineCache.invalidate(event.getUuid()); + + //TODO: We could also rely on redisbungee-serverchange pubsub messages to update the cache in-place without querying redis. That would be a lot more efficient. + this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY); + } + + protected void handleNetworkPlayerJoin(IPlayerJoinedNetworkEvent event) { + this.proxyCache.invalidate(event.getUuid()); + this.serverCache.invalidate(event.getUuid()); + this.ipCache.invalidate(event.getUuid()); + this.lastOnlineCache.invalidate(event.getUuid()); //TODO: We could also rely on redisbungee-serverchange pubsub messages to update the cache in-place without querying redis. That would be a lot more efficient. this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY); @@ -228,7 +243,7 @@ public InetAddress getIpFor(UUID uuid) { } public long getLastOnline(UUID uuid) { - return getLastOnlineFromRedis(uuid); + return this.lastOnlineCache.get(uuid); } public Multimap serversToPlayers() { diff --git a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java index 8dc6887a..e0813cc5 100644 --- a/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java +++ b/api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/RedisBungeePlugin.java @@ -65,7 +65,7 @@ default void stop() { ProxyDataManager proxyDataManager(); - PlayerDataManager playerDataManager(); + PlayerDataManager playerDataManager(); UUIDTranslator getUuidTranslator(); diff --git a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java index 8b7ff2f3..54557319 100644 --- a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java +++ b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/BungeePlayerDataManager.java @@ -12,7 +12,9 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; +import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; @@ -28,7 +30,7 @@ import java.util.concurrent.TimeUnit; -public class BungeePlayerDataManager extends PlayerDataManager implements Listener { +public class BungeePlayerDataManager extends PlayerDataManager implements Listener { public BungeePlayerDataManager(RedisBungeePlugin plugin) { super(plugin); @@ -44,6 +46,11 @@ public void onNetworkPlayerQuit(PlayerLeftNetworkEvent event) { super.handleNetworkPlayerQuit(event); } + @EventHandler + public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) { + super.handleNetworkPlayerJoin(event); + } + @EventHandler public void onPubSubMessageEvent(PubSubMessageEvent event) { super.handlePubSubMessageEvent(event); diff --git a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 4fd9094e..7944d616 100644 --- a/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/proxies/bungeecord/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -94,7 +94,7 @@ public ProxyDataManager proxyDataManager() { } @Override - public PlayerDataManager playerDataManager() { + public PlayerDataManager playerDataManager() { return this.playerDataManager; } diff --git a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java index 103dcef8..473b39cd 100644 --- a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java +++ b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java @@ -143,7 +143,7 @@ public ProxyDataManager proxyDataManager() { } @Override - public PlayerDataManager playerDataManager() { + public PlayerDataManager playerDataManager() { return this.playerDataManager; } diff --git a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java index ceb998c7..cb4fc86e 100644 --- a/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java +++ b/proxies/velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/VelocityPlayerDataManager.java @@ -14,6 +14,7 @@ import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; +import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.velocitypowered.api.event.Continuation; @@ -28,7 +29,7 @@ import java.util.concurrent.TimeUnit; -public class VelocityPlayerDataManager extends PlayerDataManager { +public class VelocityPlayerDataManager extends PlayerDataManager { public VelocityPlayerDataManager(RedisBungeePlugin plugin) { super(plugin); } @@ -43,6 +44,11 @@ public void onNetworkPlayerQuit(PlayerLeftNetworkEvent event) { handleNetworkPlayerQuit(event); } + @Subscribe + public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) { + handleNetworkPlayerJoin(event); + } + @Subscribe public void onPubSubMessageEvent(PubSubMessageEvent event) { handlePubSubMessageEvent(event);