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() {