From d1a624ca4fe43d3f403595db24a0d14aa2fc0e45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sun, 29 Jun 2025 12:34:19 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=AD=A3ZADD=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=B6=85=E5=A4=A7Int64=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E4=B8=BB=E8=A6=81=E5=8E=9F?= =?UTF-8?q?=E5=9B=A0=E6=98=AF=E5=AE=83=E8=BD=AC=E4=B8=BADouble=E6=97=B6?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BA=86=E7=A7=91=E5=AD=A6=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E8=AF=BB=E5=8F=96=E8=A7=A3=E6=9E=90=E6=97=B6?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=94=AF=E6=8C=81=E3=80=82fix=20https://gith?= =?UTF-8?q?ub.com/NewLifeX/NewLife.Redis/issues/152?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Redis/NewLife.Redis.csproj | 2 +- NewLife.Redis/RedisSortedSet.cs | 22 ++++++++++++++++++++++ Samples/Benchmark/Benchmark.csproj | 2 +- Test/Test.csproj | 2 +- XUnitTest/SortedSetTests.cs | 26 ++++++++++++++++++++++++++ XUnitTest/XUnitTest.csproj | 6 +++--- 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/NewLife.Redis/NewLife.Redis.csproj b/NewLife.Redis/NewLife.Redis.csproj index 75b932f..8dc5648 100644 --- a/NewLife.Redis/NewLife.Redis.csproj +++ b/NewLife.Redis/NewLife.Redis.csproj @@ -56,7 +56,7 @@ - + diff --git a/NewLife.Redis/RedisSortedSet.cs b/NewLife.Redis/RedisSortedSet.cs index 673966e..9360f38 100644 --- a/NewLife.Redis/RedisSortedSet.cs +++ b/NewLife.Redis/RedisSortedSet.cs @@ -25,6 +25,12 @@ public class RedisSortedSet : RedisBase /// 添加到有序集合的成员数量,不包括已经存在更新分数的成员 public Int32 Add(T member, Double score) => Execute((rc, k) => rc.Execute("ZADD", Key, score, member), true).ToInt(-1); + /// 添加元素并指定分数,返回添加到集合的成员数量 + /// 元素 + /// 分数 + /// 添加到有序集合的成员数量,不包括已经存在更新分数的成员 + public Int32 Add(T member, Int64 score) => Execute((rc, k) => rc.Execute("ZADD", Key, score, member), true).ToInt(-1); + /// 批量添加,返回添加到集合的成员数量 /// /// @@ -41,6 +47,22 @@ public class RedisSortedSet : RedisBase return Execute((rc, k) => rc.Execute("ZADD", args.ToArray()), true).ToInt(-1); } + /// 批量添加,返回添加到集合的成员数量 + /// + /// + /// 添加到有序集合的成员数量,不包括已经存在更新分数的成员 + public Int32 Add(IEnumerable members, Int64 score) + { + var args = new List { Key }; + + foreach (var item in members) + { + args.Add(score); + args.Add(item!); + } + return Execute((rc, k) => rc.Execute("ZADD", args.ToArray()), true).ToInt(-1); + } + /// 删除元素 /// /// diff --git a/Samples/Benchmark/Benchmark.csproj b/Samples/Benchmark/Benchmark.csproj index 69810ea..82e6674 100644 --- a/Samples/Benchmark/Benchmark.csproj +++ b/Samples/Benchmark/Benchmark.csproj @@ -24,7 +24,7 @@ - + diff --git a/Test/Test.csproj b/Test/Test.csproj index c89e947..5a5f202 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ - + diff --git a/XUnitTest/SortedSetTests.cs b/XUnitTest/SortedSetTests.cs index 7ba7d52..71843a8 100644 --- a/XUnitTest/SortedSetTests.cs +++ b/XUnitTest/SortedSetTests.cs @@ -190,6 +190,32 @@ public class SortedSetTests Assert.Equal(33.44, zset.GetScore("stone")); } + [Fact] + public void Add_xx2() + { + var rkey = "zset_add_xx2"; + + // 删除已有 + _redis.Remove(rkey); + + var zset = new RedisSortedSet(_redis, rkey); + + // 插入数据 + var ticks = DateTime.Now.Ticks; + zset.Add("stone", ticks); + zset.Add("stone1", ticks); + Assert.Equal(2, zset.Count); + + var r = zset.GetScore("stone"); + var r1 = zset.GetScore("stone2"); + var r2 = zset.Execute((r, k) => r.Execute("ZSCORE", zset.Key, "stone"), false); + + Assert.Equal(ticks, r); + Assert.Equal(0, r1); + Assert.NotEmpty(r2); + Assert.NotEqual("0", r2); + } + [Fact] public void Add_nx() { diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj index 48ded6e..c9c0acb 100644 --- a/XUnitTest/XUnitTest.csproj +++ b/XUnitTest/XUnitTest.csproj @@ -9,11 +9,11 @@ - - + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive