From d38011a409b34dc29bfa80c4248118fdf87af427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Fri, 28 Mar 2025 17:31:34 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9ELPOP=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81Redis7=E4=B8=8B=E7=9A=84=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=9F=A5=E6=89=BE=EF=BC=8CRedis7-=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E9=80=9A=E8=BF=87=E5=A4=9A=E6=AC=A1LRange=E9=81=8D?= =?UTF-8?q?=E5=8E=86=E5=AE=9E=E7=8E=B0=E3=80=82close:=20https://github.com?= =?UTF-8?q?/NewLifeX/NewLife.Redis/issues/137?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Redis/Queues/RedisReliableQueue.cs | 2 ++ NewLife.Redis/RedisList.cs | 25 +++++++++++++--- XUnitTest/ListTests.cs | 33 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/NewLife.Redis/Queues/RedisReliableQueue.cs b/NewLife.Redis/Queues/RedisReliableQueue.cs index 3f7ae51..460f635 100644 --- a/NewLife.Redis/Queues/RedisReliableQueue.cs +++ b/NewLife.Redis/Queues/RedisReliableQueue.cs @@ -442,6 +442,7 @@ public class RedisReliableQueue : QueueBase, IProducerConsumer, IDisposabl // 清理已经失去Status的Ack foreach (var key in rds.Search($"{_Key}:Ack:*", 1000)) + { if (!acks.Contains(key)) { var queue = rds.GetList(key) as RedisList; @@ -449,6 +450,7 @@ public class RedisReliableQueue : QueueBase, IProducerConsumer, IDisposabl XTrace.WriteLine("全局清理死信:{0} {1}", key, msgs.ToJson()); rds.Remove(key); } + } return count; } diff --git a/NewLife.Redis/RedisList.cs b/NewLife.Redis/RedisList.cs index 743fe2c..bd8cbf3 100644 --- a/NewLife.Redis/RedisList.cs +++ b/NewLife.Redis/RedisList.cs @@ -68,11 +68,28 @@ public class RedisList : RedisBase, IList // Redis7支持LPOS if (Redis.Version.Major >= 7) return LPOS(item); - var count = Count; - if (count > 1000) throw new NotSupportedException($"[{Key}]的元素个数过多,不支持!"); + var p = 0; + var batch = 100; + while (true) + { + var arr = LRange(p, p + batch - 1); + if (arr == null || arr.Length == 0) break; - var arr = GetAll(); - return Array.IndexOf(arr, item); + var idx = Array.IndexOf(arr, item); + if (idx >= 0) return p + idx; + + if (p >= 1_000_000) throw new NotSupportedException($"[{Key}]的元素个数过多,不支持遍历!"); + + p += batch; + } + + return -1; + + //var count = Count; + //if (count > 1000) + + //var arr = GetAll(); + //return Array.IndexOf(arr, item); } /// 在指定位置插入 diff --git a/XUnitTest/ListTests.cs b/XUnitTest/ListTests.cs index f1c45f3..b046560 100644 --- a/XUnitTest/ListTests.cs +++ b/XUnitTest/ListTests.cs @@ -1,5 +1,6 @@ using NewLife.Caching; using NewLife.Log; +using NewLife.Security; using System; using System.Diagnostics; using System.Linq; @@ -164,6 +165,38 @@ public class ListTests Assert.Equal(vs3[1], item2); } + [Fact] + public void List_IndexOf() + { + var key = "lkey_indexof"; + + // 删除已有 + _redis.Remove(key); + + var rlist = _redis.GetList(key) as RedisList; + Assert.NotNull(rlist); + + // 添加 + var vs = Enumerable.Range(0, 1000).Select(e => Rand.NextString(8)).ToArray(); + rlist.AddRange(vs); + _redis.SetExpire(key, TimeSpan.FromSeconds(60)); + + // 索引 + var idx = rlist.IndexOf(vs[1]); + Assert.Equal(1, idx); + + idx = rlist.IndexOf("abcd2"); + Assert.Equal(-1, idx); + + idx = rlist.IndexOf(vs[321]); + Assert.Equal(321, idx); + + var rs = rlist.Contains(vs[456]); + Assert.True(rs); + + _redis.Remove(key); + } + [Fact] public void RPOPLPUSH_Test() {