优化字符串数组和Object数组使用,减少GC分配

This commit is contained in:
大石头 2025-02-02 10:52:23 +08:00
parent 868f994cdd
commit 1b16e8369d
2 changed files with 30 additions and 25 deletions

View File

@ -470,7 +470,11 @@ public class FullRedis : Redis
{
if (keys == null || keys.Length == 0) return 0;
keys = keys.Select(GetKey).ToArray();
//keys = keys.Select(GetKey).ToArray();
for (var i = 0; i < keys.Length; i++)
{
keys[i] = GetKey(keys[i]);
}
if (keys.Length == 1) return base.Remove(keys[0]);
InitCluster();
@ -481,7 +485,7 @@ public class FullRedis : Redis
}
else
{
return Execute(keys.FirstOrDefault(), (rds, k) => rds.Execute<Int32>("DEL", keys), true);
return Execute(keys[0], (rds, k) => rds.Execute<Int32>("DEL", keys), true);
}
}
#endregion
@ -574,24 +578,24 @@ public class FullRedis : Redis
/// <returns></returns>
public override IDictionary<String, T> GetDictionary<T>(String key) => new RedisHash<String, T>(this, key);
/// <summary>
/// 获取哈希表所有数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public IDictionary<String, T> GetHashAll<T>(String key)
{
var hashMap = new RedisHash<String, T>(this, key);
var nCount = hashMap!.Count();
var sModel = new SearchModel()
{
Pattern = "*",
Position = 0,
Count = nCount
};
return hashMap!.Search(sModel).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
/// <summary>
/// 获取哈希表所有数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public IDictionary<String, T> GetHashAll<T>(String key)
{
var hashMap = new RedisHash<String, T>(this, key);
var nCount = hashMap!.Count();
var sModel = new SearchModel()
{
Pattern = "*",
Position = 0,
Count = nCount
};
return hashMap!.Search(sModel).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
/// <summary>获取队列快速LIST结构无需确认</summary>
/// <typeparam name="T"></typeparam>

View File

@ -999,16 +999,17 @@ public class RedisClient : DisposeBase
{
if (values == null || values.Count == 0) throw new ArgumentNullException(nameof(values));
var ps = new List<Object>();
var k = 0;
var ps = new Object[values.Count * 2];
foreach (var item in values)
{
ps.Add(item.Key);
ps[k++] = item.Key;
if (item.Value == null) throw new NullReferenceException();
ps.Add(item.Value);
ps[k++] = item.Value;
}
var rs = Execute<String>("MSET", ps.ToArray());
var rs = Execute<String>("MSET", ps);
if (rs != "OK")
{
using var span = Host.Tracer?.NewSpan($"redis:{Name}:ErrorSetAll", values);