mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(data): 统一设置数据仓库实现线程安全和键管理
- 添加 UnifiedKey 属性用于获取统一键值 - 在 SaveAsync 方法中添加异步锁确保线程安全 - 使用 UnifiedKey 替换 fileName 进行文件读写操作 - 在 EnsureLoadedAsync 中使用统一键进行存在性检查 - 优化 SaveAllToUnifiedFile 方法使用新的键属性 - 移除多余的空行以改善代码格式 - [release ci]
This commit is contained in:
parent
e5d6f72bac
commit
eb60b05144
@ -45,6 +45,9 @@ public class UnifiedSettingsDataRepository(
|
||||
|
||||
private UnifiedSettingsFile File =>
|
||||
_file ?? throw new InvalidOperationException("UnifiedSettingsFile not set.");
|
||||
|
||||
private string UnifiedKey => GetUnifiedKey();
|
||||
|
||||
// =========================
|
||||
// IDataRepository
|
||||
// =========================
|
||||
@ -75,6 +78,9 @@ public class UnifiedSettingsDataRepository(
|
||||
/// <returns>异步操作任务</returns>
|
||||
public async Task SaveAsync<T>(IDataLocation location, T data)
|
||||
where T : class, IData
|
||||
{
|
||||
await _lock.WaitAsync();
|
||||
try
|
||||
{
|
||||
await EnsureLoadedAsync();
|
||||
|
||||
@ -83,10 +89,15 @@ public class UnifiedSettingsDataRepository(
|
||||
|
||||
_file!.Sections[key] = serialized;
|
||||
|
||||
await Storage.WriteAsync(fileName, _file);
|
||||
await Storage.WriteAsync(UnifiedKey, _file);
|
||||
if (_options.EnableEvents)
|
||||
this.SendEvent(new DataSavedEvent<T>(data));
|
||||
}
|
||||
finally
|
||||
{
|
||||
_lock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查指定位置的数据是否存在
|
||||
@ -175,9 +186,11 @@ public class UnifiedSettingsDataRepository(
|
||||
{
|
||||
if (_loaded) return;
|
||||
|
||||
if (await Storage.ExistsAsync(fileName))
|
||||
var key = UnifiedKey;
|
||||
|
||||
if (await Storage.ExistsAsync(key))
|
||||
{
|
||||
_file = await Storage.ReadAsync<UnifiedSettingsFile>(fileName);
|
||||
_file = await Storage.ReadAsync<UnifiedSettingsFile>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -192,6 +205,7 @@ public class UnifiedSettingsDataRepository(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将缓存中的所有数据保存到统一文件
|
||||
/// </summary>
|
||||
@ -200,7 +214,7 @@ public class UnifiedSettingsDataRepository(
|
||||
await _lock.WaitAsync();
|
||||
try
|
||||
{
|
||||
await Storage.WriteAsync(GetUnifiedKey(), File);
|
||||
await Storage.WriteAsync(UnifiedKey, _file);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user