RedisClient新增ExecuteByKey方法,简化子级key操作

This commit is contained in:
智能大石头 2025-05-05 08:03:34 +08:00
parent 1964bf15a4
commit aa66a0c731
10 changed files with 49 additions and 172 deletions

View File

@ -786,15 +786,7 @@ public class FullRedis : Redis
{
// 这里提前打包参数需要处理key前缀。其它普通情况由Execute处理
key = GetKey(key);
var args = new List<Object>
{
key
};
foreach (var item in values)
{
if (item != null) args.Add(item);
}
return Execute(key, (rc, k) => rc.Execute<Int32>("RPUSH", args.ToArray()), true);
return Execute(key, (rc, k) => rc.ExecuteByKey<T, Int32>("RPUSH", key, values), true);
}
/// <summary>向列表头部插入</summary>
@ -806,15 +798,7 @@ public class FullRedis : Redis
{
// 这里提前打包参数需要处理key前缀。其它普通情况由Execute处理
key = GetKey(key);
var args = new List<Object>
{
key
};
foreach (var item in values)
{
if (item != null) args.Add(item);
}
return Execute(key, (rc, k) => rc.Execute<Int32>("LPUSH", args.ToArray()), true);
return Execute(key, (rc, k) => rc.ExecuteByKey<T, Int32>("LPUSH", key, values), true);
}
/// <summary>从列表末尾弹出一个元素</summary>
@ -935,15 +919,7 @@ public class FullRedis : Redis
public virtual Int32 SADD<T>(String key, params T[] members)
{
key = GetKey(key);
var args = new List<Object>
{
key
};
foreach (var item in members)
{
if (item != null) args.Add(item);
}
return Execute(key, (rc, k) => rc.Execute<Int32>("SADD", args.ToArray()), true);
return Execute(key, (rc, k) => rc.ExecuteByKey<T, Int32>("SADD", key, members), true);
}
/// <summary>向集合删除多个元素</summary>
@ -954,15 +930,7 @@ public class FullRedis : Redis
public virtual Int32 SREM<T>(String key, params T[] members)
{
key = GetKey(key);
var args = new List<Object>
{
key
};
foreach (var item in members)
{
if (item != null) args.Add(item);
}
return Execute(key, (rc, k) => rc.Execute<Int32>("SREM", args.ToArray()), true);
return Execute(key, (rc, k) => rc.ExecuteByKey<T, Int32>("SREM", key, members), true);
}
/// <summary>获取所有元素</summary>

View File

@ -25,18 +25,7 @@ public class HyperLogLog : RedisBase
/// </remarks>
/// <param name="items"></param>
/// <returns></returns>
public Int32 Add(params String[] items)
{
var args = new List<Object>
{
Key
};
foreach (var item in items)
{
args.Add(item);
}
return Execute((rc, k) => rc.Execute<Int32>("PFADD", args.ToArray()), true);
}
public Int32 Add(params String[] items) => Execute((rc, k) => rc.ExecuteByKey<String, Int32>("PFADD", Key, items), true);
/// <summary>近似基数</summary>
/// <remarks>

View File

@ -74,9 +74,9 @@ public class RedisQueue<T> : QueueBase, IProducerConsumer<T>
foreach (var item in values)
{
if (AttachTraceId)
args.Add(Redis.AttachTraceId(item));
args.Add(Redis.AttachTraceId(item!));
else
args.Add(item);
args.Add(item!);
}
var rs = 0;

View File

@ -706,6 +706,21 @@ public class RedisClient : DisposeBase
}
}
/// <summary>执行命令</summary>
/// <typeparam name="TArg"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="cmd"></param>
/// <param name="key"></param>
/// <param name="args"></param>
/// <returns></returns>
public TResult? ExecuteByKey<TArg, TResult>(String cmd, String key, TArg?[] args)
{
var arr = new Object?[args.Length + 1];
arr[0] = key;
Array.Copy(args, 0, arr, 1, args.Length);
return Execute<TResult>(cmd, arr);
}
/// <summary>尝试执行命令。返回基本类型、对象、对象数组</summary>
/// <param name="cmd"></param>
/// <param name="args"></param>

View File

