[fix] GetAll返回空处理,出现这种情况,更可能是协议底层返回数据异常。fix: https://github.com/NewLifeX/NewLife.Redis/issues/154

This commit is contained in:
智能大石头 2025-05-02 09:23:24 +08:00
parent 59c7e3ea79
commit 44bb1203ac
1 changed files with 3 additions and 3 deletions

View File

@ -151,9 +151,9 @@ public class RedisHash<TKey, TValue> : RedisBase, IDictionary<TKey, TValue>
/// <summary>获取所有名值对</summary> /// <summary>获取所有名值对</summary>
/// <returns></returns> /// <returns></returns>
public IDictionary<TKey, TValue> GetAll() public IDictionary<TKey, TValue?> GetAll()
{ {
var dic = new Dictionary<TKey, TValue>(); var dic = new Dictionary<TKey, TValue?>();
var rs = Execute((r, k) => r.Execute<IPacket[]>("HGETALL", Key)); var rs = Execute((r, k) => r.Execute<IPacket[]>("HGETALL", Key));
if (rs == null || rs.Length == 0) return dic; if (rs == null || rs.Length == 0) return dic;
@ -163,7 +163,7 @@ public class RedisHash<TKey, TValue> : RedisBase, IDictionary<TKey, TValue>
var pk2 = rs[++i]; var pk2 = rs[++i];
var key = Redis.Encoder.Decode<TKey>(pk); var key = Redis.Encoder.Decode<TKey>(pk);
var value = Redis.Encoder.Decode<TValue>(pk2); var value = Redis.Encoder.Decode<TValue>(pk2);
dic[key] = value; if (key != null) dic[key] = value;
} }
if (typeof(TKey) != typeof(IPacket) && typeof(TValue) != typeof(IPacket)) rs.TryDispose(); if (typeof(TKey) != typeof(IPacket) && typeof(TValue) != typeof(IPacket)) rs.TryDispose();