Skip to content

Commit

Permalink
trim Null
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 24, 2024
1 parent eaaaebd commit 2921b5a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 30 deletions.
4 changes: 2 additions & 2 deletions NewLife.Redis.Extensions/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void Set(String key, Byte[] value, DistributedCacheEntryOptions options)
base.Set(key, value);
else
if (options.AbsoluteExpiration != null)
base.Set(key, value, options.AbsoluteExpiration.Value - DateTime.Now);
base.Set(key, value, options.AbsoluteExpiration.Value - DateTime.Now);
else if (options.AbsoluteExpirationRelativeToNow != null)
base.Set(key, value, options.AbsoluteExpirationRelativeToNow.Value);
else if (options.SlidingExpiration != null)
Expand Down Expand Up @@ -99,7 +99,7 @@ public void Set(String key, Byte[] value, DistributedCacheEntryOptions options)
/// 删除
/// </summary>
/// <param name="key"></param>
public void Remove(String key) => base.Remove(key);
public new void Remove(String key) => base.Remove(key);

/// <summary>
/// 异步删除
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Redis/Clusters/RedisCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RedisCluster : RedisBase, IRedisCluster, IDisposable
IList<IRedisNode> IRedisCluster.Nodes => Nodes.Select(x => (IRedisNode)x).ToList();

/// <summary>节点改变事件</summary>
public event EventHandler NodeChanged;
public event EventHandler? NodeChanged;

