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.
138 lines
4.1 KiB
138 lines
4.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CoreAgent.ProtocolClient.Context.UeStateManager
|
|
{
|
|
/// <summary>
|
|
/// UeIdentifierManager 字符串ID管理部分
|
|
/// </summary>
|
|
public partial class UeIdentifierManager
|
|
{
|
|
#region 字符串ID管理方法
|
|
|
|
/// <summary>
|
|
/// 将字符串转换为ID,如果不存在则创建新的ID
|
|
/// 性能优化:使用缓存减少重复计算
|
|
/// </summary>
|
|
/// <param name="str">要转换的字符串</param>
|
|
/// <returns>对应的ID</returns>
|
|
/// <exception cref="ArgumentNullException">字符串为空时抛出</exception>
|
|
public int StringToId(string str)
|
|
{
|
|
if (string.IsNullOrEmpty(str))
|
|
{
|
|
_logger?.LogWarning("尝试转换空字符串为ID");
|
|
return 0;
|
|
}
|
|
|
|
lock (_lockObject)
|
|
{
|
|
IncrementCacheStat("StringToId");
|
|
|
|
if (_stringIdCache.TryGetValue(str, out int existingId))
|
|
{
|
|
IncrementCacheStat("CacheHits");
|
|
return existingId;
|
|
}
|
|
|
|
IncrementCacheStat("CacheMisses");
|
|
int newId = _stringList.Count + 1;
|
|
_stringIdCache[str] = newId;
|
|
_stringList.Add(str);
|
|
|
|
_logger?.LogDebug("创建新的字符串ID映射: '{String}' -> {Id}", str, newId);
|
|
return newId;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取对应的字符串
|
|
/// 性能优化:使用缓存减少重复计算
|
|
/// </summary>
|
|
/// <param name="id">字符串ID</param>
|
|
/// <returns>对应的字符串,如果ID无效则返回空字符串</returns>
|
|
public string IdToString(int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
_logger?.LogWarning("尝试获取无效ID的字符串: {Id}", id);
|
|
return string.Empty;
|
|
}
|
|
|
|
lock (_lockObject)
|
|
{
|
|
IncrementCacheStat("IdToString");
|
|
|
|
if (id <= _stringList.Count)
|
|
{
|
|
IncrementCacheStat("CacheHits");
|
|
return _stringList[id - 1];
|
|
}
|
|
|
|
IncrementCacheStat("CacheMisses");
|
|
_logger?.LogWarning("ID超出范围: {Id}, 最大ID: {MaxId}", id, _stringList.Count);
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量转换字符串为ID
|
|
/// 性能优化:批量处理减少锁开销
|
|
/// </summary>
|
|
/// <param name="strings">字符串列表</param>
|
|
/// <returns>对应的ID列表</returns>
|
|
public List<int> StringToIdBatch(IEnumerable<string> strings)
|
|
{
|
|
if (strings == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(strings));
|
|
}
|
|
|
|
var results = new List<int>();
|
|
lock (_lockObject)
|
|
{
|
|
foreach (var str in strings)
|
|
{
|
|
results.Add(StringToId(str));
|
|
}
|
|
}
|
|
|
|
_logger?.LogDebug("批量转换字符串为ID,共处理 {Count} 个字符串", results.Count);
|
|
return results;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字符串缓存信息
|
|
/// </summary>
|
|
/// <returns>缓存信息</returns>
|
|
public (int CacheSize, int ListSize) GetStringCacheInfo()
|
|
{
|
|
lock (_lockObject)
|
|
{
|
|
return (_stringIdCache.Count, _stringList.Count);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 私有辅助方法
|
|
|
|
/// <summary>
|
|
/// 增加缓存统计计数
|
|
/// </summary>
|
|
/// <param name="statName">统计名称</param>
|
|
private void IncrementCacheStat(string statName)
|
|
{
|
|
if (_cacheStats.ContainsKey(statName))
|
|
{
|
|
_cacheStats[statName]++;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|