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 =>
|
private UnifiedSettingsFile File =>
|
||||||
_file ?? throw new InvalidOperationException("UnifiedSettingsFile not set.");
|
_file ?? throw new InvalidOperationException("UnifiedSettingsFile not set.");
|
||||||
|
|
||||||
|
private string UnifiedKey => GetUnifiedKey();
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
// IDataRepository
|
// IDataRepository
|
||||||
// =========================
|
// =========================
|
||||||
@ -76,16 +79,24 @@ public class UnifiedSettingsDataRepository(
|
|||||||
public async Task SaveAsync<T>(IDataLocation location, T data)
|
public async Task SaveAsync<T>(IDataLocation location, T data)
|
||||||
where T : class, IData
|
where T : class, IData
|
||||||
{
|
{
|
||||||
await EnsureLoadedAsync();
|
await _lock.WaitAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await EnsureLoadedAsync();
|
||||||
|
|
||||||
var key = location.Key;
|
var key = location.Key;
|
||||||
var serialized = Serializer.Serialize(data);
|
var serialized = Serializer.Serialize(data);
|
||||||
|
|
||||||
_file!.Sections[key] = serialized;
|
_file!.Sections[key] = serialized;
|
||||||
|
|
||||||
await Storage.WriteAsync(fileName, _file);
|
await Storage.WriteAsync(UnifiedKey, _file);
|
||||||
if (_options.EnableEvents)
|
if (_options.EnableEvents)
|
||||||
this.SendEvent(new DataSavedEvent<T>(data));
|
this.SendEvent(new DataSavedEvent<T>(data));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_lock.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -175,9 +186,11 @@ public class UnifiedSettingsDataRepository(
|
|||||||
{
|
{
|
||||||
if (_loaded) return;
|
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
|
else
|
||||||
{
|
{
|
||||||
@ -192,6 +205,7 @@ public class UnifiedSettingsDataRepository(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将缓存中的所有数据保存到统一文件
|
/// 将缓存中的所有数据保存到统一文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -200,7 +214,7 @@ public class UnifiedSettingsDataRepository(
|
|||||||
await _lock.WaitAsync();
|
await _lock.WaitAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Storage.WriteAsync(GetUnifiedKey(), File);
|
await Storage.WriteAsync(UnifiedKey, _file);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user