mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-22 20:28:00 +00:00
Drastic bandwidth usage reduction.
This commit is contained in:
parent
3b2d157943
commit
1c3c1f3ef2
@ -60,6 +60,10 @@ public final class RedisBungee extends Plugin {
|
||||
private boolean usingLua;
|
||||
private LuaManager.Script serverToPlayersScript;
|
||||
private LuaManager.Script getPlayerCountScript;
|
||||
private LuaManager.Script getServerPlayersScript;
|
||||
|
||||
private static final Object SERVER_TO_PLAYERS_KEY = new Object();
|
||||
private final InternalCache<Object, Multimap<String, UUID>> serverToPlayersCache = new InternalCache<>(2000);
|
||||
|
||||
/**
|
||||
* Fetch the {@link RedisBungeeAPI} object created on plugin start.
|
||||
@ -121,21 +125,30 @@ public final class RedisBungee extends Plugin {
|
||||
}
|
||||
|
||||
final Multimap<String, UUID> serversToPlayers() {
|
||||
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
|
||||
try {
|
||||
return serverToPlayersCache.get(SERVER_TO_PLAYERS_KEY, new Callable<Multimap<String, UUID>>() {
|
||||
@Override
|
||||
public Multimap<String, UUID> call() throws Exception {
|
||||
Collection<String> data = (Collection<String>) serverToPlayersScript.eval(ImmutableList.<String>of(), getServerIds());
|
||||
|
||||
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
|
||||
String key = null;
|
||||
for (String s : data) {
|
||||
if (key == null) {
|
||||
key = s;
|
||||
continue;
|
||||
}
|
||||
ImmutableMultimap.Builder<String, UUID> builder = ImmutableMultimap.builder();
|
||||
String key = null;
|
||||
for (String s : data) {
|
||||
if (key == null) {
|
||||
key = s;
|
||||
continue;
|
||||
}
|
||||
|
||||
builder.put(key, UUID.fromString(s));
|
||||
key = null;
|
||||
builder.put(key, UUID.fromString(s));
|
||||
key = null;
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
});
|
||||
} catch (ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
final int getCount() {
|
||||
@ -181,15 +194,7 @@ public final class RedisBungee extends Plugin {
|
||||
|
||||
final Set<UUID> getPlayersOnServer(@NonNull String server) {
|
||||
checkArgument(getProxy().getServers().containsKey(server), "server does not exist");
|
||||
Multimap<String, UUID> serversToPlayers = serversToPlayers();
|
||||
|
||||
for (String s : serversToPlayers.keySet()) {
|
||||
if (s.equalsIgnoreCase(server)) {
|
||||
return ImmutableSet.copyOf(serversToPlayers.get(s));
|
||||
}
|
||||
}
|
||||
|
||||
return Collections.emptySet();
|
||||
return ImmutableSet.copyOf((Collection<UUID>) getServerPlayersScript.eval(ImmutableList.<String>of(), ImmutableList.<String>of(server)));
|
||||
}
|
||||
|
||||
final void sendProxyCommand(@NonNull String proxyId, @NonNull String command) {
|
||||
@ -234,6 +239,7 @@ public final class RedisBungee extends Plugin {
|
||||
LuaManager manager = new LuaManager(this);
|
||||
serverToPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/server_to_players.lua")));
|
||||
getPlayerCountScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_player_count.lua")));
|
||||
getServerPlayersScript = manager.createScript(IOUtil.readInputStreamAsString(getResourceAsStream("lua/get_server_players.lua")));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
33
src/main/resources/lua/get_server_players.lua
Normal file
33
src/main/resources/lua/get_server_players.lua
Normal file
@ -0,0 +1,33 @@
|
||||
local c = redis.call
|
||||
local u = string.upper
|
||||
|
||||
local curTime = c("TIME")
|
||||
local time = tonumber(curTime[1])
|
||||
|
||||
local heartbeats = c("HGETALL", "heartbeats")
|
||||
local all = {}
|
||||
local key
|
||||
|
||||
local preUppercased = u(ARGV[1])
|
||||
|
||||
for _, v in ipairs(heartbeats) do
|
||||
if not key then
|
||||
key = v
|
||||
else
|
||||
local n = tonumber(v)
|
||||
if n then
|
||||
if n + 30 >= time then
|
||||
local players = c("SMEMBERS", "proxy:" .. key .. ":usersOnline")
|
||||
for _, player in ipairs(players) do
|
||||
local server = c("HGET", "player:" .. player, "server")
|
||||
if server and u(server) == preUppercased then
|
||||
all[#all + 1] = player
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
key = nil
|
||||
end
|
||||
end
|
||||
|
||||
return all
|
Loading…
Reference in New Issue
Block a user