2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-08 22:18:02 +00:00

properly cache last online and its invalidation

This commit is contained in:
mohammed jasem alaajel 2024-05-18 16:37:31 +04:00
parent 0871bda3f3
commit d6b3956a96
Signed by: ham1255
GPG Key ID: EF343502046229F4
6 changed files with 37 additions and 7 deletions

View File

@ -16,6 +16,7 @@ import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.net.InetAddresses; import com.google.common.net.InetAddresses;
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent; 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.IPlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPubSubMessageEvent;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisPipelineTask; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisPipelineTask;
@ -34,7 +35,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEvent, SC extends IPlayerChangedServerNetworkEvent, NJE extends IPlayerLeftNetworkEvent, CE> { public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEvent, SC extends IPlayerChangedServerNetworkEvent, NJE extends IPlayerLeftNetworkEvent, CE, PJN extends IPlayerJoinedNetworkEvent> {
protected final RedisBungeePlugin<P> plugin; protected final RedisBungeePlugin<P> plugin;
private final Object SERVERS_TO_PLAYERS_KEY = new Object(); private final Object SERVERS_TO_PLAYERS_KEY = new Object();
@ -45,6 +46,7 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
private final LoadingCache<UUID, String> lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastServerFromRedis); private final LoadingCache<UUID, String> lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastServerFromRedis);
private final LoadingCache<UUID, String> proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getProxyFromRedis); private final LoadingCache<UUID, String> proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getProxyFromRedis);
private final LoadingCache<UUID, InetAddress> ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getIpAddressFromRedis); private final LoadingCache<UUID, InetAddress> ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getIpAddressFromRedis);
private final LoadingCache<UUID, Long> lastOnlineCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).build(this::getLastOnlineFromRedis);
private final LoadingCache<Object, Multimap<String, UUID>> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder); private final LoadingCache<Object, Multimap<String, UUID>> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder);
private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json(); private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json();
@ -61,6 +63,8 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
public abstract void onNetworkPlayerQuit(NJE event); public abstract void onNetworkPlayerQuit(NJE event);
public abstract void onNetworkPlayerJoin(PJN event);
// local events // local events
public abstract void onPubSubMessageEvent(PS event); public abstract void onPubSubMessageEvent(PS event);
@ -82,6 +86,17 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
this.proxyCache.invalidate(event.getUuid()); this.proxyCache.invalidate(event.getUuid());
this.serverCache.invalidate(event.getUuid()); this.serverCache.invalidate(event.getUuid());
this.ipCache.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. //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); this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY);
@ -231,7 +246,7 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
} }
public long getLastOnline(UUID uuid) { public long getLastOnline(UUID uuid) {
return getLastOnlineFromRedis(uuid); return this.lastOnlineCache.get(uuid);
} }
public Multimap<String, UUID> serversToPlayers() { public Multimap<String, UUID> serversToPlayers() {

View File

@ -65,7 +65,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
ProxyDataManager proxyDataManager(); ProxyDataManager proxyDataManager();
PlayerDataManager<P, ?, ?, ?, ?, ?, ?> playerDataManager(); PlayerDataManager<P, ?, ?, ?, ?, ?, ?, ?> playerDataManager();
UUIDTranslator getUuidTranslator(); UUIDTranslator getUuidTranslator();

View File

@ -12,7 +12,9 @@ package com.imaginarycode.minecraft.redisbungee;
import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; 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.PlayerChangedServerNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerJoinedNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent; import com.imaginarycode.minecraft.redisbungee.events.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -29,7 +31,7 @@ import net.md_5.bungee.event.EventHandler;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, PostLoginEvent, PlayerDisconnectEvent, PubSubMessageEvent, PlayerChangedServerNetworkEvent, PlayerLeftNetworkEvent, ServerConnectedEvent> implements Listener { public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, PostLoginEvent, PlayerDisconnectEvent, PubSubMessageEvent, PlayerChangedServerNetworkEvent, PlayerLeftNetworkEvent, ServerConnectedEvent, PlayerJoinedNetworkEvent> implements Listener {
public BungeePlayerDataManager(RedisBungeePlugin<ProxiedPlayer> plugin) { public BungeePlayerDataManager(RedisBungeePlugin<ProxiedPlayer> plugin) {
super(plugin); super(plugin);
@ -47,6 +49,12 @@ public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, Po
super.handleNetworkPlayerQuit(event); super.handleNetworkPlayerQuit(event);
} }
@Override
@EventHandler
public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) {
super.handleNetworkPlayerJoin(event);
}
@Override @Override
@EventHandler @EventHandler
public void onPubSubMessageEvent(PubSubMessageEvent event) { public void onPubSubMessageEvent(PubSubMessageEvent event) {

View File

@ -99,7 +99,7 @@ public class RedisBungee extends Plugin implements RedisBungeePlugin<ProxiedPlay
} }
@Override @Override
public PlayerDataManager<ProxiedPlayer, ?, ?, ?, ?, ?, ?> playerDataManager() { public PlayerDataManager<ProxiedPlayer, ?, ?, ?, ?, ?, ?, ?> playerDataManager() {
return this.playerDataManager; return this.playerDataManager;
} }

View File

@ -151,7 +151,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player>, Con
} }
@Override @Override
public PlayerDataManager<Player, ?, ?, ?, ?, ?, ?> playerDataManager() { public PlayerDataManager<Player, ?, ?, ?, ?, ?, ?, ?> playerDataManager() {
return this.playerDataManager; return this.playerDataManager;
} }

View File

@ -14,6 +14,7 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration;
import com.imaginarycode.minecraft.redisbungee.events.PlayerChangedServerNetworkEvent; 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.PlayerLeftNetworkEvent;
import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent;
import com.velocitypowered.api.event.Continuation; import com.velocitypowered.api.event.Continuation;
@ -28,7 +29,7 @@ import net.kyori.adventure.text.Component;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class VelocityPlayerDataManager extends PlayerDataManager<Player, PostLoginEvent, DisconnectEvent, PubSubMessageEvent, PlayerChangedServerNetworkEvent, PlayerLeftNetworkEvent, ServerConnectedEvent> { public class VelocityPlayerDataManager extends PlayerDataManager<Player, PostLoginEvent, DisconnectEvent, PubSubMessageEvent, PlayerChangedServerNetworkEvent, PlayerLeftNetworkEvent, ServerConnectedEvent, PlayerJoinedNetworkEvent> {
public VelocityPlayerDataManager(RedisBungeePlugin<Player> plugin) { public VelocityPlayerDataManager(RedisBungeePlugin<Player> plugin) {
super(plugin); super(plugin);
} }
@ -45,6 +46,12 @@ public class VelocityPlayerDataManager extends PlayerDataManager<Player, PostLog
handleNetworkPlayerQuit(event); handleNetworkPlayerQuit(event);
} }
@Override
@Subscribe
public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) {
handleNetworkPlayerJoin(event);
}
@Override @Override
@Subscribe @Subscribe
public void onPubSubMessageEvent(PubSubMessageEvent event) { public void onPubSubMessageEvent(PubSubMessageEvent event) {