1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(user): 令saveAll于unloadAll不再异步,避免关服后线程被强制关闭。

This commit is contained in:
Carm Jos 2023-09-02 21:31:10 +08:00
parent 046e901ec1
commit 19f25db0b7
17 changed files with 37 additions and 32 deletions

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<properties> <properties>

View File

@ -63,7 +63,11 @@ public abstract class UserDataManager<K, U extends UserData<K>> {
return key.toString(); return key.toString();
} }
public abstract @NotNull U empty(@NotNull K key); public abstract @NotNull U emptyUser(@NotNull K key);
public @NotNull U errorUser(@NotNull K key) {
return emptyUser(key);
}
protected abstract @Nullable U loadData(@NotNull K key) throws Exception; protected abstract @Nullable U loadData(@NotNull K key) throws Exception;
@ -108,7 +112,7 @@ public abstract class UserDataManager<K, U extends UserData<K>> {
U data = loadData(key); U data = loadData(key);
if (data == null) { if (data == null) {
getPlugin().debug("数据库内不存在用户 " + identifier + " 的数据,视作新档案。"); getPlugin().debug("数据库内不存在用户 " + identifier + " 的数据,视作新档案。");
return empty(key); return emptyUser(key);
} else { } else {
getPlugin().debug("加载用户 " + identifier + " 的数据完成,耗时 " + (System.currentTimeMillis() - s1) + " ms."); getPlugin().debug("加载用户 " + identifier + " 的数据完成,耗时 " + (System.currentTimeMillis() - s1) + " ms.");
return data; return data;
@ -116,7 +120,7 @@ public abstract class UserDataManager<K, U extends UserData<K>> {
} catch (Exception ex) { } catch (Exception ex) {
getPlugin().error("加载用户 " + serializeKey(key) + " 数据失败,请检查相关配置!"); getPlugin().error("加载用户 " + serializeKey(key) + " 数据失败,请检查相关配置!");
ex.printStackTrace(); ex.printStackTrace();
return empty(key); return errorUser(key);
} }
}, executor).thenApply((data) -> { }, executor).thenApply((data) -> {
@ -223,22 +227,23 @@ public abstract class UserDataManager<K, U extends UserData<K>> {
return loadGroup(allKeys, (v) -> false); return loadGroup(allKeys, (v) -> false);
} }
public @NotNull CompletableFuture<Integer> saveAll() { public void saveAll() {
CompletableFuture<Integer> future = CompletableFuture.completedFuture(0); if (getDataCache().isEmpty()) return;
if (getDataCache().isEmpty()) return future; for (U u : getDataCache().values()) {
try {
for (U value : getDataCache().values()) { saveData(u);
future = future.thenCombine(save(value), (before, result) -> before + (result ? 1 : 0)); } catch (Exception e) {
getPlugin().error("保存用户 " + serializeKey(u.getKey()) + " 数据失败,请检查相关配置!");
e.printStackTrace();
}
} }
return future;
} }
public @NotNull CompletableFuture<Integer> unloadAll() { public int unloadAll(boolean save) {
return saveAll().thenApply(result -> { if (save) saveAll();
getDataCache().clear(); int size = getDataCache().size();
return result; getDataCache().clear();
}); return size;
} }

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<version>1.5.6</version> <version>1.5.7</version>
<relativePath>../../pom.xml</relativePath> <relativePath>../../pom.xml</relativePath>
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId> <artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>1.5.6</version> <version>1.5.7</version>
<modules> <modules>
<module>base/color</module> <module>base/color</module>
<module>base/utils</module> <module>base/utils</module>