@ -57,13 +57,7 @@ public class RedisGeo : RedisBase
/// <returns></returns>
public GeoInfo[]? GetPosition(params String[] members)
{
var args = new List<Object> { Key };
foreach (var item in members)
{
args.Add(item);
}
var rs = Execute((rc, k) => rc.Execute<Object[]>("GEOPOS", args.ToArray()), false);
var rs = Execute((rc, k) => rc.ExecuteByKey<String, Object[]>("GEOPOS", Key, members), false);
if (rs == null || rs.Length == 0) return null;
var list = new List<GeoInfo>();
@ -98,16 +92,7 @@ public class RedisGeo : RedisBase
/// </remarks>
/// <param name="members"></param>
/// <returns></returns>
public String[]? GetHash(params String[] members)
{
var args = new List<Object> { Key };
foreach (var item in members)
{
args.Add(item);
}
return Execute((rc, k) => rc.Execute<String[]>("GEOHASH", args.ToArray()), false);
}
public String[]? GetHash(params String[] members) => Execute((rc, k) => rc.ExecuteByKey<String, String[]>("GEOHASH", Key, members), false);
/// <summary>以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素</summary>
/// <param name="longitude"></param>

View File

@ -1,5 +1,4 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using NewLife.Caching.Models;
using NewLife.Data;
using NewLife.Reflection;
@ -100,36 +99,12 @@ public class RedisHash<TKey, TValue> : RedisBase, IDictionary<TKey, TValue>
/// <summary>批量删除</summary>
/// <param name="fields"></param>
/// <returns></returns>
public Int32 HDel(params TKey[] fields)
{
var args = new List<Object?>
{
Key
};
foreach (var item in fields)
{
args.Add(item);
}
return Execute((r, k) => r.Execute<Int32>("HDEL", args.ToArray()), true);
}
public Int32 HDel(params TKey[] fields) => Execute((r, k) => r.ExecuteByKey<TKey, Int32>("HDEL", Key, fields), true);
/// <summary>只在 key 指定的哈希集中不存在指定的字段时,设置字段的值</summary>
/// <param name="fields"></param>
/// <returns></returns>
public TValue[]? HMGet(params TKey[] fields)
{
var args = new List<Object?>
{
Key
};
foreach (var item in fields)
{
args.Add(item);
}
return Execute((r, k) => r.Execute<TValue[]>("HMGET", args.ToArray()));
}
public TValue[]? HMGet(params TKey[] fields) => Execute((r, k) => r.ExecuteByKey<TKey, TValue[]>("HMGET", Key, fields));
/// <summary>批量插入</summary>
/// <param name="keyValues"></param>

View File

@ -1,5 +1,4 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace NewLife.Caching;
@ -84,12 +83,6 @@ public class RedisList<T> : RedisBase, IList<T>
}
return -1;
//var count = Count;
//if (count > 1000)
//var arr = GetAll();
//return Array.IndexOf(arr, item);
}
/// <summary>在指定位置插入</summary>
@ -140,34 +133,12 @@ public class RedisList<T> : RedisBase, IList<T>
/// <summary>右边批量添加,返回队列元素总数</summary>
/// <param name="values"></param>
/// <returns>队列元素总数</returns>
public Int32 RPUSH(IEnumerable<T> values)
{
var args = new List<Object?>
{
Key
};
foreach (var item in values)
{
args.Add(item);
}
return Execute((rc, k) => rc.Execute<Int32>("RPUSH", args.ToArray()), true);
}
public Int32 RPUSH(IEnumerable<T> values) => Execute((rc, k) => rc.ExecuteByKey<T, Int32>("RPUSH", Key, values as T[] ?? values.ToArray()), true);
/// <summary>左边批量添加,返回队列元素总数</summary>
/// <param name="values"></param>
/// <returns>队列元素总数</returns>
public Int32 LPUSH(IEnumerable<T> values)
{
var args = new List<Object?>
{
Key
};
foreach (var item in values)
{
args.Add(item);
}
return Execute((rc, k) => rc.Execute<Int32>("LPUSH", args.ToArray()), true);
}
public Int32 LPUSH(IEnumerable<T> values) => Execute((rc, k) => rc.ExecuteByKey<T, Int32>("LPUSH", Key, values as T[] ?? values.ToArray()), true);
/// <summary>移除并返回最右边一个元素</summary>
/// <returns></returns>

View File

