refactor(setting): 优化设置模型的并发安全性和性能

- 将字典集合替换为ConcurrentDictionary以提高并发安全性
- 添加方法缓存机制减少反射调用开销
- 实现迁移缓存避免重复查询操作
- 增加异步加载异常处理和日志记录
- 实现IDisposable接口添加资源清理功能
- 优化GetOrAdd方法简化数据获取逻辑
This commit is contained in:
GeWuYou 2026-01-27 22:38:49 +08:00
parent 11c94d462f
commit a29a484834

View File

@ -1,5 +1,9 @@
using GFramework.Core.Abstractions.versioning; using System.Collections.Concurrent;
using System.Reflection;
using GFramework.Core.Abstractions.logging;
using GFramework.Core.Abstractions.versioning;
using GFramework.Core.extensions; using GFramework.Core.extensions;
using GFramework.Core.logging;
using GFramework.Core.model; using GFramework.Core.model;
using GFramework.Game.Abstractions.setting; using GFramework.Game.Abstractions.setting;
@ -8,14 +12,24 @@ namespace GFramework.Game.setting;
/// <summary> /// <summary>
/// 设置模型类,用于管理不同类型的应用程序设置部分 /// 设置模型类,用于管理不同类型的应用程序设置部分
/// </summary> /// </summary>
public class SettingsModel : AbstractModel, ISettingsModel public class SettingsModel : AbstractModel, ISettingsModel, IDisposable
{ {
private readonly Dictionary<Type, IApplyAbleSettings> _applicators = new(); private static readonly ILogger Log = LoggerFactoryResolver.Provider.CreateLogger(nameof(SettingsModel));
private readonly Dictionary<Type, ISettingsData> _dataSettings = new(); private readonly ConcurrentDictionary<Type, IApplyAbleSettings> _applicators = new();
private readonly Dictionary<(Type type, int from), ISettingsMigration> _migrations = new(); private readonly ConcurrentDictionary<Type, ISettingsData> _dataSettings = new();
private readonly ConcurrentDictionary<Type, MethodInfo> _loadAsyncMethodCache = new();
private readonly ConcurrentDictionary<Type, Dictionary<int, ISettingsMigration>> _migrationCache = new();
private readonly ConcurrentDictionary<(Type type, int from), ISettingsMigration> _migrations = new();
private bool _disposed;
private ISettingsPersistence? _persistence; private ISettingsPersistence? _persistence;
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
// ----------------------------- // -----------------------------
// Data // Data
// ----------------------------- // -----------------------------
@ -27,13 +41,7 @@ public class SettingsModel : AbstractModel, ISettingsModel
/// <returns>指定类型的设置数据实例</returns> /// <returns>指定类型的设置数据实例</returns>
public T GetData<T>() where T : class, ISettingsData, new() public T GetData<T>() where T : class, ISettingsData, new()
{ {
var type = typeof(T); return (T)_dataSettings.GetOrAdd(typeof(T), _ => new T());
if (_dataSettings.TryGetValue(type, out var data))
return (T)data;
var created = new T();
_dataSettings[type] = created;
return created;
} }
/// <summary> /// <summary>
@ -135,7 +143,16 @@ public class SettingsModel : AbstractModel, ISettingsModel
var type = section.GetType(); var type = section.GetType();
var current = section; var current = section;
while (_migrations.TryGetValue((type, versioned.Version), out var migration)) if (!_migrationCache.TryGetValue(type, out var versionMap))
{
versionMap = _migrations
.Where(kv => kv.Key.type == type)
.ToDictionary(kv => kv.Key.from, kv => kv.Value);
_migrationCache[type] = versionMap;
}
while (versionMap.TryGetValue(versioned.Version, out var migration))
{ {
current = migration.Migrate(current); current = migration.Migrate(current);
versioned = (IVersioned)current; versioned = (IVersioned)current;
@ -144,6 +161,7 @@ public class SettingsModel : AbstractModel, ISettingsModel
return current; return current;
} }
// ----------------------------- // -----------------------------
// Load / Init // Load / Init
// ----------------------------- // -----------------------------
@ -161,20 +179,24 @@ public class SettingsModel : AbstractModel, ISettingsModel
type.GetConstructor(Type.EmptyTypes) == null) type.GetConstructor(Type.EmptyTypes) == null)
continue; continue;
// Load<T>() try
var method = typeof(ISettingsPersistence) {
.GetMethod(nameof(ISettingsPersistence.LoadAsync))! var method = _loadAsyncMethodCache.GetOrAdd(type, t => typeof(ISettingsPersistence)
.MakeGenericMethod(type); .GetMethod(nameof(ISettingsPersistence.LoadAsync))!
.MakeGenericMethod(t));
var task = (Task)method.Invoke(_persistence, null)!; var task = (Task)method.Invoke(_persistence, null)!;
await task; await task;
var loaded = (ISettingsSection)((dynamic)task).Result; var loaded = (ISettingsSection)((dynamic)task).Result;
var migrated = MigrateIfNeeded(loaded);
// ★ 关键:迁移 _dataSettings[type] = (ISettingsData)migrated;
var migrated = MigrateIfNeeded(loaded); _migrationCache.TryRemove(type, out _);
}
_dataSettings[type] = (ISettingsData)migrated; catch (Exception ex)
{
Log.Error($"Failed to load settings for {type.Name}: {ex}", ex);
}
} }
} }
@ -185,4 +207,22 @@ public class SettingsModel : AbstractModel, ISettingsModel
{ {
_persistence = this.GetUtility<ISettingsPersistence>(); _persistence = this.GetUtility<ISettingsPersistence>();
} }
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
// 清理托管资源
_dataSettings.Clear();
_applicators.Clear();
_migrations.Clear();
_migrationCache.Clear();
_loadAsyncMethodCache.Clear();
}
// 清理非托管资源(如果有)
_disposed = true;
}
} }