fix(config): 修复配置管理器变更检测和资源配置优化

- 修改 ConfigurationManager 中的 AddOrUpdate 逻辑,先获取旧值再更新以正确检测变更
- 只有在配置值真正发生变化时才触发监听器回调
- 更新异常日志记录方式,移除冗余的标签前缀
- 将 ConfigWatcherUnRegister 移动到正确的命名空间
- 修复 ResourceManager 中的引用计数逻辑,移除重复的 AddReference 调用
- 优化资源加载和卸载时的异常处理和日志记录
- 更新测试注释以反映正确的引用计数行为
This commit is contained in:
GeWuYou 2026-03-05 08:27:34 +08:00 committed by gewuyou
parent 7919c93f44
commit 54bed12056
4 changed files with 14 additions and 18 deletions

View File

@ -200,7 +200,7 @@ public class ResourceManagerTests
var handle = _resourceManager.GetHandle<TestResource>("test/resource1.txt");
handle!.Dispose();
// 资源应该仍然存在,因为还有一个初始引用
// 引用计数降为 0但手动释放策略不会自动卸载
Assert.That(_resourceManager.IsLoaded("test/resource1.txt"), Is.True);
}

View File

@ -1,6 +1,6 @@
using GFramework.Core.Abstractions.events;
namespace GFramework.Core.Abstractions.configuration;
namespace GFramework.Core.configuration;
/// <summary>
/// 配置监听取消注册接口

View File

@ -92,9 +92,13 @@ public class ConfigurationManager : IConfigurationManager
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentException(KeyCannotBeNullOrEmptyMessage, nameof(key));
var oldValue = _configs.AddOrUpdate(key, value!, (_, _) => value!);
// 先获取旧值,以便正确检测变更
_configs.TryGetValue(key, out var oldValue);
// 触发监听器
// 更新配置值
_configs[key] = value!;
// 只有在值真正变化时才触发监听器
if (!EqualityComparer<object>.Default.Equals(oldValue, value))
{
NotifyWatchers(key, value);
@ -293,8 +297,7 @@ public class ConfigurationManager : IConfigurationManager
catch (Exception ex)
{
// 防止监听器异常影响其他监听器
_logger.Error(
$"[ConfigurationManager] Error in config watcher for key '{key}': {ex.Message}");
_logger.Error($"Error in config watcher for key '{key}'", ex);
}
}
}

View File

@ -48,7 +48,6 @@ public class ResourceManager : IResourceManager
var cached = _cache.Get<T>(path);
if (cached != null)
{
_cache.AddReference(path);
return cached;
}
@ -59,7 +58,6 @@ public class ResourceManager : IResourceManager
cached = _cache.Get<T>(path);
if (cached != null)
{
_cache.AddReference(path);
return cached;
}
@ -73,12 +71,11 @@ public class ResourceManager : IResourceManager
{
var resource = loader.Load(path);
_cache.Add(path, resource);
_cache.AddReference(path);
return resource;
}
catch (Exception ex)
{
_logger.Error($"[ResourceManager] Failed to load resource '{path}': {ex.Message}");
_logger.Error($"Failed to load resource '{path}'", ex);
return null;
}
}
@ -96,7 +93,6 @@ public class ResourceManager : IResourceManager
var cached = _cache.Get<T>(path);
if (cached != null)
{
_cache.AddReference(path);
return cached;
}
@ -117,19 +113,17 @@ public class ResourceManager : IResourceManager
{
// 已经被其他线程加载了,卸载当前加载的资源
loader.Unload(resource);
_cache.AddReference(path);
return cached;
}
_cache.Add(path, resource);
_cache.AddReference(path);
}
return resource;
}
catch (Exception ex)
{
_logger.Error($"[ResourceManager] Failed to load resource '{path}': {ex.Message}");
_logger.Error($"Failed to load resource '{path}'", ex);
return null;
}
}
@ -168,8 +162,7 @@ public class ResourceManager : IResourceManager
var refCount = _cache.GetReferenceCount(path);
if (refCount > 0)
{
_logger.Error(
$"[ResourceManager] Cannot unload resource '{path}' with {refCount} active references");
_logger.Error($"Cannot unload resource '{path}' with {refCount} active references");
return false;
}
@ -287,7 +280,7 @@ public class ResourceManager : IResourceManager
}
catch (Exception ex)
{
_logger.Error($"[ResourceManager] Error unloading resource: {ex.Message}");
_logger.Error("Error unloading resource", ex);
}
}
@ -300,7 +293,7 @@ public class ResourceManager : IResourceManager
}
catch (Exception ex)
{
_logger.Error($"[ResourceManager] Error disposing resource: {ex.Message}");
_logger.Error("Error disposing resource", ex);
}
}
}