|
|
@ -19,7 +19,7 @@ import java.util.concurrent.CompletableFuture;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Manages the connection to the Redis server, handling the caching of user data
|
|
|
|
* Manages the connection to the Redis server, handling the caching of user data
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class RedisManager {
|
|
|
|
public class RedisManager extends JedisPubSub {
|
|
|
|
|
|
|
|
|
|
|
|
protected static final String KEY_NAMESPACE = "husksync:";
|
|
|
|
protected static final String KEY_NAMESPACE = "husksync:";
|
|
|
|
protected static String clusterId = "";
|
|
|
|
protected static String clusterId = "";
|
|
|
@ -53,8 +53,7 @@ public class RedisManager {
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return a future returning void when complete
|
|
|
|
* @return a future returning void when complete
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public CompletableFuture<Boolean> initialize() {
|
|
|
|
public boolean initialize() {
|
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
|
|
|
|
|
if (redisPassword.isBlank()) {
|
|
|
|
if (redisPassword.isBlank()) {
|
|
|
|
jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisUseSsl);
|
|
|
|
jedisPool = new JedisPool(jedisPoolConfig, redisHost, redisPort, 0, redisUseSsl);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -67,7 +66,6 @@ public class RedisManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CompletableFuture.runAsync(this::subscribe);
|
|
|
|
CompletableFuture.runAsync(this::subscribe);
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void subscribe() {
|
|
|
|
private void subscribe() {
|
|
|
@ -75,11 +73,19 @@ public class RedisManager {
|
|
|
|
new Jedis(redisHost, redisPort, DefaultJedisClientConfig.builder()
|
|
|
|
new Jedis(redisHost, redisPort, DefaultJedisClientConfig.builder()
|
|
|
|
.password(redisPassword).timeoutMillis(0).ssl(redisUseSsl).build())) {
|
|
|
|
.password(redisPassword).timeoutMillis(0).ssl(redisUseSsl).build())) {
|
|
|
|
subscriber.connect();
|
|
|
|
subscriber.connect();
|
|
|
|
subscriber.subscribe(new JedisPubSub() {
|
|
|
|
subscriber.subscribe(this, Arrays.stream(RedisMessageType.values())
|
|
|
|
|
|
|
|
.map(RedisMessageType::getMessageChannel)
|
|
|
|
|
|
|
|
.toArray(String[]::new));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
public void onMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
RedisMessageType.getTypeFromChannel(channel).ifPresent(messageType -> {
|
|
|
|
final RedisMessageType messageType = RedisMessageType.getTypeFromChannel(channel).orElse(null);
|
|
|
|
if (messageType == RedisMessageType.UPDATE_USER_DATA) {
|
|
|
|
if (messageType != RedisMessageType.UPDATE_USER_DATA) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final RedisMessage redisMessage = RedisMessage.fromJson(message);
|
|
|
|
final RedisMessage redisMessage = RedisMessage.fromJson(message);
|
|
|
|
plugin.getOnlineUser(redisMessage.targetUserUuid).ifPresent(user -> {
|
|
|
|
plugin.getOnlineUser(redisMessage.targetUserUuid).ifPresent(user -> {
|
|
|
|
final UserData userData = plugin.getDataAdapter().fromBytes(redisMessage.data);
|
|
|
|
final UserData userData = plugin.getDataAdapter().fromBytes(redisMessage.data);
|
|
|
@ -103,11 +109,6 @@ public class RedisManager {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, Arrays.stream(RedisMessageType.values()).map(RedisMessageType::getMessageChannel).toArray(String[]::new));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected void sendMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
protected void sendMessage(@NotNull String channel, @NotNull String message) {
|
|
|
|
try (Jedis jedis = jedisPool.getResource()) {
|
|
|
|
try (Jedis jedis = jedisPool.getResource()) {
|
|
|
|