diff --git a/pom.xml b/pom.xml
index 509868de..38139539 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
cn.lunadeer
Dominion
- 1.16.7-beta
+ 1.17.0-beta
jar
Dominion
diff --git a/src/main/java/cn/lunadeer/dominion/Cache.java b/src/main/java/cn/lunadeer/dominion/Cache.java
index f2913832..b4c21637 100644
--- a/src/main/java/cn/lunadeer/dominion/Cache.java
+++ b/src/main/java/cn/lunadeer/dominion/Cache.java
@@ -11,6 +11,7 @@
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
public class Cache {
@@ -24,6 +25,24 @@ public Cache() {
* 从数据库加载所有领地
*/
public void loadDominions() {
+ if (_last_update_dominion + UPDATE_INTERVAL < System.currentTimeMillis()) {
+ XLogger.debug("run loadDominionsExecution directly");
+ loadDominionsExecution();
+ } else {
+ if (_update_dominion_is_scheduled) return;
+ XLogger.debug("schedule loadDominionsExecution");
+ _update_dominion_is_scheduled = true;
+ Dominion.scheduler.async.runDelayed(Dominion.instance, (instance) -> {
+ XLogger.debug("run loadDominionsExecution scheduled");
+ loadDominionsExecution();
+ _update_dominion_is_scheduled = false;
+ },
+ UPDATE_INTERVAL - (System.currentTimeMillis() - _last_update_dominion),
+ TimeUnit.MILLISECONDS);
+ }
+ }
+
+ private void loadDominionsExecution() {
id_dominions = new ConcurrentHashMap<>();
world_dominions = new ConcurrentHashMap<>();
dominion_children = new ConcurrentHashMap<>();
@@ -45,12 +64,28 @@ public void loadDominions() {
}
}
BlueMapConnect.render();
+ _last_update_dominion = System.currentTimeMillis();
}
/**
* 从数据库加载所有玩家特权
*/
public void loadPlayerPrivileges() {
+ if (_last_update_privilege + UPDATE_INTERVAL < System.currentTimeMillis()) {
+ loadPlayerPrivilegesExecution();
+ } else {
+ if (_update_privilege_is_scheduled) return;
+ _update_privilege_is_scheduled = true;
+ Dominion.scheduler.async.runDelayed(Dominion.instance, (instance) -> {
+ loadPlayerPrivilegesExecution();
+ _update_privilege_is_scheduled = false;
+ },
+ UPDATE_INTERVAL - (System.currentTimeMillis() - _last_update_privilege),
+ TimeUnit.MILLISECONDS);
+ }
+ }
+
+ private void loadPlayerPrivilegesExecution() {
List all_privileges = PlayerPrivilegeDTO.selectAll();
if (all_privileges == null) {
XLogger.err("加载玩家特权失败");
@@ -64,6 +99,7 @@ public void loadPlayerPrivileges() {
}
player_uuid_to_privilege.get(player_uuid).put(privilege.getDomID(), privilege);
}
+ _last_update_privilege = System.currentTimeMillis();
}
/**
@@ -227,4 +263,9 @@ public List getDominions() {
private ConcurrentHashMap> player_uuid_to_privilege; // 玩家所有的特权
private final Map player_current_dominion_id; // 玩家当前所在领地
private ConcurrentHashMap> dominion_children;
+ private long _last_update_dominion = 0;
+ private boolean _update_dominion_is_scheduled = false;
+ private long _last_update_privilege = 0;
+ private boolean _update_privilege_is_scheduled = false;
+ private static final long UPDATE_INTERVAL = 1000 * 4;
}