diff --git a/NewLife.Redis/Queues/RedisQueue.cs b/NewLife.Redis/Queues/RedisQueue.cs index 2725dd1..4735a37 100644 --- a/NewLife.Redis/Queues/RedisQueue.cs +++ b/NewLife.Redis/Queues/RedisQueue.cs @@ -112,7 +112,13 @@ public class RedisQueue : QueueBase, IProducerConsumer if (timeout > 0 && Redis.Timeout < (timeout + 1) * 1000) Redis.Timeout = (timeout + 1) * 1000; var rs = Execute((rc, k) => rc.Execute("BRPOP", Key, timeout), true); - return rs == null || rs.Length < 2 ? default : (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + if (rs == null || rs.Length < 2) return default; + + var msg = (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + + if (typeof(T) != typeof(IPacket)) rs.TryDispose(); + + return msg; } /// 异步消费获取 @@ -126,7 +132,13 @@ public class RedisQueue : QueueBase, IProducerConsumer if (timeout > 0 && Redis.Timeout < (timeout + 1) * 1000) Redis.Timeout = (timeout + 1) * 1000; var rs = await ExecuteAsync((rc, k) => rc.ExecuteAsync("BRPOP", [Key, timeout], cancellationToken), true).ConfigureAwait(false); - return rs == null || rs.Length < 2 ? default : (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + if (rs == null || rs.Length < 2) return default; + + var msg = (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + + if (typeof(T) != typeof(IPacket)) rs.TryDispose(); + + return msg; } /// 异步消费获取 diff --git a/NewLife.Redis/RedisClient.cs b/NewLife.Redis/RedisClient.cs index a9dfb3f..396dd2e 100644 --- a/NewLife.Redis/RedisClient.cs +++ b/NewLife.Redis/RedisClient.cs @@ -678,20 +678,22 @@ public class RedisClient : DisposeBase try { // 管道模式 + var type = typeof(TResult); if (_ps != null) { - _ps.Add(new Command(cmd, args, typeof(TResult))); + _ps.Add(new Command(cmd, args, type)); return default; } var rs = ExecuteCommand(cmd, args); if (rs == null) return default; if (rs is TResult rs2) return rs2; - if (TryChangeType(rs, typeof(TResult), out var target)) + + if (TryChangeType(rs, type, out var target)) { //!!! 外部调用者可能需要直接使用内部申请的OwnerPacket,所以这里不释放 - //// 释放内部申请的OwnerPacket - //rs.TryDispose(); + // 释放内部申请的OwnerPacket + if (type != typeof(IPacket) && type != typeof(IPacket[])) rs.TryDispose(); return (TResult?)target; } @@ -725,11 +727,13 @@ public class RedisClient : DisposeBase value = default; if (rs == null) return false; - if (TryChangeType(rs, typeof(TResult), out var target)) + + var type = typeof(TResult); + if (TryChangeType(rs, type, out var target)) { //!!! 外部调用者可能需要直接使用内部申请的OwnerPacket,所以这里不释放 - //// 释放内部申请的OwnerPacket - //rs.TryDispose(); + // 释放内部申请的OwnerPacket + if (type != typeof(IPacket) && type != typeof(IPacket[])) rs.TryDispose(); value = (TResult?)target; return true; } @@ -778,20 +782,22 @@ public class RedisClient : DisposeBase public virtual async Task ExecuteAsync(String cmd, Object?[] args, CancellationToken cancellationToken) { // 管道模式 + var type = typeof(TResult); if (_ps != null) { - _ps.Add(new Command(cmd, args, typeof(TResult))); + _ps.Add(new Command(cmd, args, type)); return default; } var rs = await ExecuteAsync(cmd, args, cancellationToken).ConfigureAwait(false); if (rs == null) return default; if (rs is TResult rs2) return rs2; - if (TryChangeType(rs, typeof(TResult), out var target)) + + if (TryChangeType(rs, type, out var target)) { //!!! 外部调用者可能需要直接使用内部申请的OwnerPacket,所以这里不释放 - //// 释放内部申请的OwnerPacket - //rs.TryDispose(); + // 释放内部申请的OwnerPacket + if (type != typeof(IPacket) && type != typeof(IPacket[])) rs.TryDispose(); return (TResult?)target; } @@ -812,7 +818,14 @@ public class RedisClient : DisposeBase //var rs = ExecuteCommand(null, null, null); if (rs == null) return default; if (rs is TResult rs2) return rs2; - if (TryChangeType(rs, typeof(TResult), out var target)) return (TResult?)target; + + var type = typeof(TResult); + if (TryChangeType(rs, type, out var target)) + { + // 释放内部申请的OwnerPacket + if (type != typeof(IPacket) && type != typeof(IPacket[])) rs.TryDispose(); + return (TResult?)target; + } return default; } @@ -942,8 +955,8 @@ public class RedisClient : DisposeBase if (rs != null && TryChangeType(rs, ps[i].Type, out var target) && target != null) { //!!! 外部调用者可能需要直接使用内部申请的OwnerPacket,所以这里不释放 - //// 释放内部申请的OwnerPacket - //rs.TryDispose(); + // 释放内部申请的OwnerPacket + if (ps[i].Type != typeof(IPacket) && ps[i].Type != typeof(IPacket[])) rs.TryDispose(); list[i] = target; } } diff --git a/NewLife.Redis/RedisHash.cs b/NewLife.Redis/RedisHash.cs index 0ff6b8e..14f531e 100644 --- a/NewLife.Redis/RedisHash.cs +++ b/NewLife.Redis/RedisHash.cs @@ -68,6 +68,8 @@ public class RedisHash : RedisBase, IDictionary value = Redis.Encoder.Decode(pk)!; //value = (TValue?)Redis.Encoder.Decode(pk, typeof(TValue))!; + if (typeof(TValue) != typeof(IPacket)) pk.TryDispose(); + return true; } @@ -164,6 +166,8 @@ public class RedisHash : RedisBase, IDictionary dic[key] = value; } + if (typeof(TKey) != typeof(IPacket) && typeof(TValue) != typeof(IPacket)) rs.TryDispose(); + return dic; } diff --git a/NewLife.Redis/RedisStack.cs b/NewLife.Redis/RedisStack.cs index d69fda2..26de795 100644 --- a/NewLife.Redis/RedisStack.cs +++ b/NewLife.Redis/RedisStack.cs @@ -83,7 +83,13 @@ public class RedisStack : RedisBase, IProducerConsumer if (timeout < 0) return Execute((rc, k) => rc.Execute("RPOP", Key), true); var rs = Execute((rc, k) => rc.Execute("BRPOP", Key, timeout), true); - return rs == null || rs.Length < 2 ? default : (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + if (rs == null || rs.Length < 2) return default; + + var msg = (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + + if (typeof(T) != typeof(IPacket)) rs.TryDispose(); + + return msg; } /// 异步消费获取 @@ -95,7 +101,13 @@ public class RedisStack : RedisBase, IProducerConsumer if (timeout < 0) return await ExecuteAsync((rc, k) => rc.ExecuteAsync("RPOP", Key), true).ConfigureAwait(false); var rs = await ExecuteAsync((rc, k) => rc.ExecuteAsync("BRPOP", [Key, timeout], cancellationToken), true).ConfigureAwait(false); - return rs == null || rs.Length < 2 ? default : (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + if (rs == null || rs.Length < 2) return default; + + var msg = (T?)Redis.Encoder.Decode(rs[1], typeof(T)); + + if (typeof(T) != typeof(IPacket)) rs.TryDispose(); + + return msg; } /// 异步消费获取 diff --git a/NewLife.Redis/Services/RedisStat.cs b/NewLife.Redis/Services/RedisStat.cs index e575566..0812a77 100644 --- a/NewLife.Redis/Services/RedisStat.cs +++ b/NewLife.Redis/Services/RedisStat.cs @@ -90,7 +90,7 @@ public class RedisStat : DisposeBase if (!_redis.Rename(key, newKey, false)) return; _redis.Remove($"exists:{key}"); - var rs = _redis.Execute(newKey, (r,k) => r.Execute("HGETALL", k)); + var rs = _redis.Execute(newKey, (r, k) => r.Execute("HGETALL", k)); if (rs != null) { var dic = new Dictionary(); @@ -104,6 +104,8 @@ public class RedisStat : DisposeBase OnSave(key, dic); } + rs.TryDispose(); + _redis.Remove(newKey); } #endregion