Skip to content

Commit

Permalink
优化了缓存加载方式
Browse files Browse the repository at this point in the history
引入对短时间内多次操作合并加载
减少了加载频率 降低一部分开销
  • Loading branch information
ColdeZhang committed Apr 15, 2024
1 parent 0e3873e commit f163a19
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.lunadeer</groupId>
<artifactId>Dominion</artifactId>
<version>1.16.7-beta</version>
<version>1.17.0-beta</version>
<packaging>jar</packaging>

<name>Dominion</name>
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/cn/lunadeer/dominion/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class Cache {

Expand All @@ -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<>();
Expand All @@ -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<PlayerPrivilegeDTO> all_privileges = PlayerPrivilegeDTO.selectAll();
if (all_privileges == null) {
XLogger.err("加载玩家特权失败");
Expand All @@ -64,6 +99,7 @@ public void loadPlayerPrivileges() {
}
player_uuid_to_privilege.get(player_uuid).put(privilege.getDomID(), privilege);
}
_last_update_privilege = System.currentTimeMillis();
}

/**
Expand Down Expand Up @@ -227,4 +263,9 @@ public List<DominionDTO> getDominions() {
private ConcurrentHashMap<UUID, ConcurrentHashMap<Integer, PlayerPrivilegeDTO>> player_uuid_to_privilege; // 玩家所有的特权
private final Map<UUID, Integer> player_current_dominion_id; // 玩家当前所在领地
private ConcurrentHashMap<Integer, List<Integer>> 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;
}

0 comments on commit f163a19

Please sign in to comment.