Skip to content

Commit

Permalink
Invalidate serversToPlayersCache on player updates (#103)
Browse files Browse the repository at this point in the history
This closes #102
  • Loading branch information
md5nake authored May 18, 2024
1 parent 70eebdc commit 9ebfafb
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEvent, SC extends IPlayerChangedServerNetworkEvent, NJE extends IPlayerLeftNetworkEvent, CE> {

protected final RedisBungeePlugin<P> plugin;
private final Object SERVERS_TO_PLAYERS_KEY = new Object();
private final UnifiedJedis unifiedJedis;
private final String proxyId;
private final String networkId;
private final LoadingCache<UUID, String> serverCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getServerFromRedis);
private final LoadingCache<UUID, String> lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getLastServerFromRedis);
private final LoadingCache<UUID, String> proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getProxyFromRedis);
private final LoadingCache<UUID, InetAddress> ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getIpAddressFromRedis);
private final Object SERVERS_TO_PLAYERS_KEY = new Object();
private final LoadingCache<Object, Multimap<String, UUID>> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder);
private final UnifiedJedis unifiedJedis;
private final String proxyId;
private final String networkId;
private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json();

public PlayerDataManager(RedisBungeePlugin<P> plugin) {
this.plugin = plugin;
Expand All @@ -69,16 +70,21 @@ public PlayerDataManager(RedisBungeePlugin<P> plugin) {

public abstract void onDisconnectEvent(DE event);


protected void handleNetworkPlayerServerChange(IPlayerChangedServerNetworkEvent event) {
this.serverCache.invalidate(event.getUuid());
this.lastServerCache.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 handleNetworkPlayerQuit(IPlayerLeftNetworkEvent event) {
this.proxyCache.invalidate(event.getUuid());
this.serverCache.invalidate(event.getUuid());
this.ipCache.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 handlePubSubMessageEvent(IPubSubMessageEvent event) {
Expand Down Expand Up @@ -140,8 +146,6 @@ protected void playerChangedServer(UUID uuid, String from, String to) {
handleServerChangeRedis(uuid, to);
}

private final JSONComponentSerializer COMPONENT_SERIALIZER =JSONComponentSerializer.json();

public void kickPlayer(UUID uuid, Component message) {
if (!plugin.handlePlatformKick(uuid, message)) { // handle locally before SENDING a message
JSONObject data = new JSONObject();
Expand Down Expand Up @@ -213,6 +217,7 @@ protected long getLastOnlineFromRedis(UUID uuid) {
public String getLastServerFor(UUID uuid) {
return this.lastServerCache.get(uuid);
}

public String getServerFor(UUID uuid) {
return this.serverCache.get(uuid);
}
Expand Down

0 comments on commit 9ebfafb

Please sign in to comment.