Skip to content

Commit 52bf043

Browse files
committed
优化缓存文件自动保存
1 parent d90e61e commit 52bf043

2 files changed

Lines changed: 23 additions & 22 deletions

File tree

src/main/java/io/github/aplini/chat2qq/Chat2QQ.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.util.*;
23+
import java.util.concurrent.CompletableFuture;
24+
import java.util.concurrent.TimeUnit;
2325
import java.util.stream.Collectors;
2426

2527
import static io.github.aplini.chat2qq.utils.Util.sendToGroup;
@@ -38,32 +40,36 @@ public void onLoad() {
3840
plugin.saveDefaultConfig();
3941
plugin.reloadConfig();
4042

41-
// 加载 temp.yml
42-
temp = YamlConfiguration.loadConfiguration(tempFile);
43-
44-
getLogger().info("[Chat2QQ] ");
45-
getLogger().info(temp.toString());
46-
getLogger().info(Objects.requireNonNull(temp.get("group_cache_all")).toString());
43+
readTempData();
44+
}
4745

48-
if(getConfig().getBoolean("aplini.player-cache.enabled", true) && temp.get("group_cache_all") != null){
46+
public static void readTempData() {
47+
temp = YamlConfiguration.loadConfiguration(tempFile);
48+
if(plugin.getConfig().getBoolean("aplini.player-cache.enabled", true) && temp.get("group_cache_all") != null){
4949
ConfigurationSection outerSection = temp.getConfigurationSection("group_cache_all");
5050
for (String groupIdStr : outerSection.getKeys(false)) {
5151
ConfigurationSection playerSection = outerSection.getConfigurationSection(groupIdStr);
5252
Map<Long, String> playerMap = playerSection.getKeys(false).stream().collect(Collectors.toMap(Long::parseLong, playerSection::getString));
5353
group_cache_all.put(Long.parseLong(groupIdStr), playerMap);
5454
}
55-
getLogger().info("[Chat2QQ] 读取群成员缓存");
56-
getLogger().info(group_cache_all.toString());
5755
}
5856
}
5957

58+
public static boolean saveTempLock = false;
6059
public static void saveTempData() {
61-
temp.set("group_cache_all", group_cache_all);
62-
try {
63-
temp.save(tempFile);
64-
} catch (IOException e) {
65-
throw new RuntimeException(e);
66-
}
60+
if(saveTempLock) return;
61+
saveTempLock = true;
62+
CompletableFuture.runAsync(() -> {
63+
try {
64+
TimeUnit.MILLISECONDS.sleep(3000);
65+
temp.set("group_cache_all", group_cache_all);
66+
temp.save(tempFile);
67+
} catch (InterruptedException | IOException ex) {
68+
throw new RuntimeException(ex);
69+
} finally {
70+
saveTempLock = false;
71+
}
72+
});
6773
}
6874

6975
@Override // 启用插件

src/main/java/io/github/aplini/chat2qq/bot/onCardChange.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import org.bukkit.event.EventHandler;
66
import org.bukkit.event.Listener;
77

8-
import java.io.IOException;
98
import java.util.HashMap;
109
import java.util.Map;
1110
import java.util.Objects;
1211
import java.util.concurrent.CompletableFuture;
1312

1413
import static io.github.aplini.chat2qq.Chat2QQ.plugin;
14+
import static io.github.aplini.chat2qq.Chat2QQ.saveTempData;
1515
import static io.github.aplini.chat2qq.utils.Util.isGroupInConfig;
1616
import static org.bukkit.Bukkit.getLogger;
1717

@@ -70,12 +70,7 @@ public static void updateMemberCardChange(long botID, long groupID, long qq, Str
7070
group_cache.put(qq, name);
7171

7272
// 保存缓存文件
73-
Chat2QQ.temp.set("group_cache_all", Chat2QQ.group_cache_all);
74-
try {
75-
Chat2QQ.temp.save(Chat2QQ.tempFile);
76-
} catch (IOException e) {
77-
throw new RuntimeException(e);
78-
}
73+
saveTempData();
7974
});
8075
}
8176
}

0 commit comments

Comments
 (0)