mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-12-26 20:15:29 +00:00
properly cache last online and its invalidation
This commit is contained in:
parent
19448971e0
commit
8edee620b1
@ -16,6 +16,7 @@ import com.google.common.collect.ImmutableMultimap;
|
||||
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;
|
||||
@ -31,7 +32,7 @@ import java.net.InetAddress;
|
||||
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();
|
||||
@ -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();
|
||||
|
||||
@ -58,6 +60,8 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
|
||||
|
||||
// public abstract void onNetworkPlayerQuit(NJE event);
|
||||
|
||||
// public abstract void onNetworkPlayerJoin(PJN event);
|
||||
|
||||
// local events
|
||||
//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.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);
|
||||
@ -228,7 +243,7 @@ public abstract class PlayerDataManager<P, LE, DE, PS extends IPubSubMessageEven
|
||||
}
|
||||
|
||||
public long getLastOnline(UUID uuid) {
|
||||
return getLastOnlineFromRedis(uuid);
|
||||
return this.lastOnlineCache.get(uuid);
|
||||
}
|
||||
|
||||
public Multimap<String, UUID> serversToPlayers() {
|
||||
|
@ -65,7 +65,7 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
|
||||
|
||||
ProxyDataManager proxyDataManager();
|
||||
|
||||
PlayerDataManager<P, ?, ?, ?, ?, ?, ?> playerDataManager();
|
||||
PlayerDataManager<P, ?, ?, ?, ?, ?, ?, ?> playerDataManager();
|
||||
|
||||
UUIDTranslator getUuidTranslator();
|
||||
|
||||
|
@ -12,7 +12,9 @@ package com.imaginarycode.minecraft.redisbungee;
|
||||
|
||||
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;
|
||||
@ -28,7 +30,7 @@ import net.md_5.bungee.event.EventHandler;
|
||||
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);
|
||||
@ -44,6 +46,11 @@ public class BungeePlayerDataManager extends PlayerDataManager<ProxiedPlayer, Po
|
||||
super.handleNetworkPlayerQuit(event);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) {
|
||||
super.handleNetworkPlayerJoin(event);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPubSubMessageEvent(PubSubMessageEvent event) {
|
||||
super.handlePubSubMessageEvent(event);
|
||||
|
@ -94,7 +94,7 @@ public class RedisBungee extends Plugin implements RedisBungeePlugin<ProxiedPlay
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerDataManager<ProxiedPlayer, ?, ?, ?, ?, ?, ?> playerDataManager() {
|
||||
public PlayerDataManager<ProxiedPlayer, ?, ?, ?, ?, ?, ?, ?> playerDataManager() {
|
||||
return this.playerDataManager;
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player>, Con
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerDataManager<Player, ?, ?, ?, ?, ?, ?> playerDataManager() {
|
||||
public PlayerDataManager<Player, ?, ?, ?, ?, ?, ?, ?> playerDataManager() {
|
||||
return this.playerDataManager;
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager;
|
||||
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;
|
||||
@ -28,7 +29,7 @@ import net.kyori.adventure.text.Component;
|
||||
|
||||
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);
|
||||
}
|
||||
@ -43,6 +44,11 @@ public class VelocityPlayerDataManager extends PlayerDataManager<Player, PostLog
|
||||
handleNetworkPlayerQuit(event);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onNetworkPlayerJoin(PlayerJoinedNetworkEvent event) {
|
||||
handleNetworkPlayerJoin(event);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onPubSubMessageEvent(PubSubMessageEvent event) {
|
||||
handlePubSubMessageEvent(event);
|
||||
|
Loading…
Reference in New Issue
Block a user