You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreAgent.ProtocolClient.Enums;
using CoreAgent.ProtocolClient.Models;
using Microsoft.Extensions.Logging;
namespace CoreAgent.ProtocolClient.Context.UeStateManager
{
/// <summary>
/// UE标识符管理类
/// 负责管理UE信息、标识符映射、时间戳等状态
/// 优化版本:增强异常处理、性能优化、代码拆分
/// </summary>
public partial class UeIdentifierManager
{
#region 公共属性 - 映射表
/// <summary>
/// TMSI到UE ID的映射表
/// </summary>
public Dictionary<uint, int> TmsiToUeId { get; set; } = new();
/// <summary>
/// 请求TMSI到UE ID的映射表
/// </summary>
public Dictionary<uint, int> RequestTmsiToUeId { get; set; } = new();
/// <summary>
/// IMSI到UE ID的映射表
/// </summary>
public Dictionary<string, List<int>> ImsiToUeId { get; set; } = new();
/// <summary>
/// PLMN到UE ID的映射表
/// </summary>
public Dictionary<string, List<int>> PlmnToUeId { get; set; } = new();
/// <summary>
/// Service Request TMSI到UE ID的映射表
/// </summary>
public Dictionary<uint, List<int>> SrTmsiToUeId { get; set; } = new();
/// <summary>
/// RNTI到UE ID的映射表
/// </summary>
public Dictionary<int, int> RntiToUeId { get; set; } = new();
/// <summary>
/// UE信息列表,以UE ID为键
/// </summary>
public Dictionary<int, UEInfo> UeList { get; set; } = new();
#endregion
#region 公共属性 - 时间戳和小区信息
/// <summary>
/// 最后处理的时间戳
/// </summary>
public long LastTimestamp { get; set; }
/// <summary>
/// 时间戳偏移量
/// </summary>
public long TimestampOffset { get; set; }
/// <summary>
/// 最后处理的小区ID
/// </summary>
public int? LastCell { get; set; }
#endregion
#region 私有字段 - 字符串缓存和性能优化
/// <summary>
/// 字符串到ID的映射缓存
/// </summary>
private readonly Dictionary<string, int> _stringIdCache = new();
/// <summary>
/// ID到字符串的列表缓存
/// </summary>
private readonly List<string> _stringList = new();
/// <summary>
/// 日志记录器
/// </summary>
private readonly ILogger<UeIdentifierManager> _logger;
/// <summary>
/// 缓存统计信息
/// </summary>
private readonly Dictionary<string, int> _cacheStats = new();
/// <summary>
/// 线程锁对象
/// </summary>
private readonly object _lockObject = new object();
#endregion
#region 构造函数
/// <summary>
/// 初始化UE标识符管理器
/// </summary>
/// <param name="logger">日志记录器</param>
public UeIdentifierManager(ILogger<UeIdentifierManager> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
InitializeProtocolTypeStrings();
InitializeCacheStats();
}
#endregion
#region 私有初始化方法
/// <summary>
/// 初始化协议类型字符串缓存
/// </summary>
private void InitializeProtocolTypeStrings()
{
try
{
string[] protocolTypeNames = Enum.GetNames(typeof(LogChannelId));
foreach (var protocolTypeName in protocolTypeNames)
{
StringToId(protocolTypeName);
}
_logger.LogDebug("协议类型字符串缓存初始化完成,共 {Count} 个类型", protocolTypeNames.Length);
}
catch (Exception ex)
{
_logger.LogError(ex, "初始化协议类型字符串缓存失败");
throw new CacheException("ProtocolTypeStrings", "初始化协议类型字符串缓存失败", ex);
}
}
/// <summary>
/// 初始化缓存统计信息
/// </summary>
private void InitializeCacheStats()
{
_cacheStats["StringToId"] = 0;
_cacheStats["IdToString"] = 0;
_cacheStats["CacheHits"] = 0;
_cacheStats["CacheMisses"] = 0;
}
#endregion
#region 公共方法 - 统计和监控
/// <summary>
/// 获取缓存统计信息
/// </summary>
/// <returns>缓存统计信息字典</returns>
public Dictionary<string, int> GetCacheStats()
{
lock (_lockObject)
{
return new Dictionary<string, int>(_cacheStats);
}
}
/// <summary>
/// 获取映射表统计信息
/// </summary>
/// <returns>映射表统计信息</returns>
public Dictionary<string, int> GetMappingStats()
{
return new Dictionary<string, int>
{
["TmsiToUeId"] = TmsiToUeId.Count,
["RequestTmsiToUeId"] = RequestTmsiToUeId.Count,
["ImsiToUeId"] = ImsiToUeId.Count,
["PlmnToUeId"] = PlmnToUeId.Count,
["SrTmsiToUeId"] = SrTmsiToUeId.Count,
["RntiToUeId"] = RntiToUeId.Count,
["UeList"] = UeList.Count
};
}
/// <summary>
/// 清理缓存
/// </summary>
public void ClearCache()
{
lock (_lockObject)
{
_stringIdCache.Clear();
_stringList.Clear();
InitializeCacheStats();
_logger.LogInformation("字符串缓存已清理");
}
}
#endregion
}
}