From f38dd4e1948c1c93fb4a0b6dc1ca96e3e582db2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B7=E4=B8=BD=E6=96=87?= Date: Mon, 22 Mar 2021 14:25:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EZRANDMEMBER=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FreeRedis/FreeRedis.xml | 20 ++++++++++++++++++++ src/FreeRedis/RedisClient/SortedSets.cs | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/FreeRedis/FreeRedis.xml b/src/FreeRedis/FreeRedis.xml index c27b3f42..b4c16619 100644 --- a/src/FreeRedis/FreeRedis.xml +++ b/src/FreeRedis/FreeRedis.xml @@ -1109,6 +1109,26 @@ 成功/失败 + + + 随机返回N个元素 + Redis 6.2.0+以上才支持该命令 + + Key + 返回的个数 + 是否允许有重复元素返回 + + + + + 随机返回N个元素, 包含分数 + Redis 6.2.0+以上才支持该命令 + + Key + 返回的个数 + 是否允许有重复元素返回 + + APPEND command (A Synchronized Version)

diff --git a/src/FreeRedis/RedisClient/SortedSets.cs b/src/FreeRedis/RedisClient/SortedSets.cs index 87b4fe5e..261ce749 100644 --- a/src/FreeRedis/RedisClient/SortedSets.cs +++ b/src/FreeRedis/RedisClient/SortedSets.cs @@ -268,6 +268,29 @@ long ZStore(bool inter, string destination, string[] keys, int[] weights, ZAggre .InputIf(weights?.Any() == true, weights) .InputIf(aggregate != null, "AGGREGATE", aggregate ?? ZAggregate.max), rt => rt .ThrowOrValue()); + + /// + /// 随机返回N个元素 + /// Redis 6.2.0+以上才支持该命令 + /// + /// Key + /// 返回的个数 + /// 是否允许有重复元素返回 + /// + public string[] ZRandMember(string key, int count, bool repetition) => Call("ZRANDMEMBER".InputKey(key, repetition ? -count : count), rt => rt.ThrowOrValue()); + + /// + /// 随机返回N个元素, 包含分数 + /// Redis 6.2.0+以上才支持该命令 + /// + /// Key + /// 返回的个数 + /// 是否允许有重复元素返回 + /// + public ZMember[] ZRandMemberWithScores(string key, int count, bool repetition) => Call("ZRANDMEMBER" + .InputKey(key, repetition ? -count : count) + .Input("WITHSCORES"), rt => rt + .ThrowOrValue((a, _) => a == null || a.Length == 0 ? new ZMember[0] : a.MapToHash(rt.Encoding).Select(b => new ZMember(b.Key, b.Value)).ToArray())); } public class ZMember