@ -63,50 +63,32 @@ public class RedisSet<T> : RedisBase, ICollection<T>
/// <summary>批量添加</summary>
/// <param name="members"></param>
/// <returns></returns>
public Int32 SAdd(params T[] members)
{
var args = new List<Object> { Key };
foreach (var item in members)
{
args.Add(item);
}
return Execute((r, k) => r.Execute<Int32>("SADD", args.ToArray()), true);
}
public Int32 SAdd(params T[] members) => Execute((r, k) => r.ExecuteByKey<T, Int32>("SADD", Key, members), true);
/// <summary>批量删除</summary>
/// <param name="members"></param>
/// <returns></returns>
public Int32 SDel(params T[] members)
{
var args = new List<Object> { Key };
foreach (var item in members)
{
args.Add(item);
}
return Execute((r, k) => r.Execute<Int32>("SREM", args.ToArray()), true);
}
public Int32 SDel(params T[] members) => Execute((r, k) => r.ExecuteByKey<T, Int32>("SREM", Key, members), true);
/// <summary>获取所有元素</summary>
/// <returns></returns>
public T[] GetAll() => Execute((r, k) => r.Execute<T[]>("SMEMBERS", Key));
public T[] GetAll() => Execute((r, k) => r.Execute<T[]>("SMEMBERS", Key)) ?? [];
/// <summary>将member从source集合移动到destination集合中</summary>
/// <param name="dest"></param>
/// <param name="member"></param>
/// <returns></returns>
public T[] Move(String dest, T member) => Execute((r, k) => r.Execute<T[]>("SMOVE", Key, dest, member), true);
public T[] Move(String dest, T member) => Execute((r, k) => r.Execute<T[]>("SMOVE", Key, dest, member), true) ?? [];
/// <summary>随机获取多个</summary>
/// <param name="count"></param>
/// <returns></returns>
public T[] RandomGet(Int32 count) => Execute((r, k) => r.Execute<T[]>("SRANDMEMBER", Key, count));
public T[] RandomGet(Int32 count) => Execute((r, k) => r.Execute<T[]>("SRANDMEMBER", Key, count)) ?? [];
/// <summary>随机获取并弹出</summary>
/// <param name="count"></param>
/// <returns></returns>
public T[] Pop(Int32 count) => Execute((r, k) => r.Execute<T[]>("SPOP", Key, count), true);
public T[] Pop(Int32 count) => Execute((r, k) => r.Execute<T[]>("SPOP", Key, count), true) ?? [];
/// <summary>模糊搜索,支持?和*</summary>
/// <param name="model">搜索模型</param>
@ -120,10 +102,10 @@ public class RedisSet<T> : RedisBase, ICollection<T>
var rs = Execute((r, k) => r.Execute<Object[]>("SSCAN", Key, p, "MATCH", model.Pattern + "", "COUNT", count));
if (rs == null || rs.Length != 2) break;
model.Position = (rs[0] as IPacket).ToStr().ToInt();
model.Position = (rs[0] as IPacket)!.ToStr().ToInt();
var ps = rs[1] as Object[];
foreach (IPacket item in ps)
foreach (IPacket item in ps!)
{
if (count-- > 0) yield return item.ToStr();
}
@ -138,4 +120,13 @@ public class RedisSet<T> : RedisBase, ICollection<T>
/// <returns></returns>
public virtual IEnumerable<String> Search(String pattern, Int32 count) => Search(new SearchModel { Pattern = pattern, Count = count });
#endregion
#region
/// <summary>返回第一个集合与其他集合之间的差异</summary>
/// <remarks>也可以认为说第一个集合中独有的元素</remarks>
/// <param name="keys"></param>
/// <returns></returns>
public T[] Diff(params String[] keys) => Execute((r, k) => r.ExecuteByKey<String, T[]>("SDIFF", Key, keys)) ?? [];
#endregion
}

View File

@ -36,7 +36,7 @@ public class RedisSortedSet<T> : RedisBase
foreach (var item in members)
{
args.Add(score);
args.Add(item);
args.Add(item!);
}
return Execute((rc, k) => rc.Execute<String>("ZADD", args.ToArray()), true).ToInt(-1);
}
@ -44,16 +44,7 @@ public class RedisSortedSet<T> : RedisBase
/// <summary>删除元素</summary>
/// <param name="members"></param>
/// <returns></returns>
public Int32 Remove(params T[] members)
{
var args = new List<Object> { Key };
foreach (var item in members)
{
args.Add(item);
}
return Execute((rc, k) => rc.Execute<Int32>("ZREM", args.ToArray()), true);
}
public Int32 Remove(params T[] members) => Execute((rc, k) => rc.ExecuteByKey<T, Int32>("ZREM", Key, members), true);
/// <summary>返回有序集key中成员member的score值</summary>
/// <param name="member"></param>
@ -89,7 +80,7 @@ public class RedisSortedSet<T> : RedisBase
foreach (var item in members)
{
args.Add(item.Value);
args.Add(item.Key);
args.Add(item.Key!);
}
return Execute((rc, k) => rc.Execute<Double>("ZADD", args.ToArray()), true);
}

View File

@ -27,15 +27,7 @@ public class RedisStack<T> : RedisBase, IProducerConsumer<T>
/// <summary>批量生产添加</summary>
/// <param name="values"></param>
/// <returns></returns>
public Int32 Add(params T[] values)
{
var args = new List<Object> { Key };
foreach (var item in values)
{
args.Add(item);
}
return Execute((rc, k) => rc.Execute<Int32>("RPUSH", args.ToArray()), true);
}
public Int32 Add(params T[] values) => Execute((rc, k) => rc.ExecuteByKey<T, Int32>("RPUSH", Key, values), true);
/// <summary>批量消费获取</summary>
/// <param name="count"></param>