/// <summary>集群节点</summary>
public ClusterNode[]? Nodes { get; private set; }
Expand Down
34 changes: 20 additions & 14 deletions NewLife.Redis/RedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private Boolean OnCertificateValidationCallback(Object sender, X509Certificate c
/// <param name="cmd"></param>
/// <param name="args"></param>
/// <returns></returns>
protected virtual Int32 GetRequest(Memory<Byte> memory, String cmd, Object[]? args)
protected virtual Int32 GetRequest(Memory<Byte> memory, String cmd, Object?[]? args)
{
// *<number of arguments>\r\n$<number of bytes of argument 1>\r\n<argument data>\r\n
// *1\r\n$4\r\nINFO\r\n
Expand Down Expand Up @@ -200,12 +200,16 @@ protected virtual Int32 GetRequest(Memory<Byte> memory, String cmd, Object[]? ar
size = pk.Total;
else
{
pk = Host.Encoder.Encode(args[i]);
size = pk.Length;
var arg = args[i];
if (arg != null)
{
pk = Host.Encoder.Encode(arg);
size = pk.Length;
}
}

// 指令日志。简单类型显示原始值,复杂类型显示序列化后字符串
if (log != null && args != null)
if (log != null)
{
log.Append(' ');
if (str != null)
Expand All @@ -228,7 +232,7 @@ protected virtual Int32 GetRequest(Memory<Byte> memory, String cmd, Object[]? ar
writer.Write(str, -1);
else if (buf != null)
writer.Write(buf);
else
else if (pk != null)
writer.Write(pk.GetSpan());

writer.Write(_NewLine);
Expand Down Expand Up @@ -325,7 +329,7 @@ protected virtual Int32 GetRequest(Memory<Byte> memory, String cmd, Object[]? ar
/// <param name="args">参数数组</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
protected virtual async Task<Object?> ExecuteCommandAsync(String cmd, Object[]? args, CancellationToken cancellationToken)
protected virtual async Task<Object?> ExecuteCommandAsync(String cmd, Object?[]? args, CancellationToken cancellationToken)
{
var isQuit = cmd == "QUIT";

Expand Down Expand Up @@ -538,14 +542,16 @@ private static Int32 ReadLength(Stream ms)
#endif
}

private Int32 GetCommandSize(String cmd, Object[]? args)
private Int32 GetCommandSize(String cmd, Object?[]? args)
{
var total = 16 + cmd.Length;
if (args != null)
{
foreach (var item in args)
{
if (item is String str)
if (item == null)
total += 4;
else if (item is String str)
total += 16 + Encoding.UTF8.GetByteCount(str);
else if (item is Byte[] buf)
total += 16 + buf.Length;
Expand Down Expand Up @@ -595,7 +601,7 @@ private Int32 GetCommandSize(String cmd, Object[]? args)
/// <param name="args"></param>
/// <param name="value"></param>
/// <returns></returns>
public virtual Boolean TryExecute<TResult>(String cmd, Object[] args, out TResult? value)
public virtual Boolean TryExecute<TResult>(String cmd, Object?[] args, out TResult? value)
{
var rs = ExecuteAsync(cmd, args).Result;
if (rs is TResult rs2)
Expand All @@ -620,7 +626,7 @@ public virtual Boolean TryExecute<TResult>(String cmd, Object[] args, out TResul
/// <param name="args">参数数组</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public virtual async Task<Object?> ExecuteAsync(String cmd, Object[] args, CancellationToken cancellationToken = default)
public virtual async Task<Object?> ExecuteAsync(String cmd, Object?[] args, CancellationToken cancellationToken = default)
{
// 埋点名称,支持二级命令
var act = cmd.EqualIgnoreCase("cluster", "xinfo", "xgroup", "xreadgroup") ? $"{cmd}-{args?.FirstOrDefault()}" : cmd;
Expand All @@ -640,14 +646,14 @@ public virtual Boolean TryExecute<TResult>(String cmd, Object[] args, out TResul
/// <param name="cmd">命令</param>
/// <param name="args">参数数组</param>
/// <returns></returns>
public virtual async Task<TResult?> ExecuteAsync<TResult>(String cmd, params Object[] args) => await ExecuteAsync<TResult>(cmd, args, CancellationToken.None);
public virtual async Task<TResult?> ExecuteAsync<TResult>(String cmd, params Object?[] args) => await ExecuteAsync<TResult>(cmd, args, CancellationToken.None);

/// <summary>异步执行命令。返回基本类型、对象、对象数组</summary>
/// <param name="cmd">命令</param>
/// <param name="args">参数数组</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public virtual async Task<TResult?> ExecuteAsync<TResult>(String cmd, Object[] args, CancellationToken cancellationToken)
public virtual async Task<TResult?> ExecuteAsync<TResult>(String cmd, Object?[] args, CancellationToken cancellationToken)
{
// 管道模式
if (_ps != null)
Expand Down Expand Up @@ -817,10 +823,10 @@ public virtual Boolean TryChangeType(Object value, Type type, out Object? target
}
}

private class Command(String name, Object[] args, Type type)
private class Command(String name, Object?[] args, Type type)
{
public String Name { get; } = name;
public Object[] Args { get; } = args;
public Object?[] Args { get; } = args;
public Type Type { get; } = type;
}
#endregion
Expand Down
10 changes: 10 additions & 0 deletions NewLife.Redis/RedisEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ internal static IJsonHost GetJsonHost()
return (ArrayPacket)str.GetBytes();
}

/// <summary>解码数据包为目标类型</summary>
/// <param name="pk"></param>
/// <param name="type"></param>
/// <returns></returns>
public virtual Object? Decode(IPacket pk, Type type)
{
try
Expand Down Expand Up @@ -101,9 +105,15 @@ internal static IJsonHost GetJsonHost()
}
}

/// <summary>编解码助手</summary>
public static class RedisJsonEncoderHelper
{
//public static T Decode<T>(this RedisJsonEncoder encoder, Packet pk) => (T)encoder.Decode(pk, typeof(T))!;

/// <summary>解码数据包</summary>
/// <typeparam name="T"></typeparam>
/// <param name="encoder"></param>
/// <param name="pk"></param>
/// <returns></returns>
public static T Decode<T>(this IPacketEncoder encoder, IPacket pk) => (T)encoder.Decode(pk, typeof(T))!;
}
16 changes: 9 additions & 7 deletions NewLife.Redis/RedisHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
/// <returns></returns>
public Int32 HDel(params TKey[] fields)
{
var args = new List<Object>
var args = new List<Object?>
{
Key
};
Expand All @@ -115,9 +115,9 @@ public Int32 HDel(params TKey[] fields)
/// <summary>只在 key 指定的哈希集中不存在指定的字段时,设置字段的值</summary>
/// <param name="fields"></param>
/// <returns></returns>
public TValue[] HMGet(params TKey[] fields)
public TValue[]? HMGet(params TKey[] fields)
{
var args = new List<Object>
var args = new List<Object?>
{
Key
};
Expand All @@ -134,7 +134,7 @@ public TValue[] HMGet(params TKey[] fields)
/// <returns></returns>
public Boolean HMSet(IEnumerable<KeyValuePair<TKey, TValue>> keyValues)
{
var args = new List<Object>
var args = new List<Object?>
{
Key
};
Expand All @@ -151,9 +151,10 @@ public Boolean HMSet(IEnumerable<KeyValuePair<TKey, TValue>> keyValues)
/// <returns></returns>
public IDictionary<TKey, TValue> GetAll()
{
var dic = new Dictionary<TKey, TValue>();
var rs = Execute((r, k) => r.Execute<IPacket[]>("HGETALL", Key));
if (rs == null || rs.Length == 0) return dic;

var dic = new Dictionary<TKey, TValue>();
for (var i = 0; i < rs.Length; i++)
{
var pk = rs[i];
Expand Down Expand Up @@ -203,14 +204,15 @@ public virtual IEnumerable<KeyValuePair<TKey, TValue>> Search(SearchModel model)

model.Position = (rs[0] as IPacket)!.ToStr().ToInt();

var ps = rs[1] as Object[];
if (rs[1] is not Object[] ps) break;

for (var i = 0; i < ps.Length - 1; i += 2)
{
if (count-- > 0)
{
var key = (ps[i] as IPacket)!.ToStr().ChangeType<TKey>();
var val = (ps[i + 1] as IPacket)!.ToStr().ChangeType<TValue>();
yield return new KeyValuePair<TKey, TValue>(key, val);
if (key != null) yield return new KeyValuePair<TKey, TValue>(key, val!);
}
}

Expand Down
12 changes: 6 additions & 6 deletions NewLife.Redis/RedisList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public IEnumerator<T> GetEnumerator()
/// <returns>队列元素总数</returns>
public Int32 RPUSH(IEnumerable<T> values)
{
var args = new List<Object>
var args = new List<Object?>
{
Key
};
Expand All @@ -144,7 +144,7 @@ public Int32 RPUSH(IEnumerable<T> values)
/// <returns>队列元素总数</returns>
public Int32 LPUSH(IEnumerable<T> values)
{
var args = new List<Object>
var args = new List<Object?>
{
Key
};
Expand All @@ -157,19 +157,19 @@ public Int32 LPUSH(IEnumerable<T> values)

/// <summary>移除并返回最右边一个元素</summary>
/// <returns></returns>
public T RPOP() => Execute((rc, k) => rc.Execute<T>("RPOP", Key), true);
public T? RPOP() => Execute((rc, k) => rc.Execute<T>("RPOP", Key), true);

/// <summary>移除并返回最左边一个元素</summary>
/// <returns></returns>
public T LPOP() => Execute((rc, k) => rc.Execute<T>("LPOP", Key), true);
public T? LPOP() => Execute((rc, k) => rc.Execute<T>("LPOP", Key), true);

/// <summary>移除并返回最右边一个元素,并插入目标列表左边,原子操作</summary>
/// <remarks>
/// 用于高可靠性消费
/// </remarks>
/// <param name="destKey">目标列表</param>
/// <returns></returns>
public T RPOPLPUSH(String destKey) => Execute((rc, k) => rc.Execute<T>("RPOPLPUSH", Key, GetKey(destKey)), true);
public T? RPOPLPUSH(String destKey) => Execute((rc, k) => rc.Execute<T>("RPOPLPUSH", Key, GetKey(destKey)), true);

/// <summary>移除并返回最右边一个元素,并插入目标列表左边,原子操作</summary>
/// <remarks>
Expand All @@ -178,7 +178,7 @@ public Int32 LPUSH(IEnumerable<T> values)
/// <param name="destKey">目标列表</param>
/// <param name="timeout">超时时间,默认0秒永远阻塞;负数表示直接返回,不阻塞。</param>
/// <returns></returns>
public T BRPOPLPUSH(String destKey, Int32 timeout) => Execute((rc, k) => rc.Execute<T>("BRPOPLPUSH", Key, GetKey(destKey), timeout), true);
public T? BRPOPLPUSH(String destKey, Int32 timeout) => Execute((rc, k) => rc.Execute<T>("BRPOPLPUSH", Key, GetKey(destKey), timeout), true);

/// <summary>在指定元素之前插入</summary>
/// <param name="pivot"></param>
Expand Down

0 comments on commit 2921b5a

Please sign in to comment.