This commit is contained in:
YuTian 2024-07-22 14:45:38 +08:00
parent 22e434b4ef
commit 142ce8ee68
6 changed files with 133 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package com.io.yutian.aulib;
import com.io.yutian.aulib.lang.Lang; import com.io.yutian.aulib.lang.Lang;
import com.io.yutian.aulib.listener.GuiHandlerListener; import com.io.yutian.aulib.listener.GuiHandlerListener;
import com.io.yutian.aulib.redis.RedisCacheSyncTimer;
import com.io.yutian.aulib.util.LangUtil; import com.io.yutian.aulib.util.LangUtil;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -9,10 +10,14 @@ public class AuLib extends JavaPlugin {
private static AuLib instance; private static AuLib instance;
private RedisCacheSyncTimer redisCacheSyncTimer;
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
redisCacheSyncTimer = new RedisCacheSyncTimer();
registerListeners(); registerListeners();
Lang.registerLangFile(this); Lang.registerLangFile(this);
@ -22,11 +27,14 @@ public class AuLib extends JavaPlugin {
} }
private void registerListeners() { private void registerListeners() {
new GuiHandlerListener(this); new GuiHandlerListener(this);
} }
public RedisCacheSyncTimer getRedisCacheSyncTimer() {
return redisCacheSyncTimer;
}
public static AuLib inst() { public static AuLib inst() {
return instance; return instance;
} }

View File

@ -0,0 +1,9 @@
package com.io.yutian.aulib.redis;
import redis.clients.jedis.Jedis;
public interface IJedisGetter {
Jedis getRedis();
}

View File

@ -1,9 +0,0 @@
package com.io.yutian.aulib.redis;
public class RedisCacheHelper {
public static void updateCache(String key, String value) {
}
}

View File

@ -0,0 +1,106 @@
package com.io.yutian.aulib.redis;
import com.io.yutian.aulib.AuLib;
import it.unimi.dsi.fastutil.Pair;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Map;
public class RedisCacheSyncTimer {
private static final String LOCK_KEY = "sync_lock";
private static final int LOCK_EXPIRE_SECONDS = 600;
private final Map<Plugin, PluginInfo> pluginInfos = new HashMap<>();
private BukkitTask task;
public RedisCacheSyncTimer() {
task = new BukkitRunnable() {
@Override
public void run() {
if (pluginInfos.isEmpty()) {
return;
}
for (Map.Entry<Plugin, PluginInfo> entry : pluginInfos.entrySet()) {
Plugin plugin = entry.getKey();
if (!plugin.isEnabled()) {
continue;
}
PluginInfo pluginInfo = entry.getValue();
String lockKey = LOCK_KEY + "_" + plugin.getName();
IJedisGetter jedisGetter = pluginInfo.getJedisGetter();
try (Jedis jedis = jedisGetter.getRedis()) {
long lockResult = jedis.setnx(lockKey, "locked");
if (lockResult != 1) {
break;
}
jedis.expire(lockKey, LOCK_EXPIRE_SECONDS);
for (Map.Entry<Pair<String, String>, RedisCacheSynchronizer> entry1 : entry.getValue().getSynchronizers().entrySet()) {
Pair<String, String> key = entry1.getKey();
RedisCacheSynchronizer synchronizer = entry1.getValue();
String k1 = key.first();
String k2 = key.second();
String data = null;
if (k2 == null) {
data = jedis.get(k1);
} else {
data = jedis.hget(k1, k2);
}
if (data != null) {
String finalData = data;
synchronizer.sync(finalData);
if (k2 == null) {
jedis.del(k1);
} else {
jedis.hdel(k1, k2);
}
}
}
}
}
}
}.runTaskTimerAsynchronously(AuLib.inst(), 0L, 10L * 20 * 60L);
}
public void registerSynchronizer(Plugin plugin, IJedisGetter jedisGetter, Pair<String, String> key, RedisCacheSynchronizer synchronizer) {
PluginInfo pluginInfo = pluginInfos.computeIfAbsent(plugin, k -> new PluginInfo(jedisGetter));
pluginInfo.addSynchronizer(key, synchronizer);
pluginInfos.put(plugin, pluginInfo);
}
class PluginInfo {
private IJedisGetter jedisGetter;
private Map<Pair<String, String>, RedisCacheSynchronizer> synchronizers;
public PluginInfo(IJedisGetter jedisGetter) {
this.jedisGetter = jedisGetter;
this.synchronizers = new HashMap<>();
}
public void addSynchronizer(Pair<String, String> key, RedisCacheSynchronizer synchronizer) {
if (key == null || synchronizer == null) {
return;
}
if (synchronizers.containsKey(key)) {
throw new IllegalArgumentException("Key already registered: " + key);
}
synchronizers.put(key, synchronizer);
}
public IJedisGetter getJedisGetter() {
return jedisGetter;
}
public Map<Pair<String, String>, RedisCacheSynchronizer> getSynchronizers() {
return synchronizers;
}
}
}

View File

@ -0,0 +1,8 @@
package com.io.yutian.aulib.redis;
@FunctionalInterface
public interface RedisCacheSynchronizer {
void sync(String data);
}

View File

@ -1,5 +1,5 @@
name: AuLib name: AuLib
main: com.io.yutian.aulib.AuLib main: com.io.yutian.aulib.AuLib
version: 2.1 version: 2.2
api-version: 1.18 api-version: 1.18
author: SuperYuTian author: SuperYuTian