2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-12-27 12:25:29 +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 committed by Mohammed Alteneiji
parent 19448971e0
commit 8edee620b1
Signed by: ham1255
GPG Key ID: EF343502046229F4
6 changed files with 35 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;
@ -31,7 +32,7 @@ import java.net.InetAddress;
import java.util.*; import java.util.*;
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();
@ -42,6 +43,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();
@ -58,6 +60,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);
@ -79,6 +83,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);
@ -228,7 +243,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.serializer.bungeecord.BungeeComponentSerializer; import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
@ -28,7 +30,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);
@ -44,6 +46,11 @@ public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, Po
super.handleNetworkPlayerQuit(event); super.handleNetworkPlayerQuit(event);
} }
@EventHandler
public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) {
super.handleNetworkPlayerJoin(event);
}
@EventHandler @EventHandler
public void onPubSubMessageEvent(PubSubMessageEvent event) { public void onPubSubMessageEvent(PubSubMessageEvent event) {
super.handlePubSubMessageEvent(event); super.handlePubSubMessageEvent(event);

View File

@ -94,7 +94,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

@ -143,7 +143,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);
} }
@ -43,6 +44,11 @@ public class VelocityPlayerDataManager extends PlayerDataManager<Player, PostLog
handleNetworkPlayerQuit(event); handleNetworkPlayerQuit(event);
} }
@Subscribe
public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) {
handleNetworkPlayerJoin(event);
}
@Subscribe @Subscribe
public void onPubSubMessageEvent(PubSubMessageEvent event) { public void onPubSubMessageEvent(PubSubMessageEvent event) {
handlePubSubMessageEvent(event); handlePubSubMessageEvent(event);