Skip to content

Commit 9ebfafb

Browse files
authored
Invalidate serversToPlayersCache on player updates (#103)
This closes #102
1 parent 70eebdc commit 9ebfafb

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

api/src/main/java/com/imaginarycode/minecraft/redisbungee/api/PlayerDataManager.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@
3737
public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEvent, SC extends IPlayerChangedServerNetworkEvent, NJE extends IPlayerLeftNetworkEvent, CE> {
3838

3939
protected final RedisBungeePlugin<P> plugin;
40+
private final Object SERVERS_TO_PLAYERS_KEY = new Object();
41+
private final UnifiedJedis unifiedJedis;
42+
private final String proxyId;
43+
private final String networkId;
4044
private final LoadingCache<UUID, String> serverCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getServerFromRedis);
4145
private final LoadingCache<UUID, String> lastServerCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getLastServerFromRedis);
4246
private final LoadingCache<UUID, String> proxyCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getProxyFromRedis);
4347
private final LoadingCache<UUID, InetAddress> ipCache = Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build(this::getIpAddressFromRedis);
44-
private final Object SERVERS_TO_PLAYERS_KEY = new Object();
4548
private final LoadingCache<Object, Multimap<String, UUID>> serverToPlayersCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build(this::serversToPlayersBuilder);
46-
private final UnifiedJedis unifiedJedis;
47-
private final String proxyId;
48-
private final String networkId;
49+
private final JSONComponentSerializer COMPONENT_SERIALIZER = JSONComponentSerializer.json();
4950

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

7071
public abstract void onDisconnectEvent(DE event);
7172

72-
7373
protected void handleNetworkPlayerServerChange(IPlayerChangedServerNetworkEvent event) {
7474
this.serverCache.invalidate(event.getUuid());
7575
this.lastServerCache.invalidate(event.getUuid());
76+
77+
//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.
78+
this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY);
7679
}
7780

7881
protected void handleNetworkPlayerQuit(IPlayerLeftNetworkEvent event) {
7982
this.proxyCache.invalidate(event.getUuid());
8083
this.serverCache.invalidate(event.getUuid());
8184
this.ipCache.invalidate(event.getUuid());
85+
86+
//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.
87+
this.serverToPlayersCache.invalidate(SERVERS_TO_PLAYERS_KEY);
8288
}
8389

8490
protected void handlePubSubMessageEvent(IPubSubMessageEvent event) {
@@ -140,8 +146,6 @@ protected void playerChangedServer(UUID uuid, String from, String to) {
140146
handleServerChangeRedis(uuid, to);
141147
}
142148

143-
private final JSONComponentSerializer COMPONENT_SERIALIZER =JSONComponentSerializer.json();
144-
145149
public void kickPlayer(UUID uuid, Component message) {
146150
if (!plugin.handlePlatformKick(uuid, message)) { // handle locally before SENDING a message
147151
JSONObject data = new JSONObject();
@@ -213,6 +217,7 @@ protected long getLastOnlineFromRedis(UUID uuid) {
213217
public String getLastServerFor(UUID uuid) {
214218
return this.lastServerCache.get(uuid);
215219
}
220+
216221
public String getServerFor(UUID uuid) {
217222
return this.serverCache.get(uuid);
218223
}

0 commit comments

Comments
 (0)