为 Config 添加延迟写入

This commit is contained in:
龙腾猫跃 committed 2026-06-26 08:56:56 +08:00
1 parent 3e579aff79
commit fc92759a39
2 files changed
+42 -8

No files matched your search

+37 -7
View File
@@ -23,7 +23,9 @@ public class JsonConfigProvider : IConfigProvider {
private readonly string filePath;
public JsonConfigProvider(string filePath) {
this.filePath = filePath;
saveAction = Throttler.Throttle(SaveImmediately, TimeSpan.FromMilliseconds(500), leading: false, trailing: true);
InitJson();
AppDomain.CurrentDomain.ProcessExit += (_, _) => SaveBeforeExit();
}
// 原始文件缓存
@@ -41,8 +43,11 @@ public class JsonConfigProvider : IConfigProvider {
});
}
// ================================= 设置、读取、缓存 =================================
public void SetToCache<T>(string key, T? value, bool encrypted) {
cache[key] = value;
lock (json) {
if (value is null) {
// 直接在 JSON 中表示为 null
json.Value[key] = JValue.CreateNull();
@@ -54,9 +59,14 @@ public class JsonConfigProvider : IConfigProvider {
json.Value[key] = JToken.FromObject(value);
}
}
}
public T? Read<T>(string key, T? defaultValue, bool encrypted) {
if (cache.TryGetValue(key, out var cachedValue)) return (T?) cachedValue; // 读取缓存
if (!json.Value.TryGetValue(key, out var entry)) return defaultValue; // 未找到该键
JToken entry;
lock (json) {
if (!json.Value.TryGetValue(key, out entry!)) return defaultValue; // 未找到该键
entry = entry.DeepClone();
}
try {
T? result;
if (entry.Type is JTokenType.Null or JTokenType.Undefined) {
@@ -77,18 +87,38 @@ public class JsonConfigProvider : IConfigProvider {
}
}
public bool HasValue(string key)
=> json.Value.ContainsKey(key);
public bool HasValue(string key) {
lock (json) return json.Value.ContainsKey(key);
}
public void RemoveFromCache(string key) {
json.Value.Remove(key);
lock (json) json.Value.Remove(key);
cache.TryRemove(key, out _);
}
public void DiscardCache() {
InitJson();
lock (json) InitJson();
cache.Clear();
}
public void Save()
=> FileUtils.Write(filePath, json.Value.ToString(Formatting.Indented));
// ===================================== 保存 =====================================
private readonly RateLimitedAction saveAction;
private bool isDirty = false;
public void Save() {
lock (json) isDirty = true;
saveAction.Invoke();
}
private void SaveBeforeExit() {
bool shouldSave;
lock (json) shouldSave = isDirty;
if (shouldSave) SaveImmediately();
saveAction.Dispose();
}
private void SaveImmediately() {
lock (json) {
FileUtils.Write(filePath, json.Value.ToString(Formatting.Indented));
isDirty = false;
}
}
}
public static class ConfigUtils {
+5 -1
View File
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
@@ -22,6 +22,7 @@
</ItemGroup>
<ItemGroup>
<Using Include="ThrottleDebounce" />
<Using Include="System.Diagnostics.CodeAnalysis" />
<Using Include="System.Collections" />
<Using Include="System.Collections.Generic" />
@@ -36,6 +37,9 @@
</ItemGroup>
<ItemGroup>
<Reference Include="ThrottleDebounce">
<HintPath>..\..\Plain Craft Launcher 2\Resources\ThrottleDebounce.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\Plain Craft Launcher 2\Resources\Newtonsoft.Json.dll</HintPath>
</Reference>