Skip to content

Commit

Permalink
Merge pull request #53 from q7164518/master
Browse files Browse the repository at this point in the history
新增ZRANDMEMBER方法
  • Loading branch information
2881099 authored Mar 22, 2021
2 parents 159aef4 + f38dd4e commit 9b29e4a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FreeRedis/FreeRedis.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions src/FreeRedis/RedisClient/SortedSets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<long>());

/// <summary>
/// 随机返回N个元素
/// <para>Redis 6.2.0+以上才支持该命令</para>
/// </summary>
/// <param name="key">Key</param>
/// <param name="count">返回的个数</param>
/// <param name="repetition">是否允许有重复元素返回</param>
/// <returns></returns>
public string[] ZRandMember(string key, int count, bool repetition) => Call("ZRANDMEMBER".InputKey(key, repetition ? -count : count), rt => rt.ThrowOrValue<string[]>());

/// <summary>
/// 随机返回N个元素, 包含分数
/// <para>Redis 6.2.0+以上才支持该命令</para>
/// </summary>
/// <param name="key">Key</param>
/// <param name="count">返回的个数</param>
/// <param name="repetition">是否允许有重复元素返回</param>
/// <returns></returns>
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<decimal>(rt.Encoding).Select(b => new ZMember(b.Key, b.Value)).ToArray()));
}

public class ZMember
Expand Down

0 comments on commit 9b29e4a

Please sign in to comment.