2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-12-23 10:38:01 +00:00

readd support for redis versions below 6, change method of the summoner

This commit is contained in:
mohammed jasem alaajel 2022-07-17 05:25:50 +04:00
parent b214e3dad7
commit b0ab5e3cb4
13 changed files with 61 additions and 32 deletions

View File

@ -20,9 +20,9 @@ as its last version before internal changes.
|:-------------:|:---------:|
| 1.x.x | ✖ |
| 2.x.x | ✖ |
| 3.x.x | ✖ |
| 4.x.x | ✖ |
| 5.x.x | ✖ |
| 3.x.x | ✔ |
| 4.x.x | ✔ |
| 5.x.x | ✔ |
| 6.x.x | ✔ |
| 7.x.x | ✔ |

View File

@ -7,7 +7,7 @@ import com.google.common.collect.Multimap;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisBungeeMode;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
import org.checkerframework.checker.nullness.qual.NonNull;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@ -37,6 +37,7 @@ public class RedisBungeeAPI {
"redisbungee-" + plugin.getConfiguration().getServerId(),
"redisbungee-data"
);
new RedisBungee(this);
}
/**

View File

@ -8,7 +8,7 @@ import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
@ -55,7 +55,7 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try {
return serverCache.get(uuid, new RedisTask<String>(plugin.getApi()) {
@Override
public String singleJedisTask(Jedis jedis) {
public String jedisTask(Jedis jedis) {
return Objects.requireNonNull(jedis.hget("player:" + uuid, "server"), "user not found");
}
@ -84,7 +84,7 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try {
return proxyCache.get(uuid, new RedisTask<String>(plugin.getApi()) {
@Override
public String singleJedisTask(Jedis jedis) {
public String jedisTask(Jedis jedis) {
return Objects.requireNonNull(jedis.hget("player:" + uuid, "proxy"), "user not found");
}
@ -110,7 +110,7 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try {
return ipCache.get(uuid, new RedisTask<InetAddress>(plugin.getApi()) {
@Override
public InetAddress singleJedisTask(Jedis jedis) {
public InetAddress jedisTask(Jedis jedis) {
String result = jedis.hget("player:" + uuid, "ip");
if (result == null)
throw new NullPointerException("user not found");
@ -142,7 +142,7 @@ public abstract class AbstractDataManager<P, PL, PD, PS> {
try {
return lastOnlineCache.get(uuid, new RedisTask<Long>(plugin.getApi()) {
@Override
public Long singleJedisTask(Jedis jedis) {
public Long jedisTask(Jedis jedis) {
String result = jedis.hget("player:" + uuid, "online");
return result == null ? -1 : Long.parseLong(result);
}

View File

@ -1,6 +1,6 @@
package com.imaginarycode.minecraft.redisbungee.api;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.exceptions.JedisConnectionException;
@ -24,7 +24,7 @@ public class PubSubListener implements Runnable {
public void run() {
RedisTask<Void> subTask = new RedisTask<Void>(plugin.getApi()) {
@Override
public Void singleJedisTask(Jedis jedis) {
public Void jedisTask(Jedis jedis) {
try {
jpsh = new JedisPubSubHandler(plugin);
addedChannels.add("redisbungee-" + plugin.getConfiguration().getServerId());

View File

@ -0,0 +1,5 @@
package com.imaginarycode.minecraft.redisbungee.api;
public enum RedisBungeeMode {
SINGLE, CLUSTER
}

View File

@ -3,7 +3,6 @@ package com.imaginarycode.minecraft.redisbungee.api;
import com.google.common.collect.Multimap;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisBungeeMode;
import com.imaginarycode.minecraft.redisbungee.api.util.uuid.UUIDTranslator;
import java.net.InetAddress;
@ -99,4 +98,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform{
RedisBungeeMode getRedisBungeeMode();
Long getRedisClusterTime();
}

View File

@ -11,6 +11,10 @@ public class JedisSummoner implements Summoner<Jedis> {
public JedisSummoner(JedisPool jedisPool) {
this.jedisPool = jedisPool;
try (Jedis jedis = this.jedisPool.getResource()) {
// Test the connection to make sure configuration is right
jedis.ping();
}
}
@Override

View File

@ -1,10 +1,11 @@
package com.imaginarycode.minecraft.redisbungee.api.util;
package com.imaginarycode.minecraft.redisbungee.api.tasks;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
@ -14,7 +15,7 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
private final Summoner<?> summoner;
private final RedisBungeeAPI api;
private Jedis jedis;
private RedisBungeePlugin<?> plugin;
@Override
@ -33,7 +34,23 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
this.summoner = api.getSummoner();
}
public abstract V singleJedisTask(Jedis jedis);
// way to reuse jedis inside another RedisTask object
public RedisTask(RedisBungeeAPI api, Jedis jedis) {
this.api = api;
this.summoner = api.getSummoner();
this.jedis = jedis;
}
// way to reuse jedis inside another RedisTask object
public RedisTask(RedisBungeePlugin<?> plugin, Jedis jedis) {
this.plugin = plugin;
this.api = plugin.getApi();
this.summoner = api.getSummoner();
this.jedis = jedis;
}
public abstract V jedisTask(Jedis jedis);
public abstract V clusterJedisTask(JedisCluster jedisCluster);
@ -44,12 +61,16 @@ public abstract class RedisTask<V> implements Runnable, Callable<V> {
public V execute(){
if (api.getMode() == RedisBungeeMode.SINGLE) {
if (this.jedis != null){
return this.jedisTask(this.jedis);
}
JedisSummoner jedisSummoner = (JedisSummoner) summoner;
try (Jedis jedis = jedisSummoner.obtainResource()) {
return this.singleJedisTask(jedis);
try (Jedis newJedis = jedisSummoner.obtainResource()) {
return this.jedisTask(newJedis);
}
} else if (api.getMode() == RedisBungeeMode.CLUSTER) {
// Jedis cluster does not need new instance since its single instance anyways.
ClusterJedisSummoner clusterJedisSummoner = (ClusterJedisSummoner) summoner;
return this.clusterJedisTask(clusterJedisSummoner.obtainResource());
}

View File

@ -1,5 +0,0 @@
package com.imaginarycode.minecraft.redisbungee.api.util;
public enum RedisBungeeMode {
SINGLE, CLUSTER
}

View File

@ -1,8 +1,9 @@
package com.imaginarycode.minecraft.redisbungee.api;
package com.imaginarycode.minecraft.redisbungee.api.util;
import com.google.common.annotations.VisibleForTesting;
import com.google.gson.Gson;
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI;
import com.imaginarycode.minecraft.redisbungee.api.AbstractDataManager;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Pipeline;
@ -43,19 +44,19 @@ public class RedisUtil {
}
public static boolean isRedisVersionRight(String redisVersion) {
// Need to use >=6.2 to use Lua optimizations.
String[] args = redisVersion.split("\\.");
if (args.length < 2) {
return false;
}
int major = Integer.parseInt(args[0]);
int minor = Integer.parseInt(args[1]);
return major >= 6 && minor >= 0;
return major >= 3 && minor >= 0;
}
// Ham1255: i am keeping this if some plugin uses this *IF*
@Deprecated
public static boolean canUseLua(String redisVersion) {
// Need to use >=3 to use Lua optimizations.
return isRedisVersionRight(redisVersion);
}
}

View File

@ -1,4 +1,4 @@
package com.imaginarycode.minecraft.redisbungee.api.util;
package com.imaginarycode.minecraft.redisbungee.api.util.io;
import com.google.common.io.ByteStreams;

View File

@ -1,6 +1,7 @@
package com.imaginarycode.minecraft.redisbungee.api.util;
package com.imaginarycode.minecraft.redisbungee.api.util.lua;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.exceptions.JedisDataException;
@ -19,7 +20,7 @@ public class LuaManager {
public Script createScript(String script) {
RedisTask<Script> scriptRedisTask = new RedisTask<Script>(plugin.getApi()) {
@Override
public Script singleJedisTask(Jedis jedis) {
public Script jedisTask(Jedis jedis) {
String hash = jedis.scriptLoad(script);
return new Script(script, hash);
}
@ -53,7 +54,7 @@ public class LuaManager {
public Object eval(List<String> keys, List<String> args) {
RedisTask<Object> objectRedisTask = new RedisTask<Object>(plugin.getApi()) {
@Override
public Object singleJedisTask(Jedis jedis) {
public Object jedisTask(Jedis jedis) {
Object data;
try {
data = jedis.evalsha(hashed, keys, args);

View File

@ -6,7 +6,7 @@ import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import org.checkerframework.checker.nullness.qual.NonNull;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
@ -74,7 +74,7 @@ public final class UUIDTranslator {
}
RedisTask<UUID> redisTask = new RedisTask<UUID>(plugin.getApi()) {
@Override
public UUID singleJedisTask(Jedis jedis) {
public UUID jedisTask(Jedis jedis) {
String stored = jedis.hget("uuid-cache", player.toLowerCase());
if (stored != null) {
// Found an entry value. Deserialize it.
@ -178,7 +178,7 @@ public final class UUIDTranslator {
RedisTask<String> redisTask = new RedisTask<String>(plugin.getApi()) {
@Override
public String singleJedisTask(Jedis jedis) {
public String jedisTask(Jedis jedis) {
String stored = jedis.hget("uuid-cache", player.toString());
if (stored != null) {
// Found an entry value. Deserialize it.