Skip to content

Commit

Permalink
properly cache last online and its invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
ham1255 committed Dec 23, 2024
1 parent 1944897 commit 8edee62
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +32,7 @@
import java.util.*;
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;
private final Object SERVERS_TO_PLAYERS_KEY = new Object();
Expand All @@ -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> 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, 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 JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json();

Expand All @@ -58,6 +60,8 @@ public PlayerDataManager(RedisBungeePlugin<P> plugin) {

// public abstract void onNetworkPlayerQuit(NJE event);

// public abstract void onNetworkPlayerJoin(PJN event);

// local events
//public abstract void onPubSubMessageEvent(PS event);

Expand All @@ -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);
Expand Down Expand Up @@ -228,7 +243,7 @@ public InetAddress getIpFor(UUID uuid) {
}

public long getLastOnline(UUID uuid) {
return getLastOnlineFromRedis(uuid);
return this.lastOnlineCache.get(uuid);
}

public Multimap<String, UUID> serversToPlayers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ default void stop() {

ProxyDataManager proxyDataManager();

PlayerDataManager<P, ?, ?, ?, ?, ?, ?> playerDataManager();
PlayerDataManager<P, ?, ?, ?, ?, ?, ?, ?> playerDataManager();

UUIDTranslator getUuidTranslator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +30,7 @@
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) {
super(plugin);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public ProxyDataManager proxyDataManager() {
}

@Override
public PlayerDataManager<ProxiedPlayer, ?, ?, ?, ?, ?, ?> playerDataManager() {
public PlayerDataManager<ProxiedPlayer, ?, ?, ?, ?, ?, ?, ?> playerDataManager() {
return this.playerDataManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public ProxyDataManager proxyDataManager() {
}

@Override
public PlayerDataManager<Player, ?, ?, ?, ?, ?, ?> playerDataManager() {
public PlayerDataManager<Player, ?, ?, ?, ?, ?, ?, ?> playerDataManager() {
return this.playerDataManager;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +29,7 @@

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) {
super(plugin);
}
Expand All @@ -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);
Expand Down

0 comments on commit 8edee62

Please sign in to comment.