You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
12 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RedisStudyModel;
using StackExchange.Redis;
namespace RedisStuy
{
/// <summary>
/// 有序集合操作
/// </summary>
public class RedisSortedSetStudy
{
#region 初始化
private IDatabase redisDatabase = RedisHelper.GetRedisDatabase();
#endregion
/// <summary>
/// 将一个 member 元素及其 score 值加入到有序集 key 当中
/// 如果这个 member 已经是有序集的成员,那么更新这个 member 的 score 值,
/// 并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。
/// </summary>
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, CommandFlags flags)
{
return redisDatabase.SortedSetAdd(key, member, score, flags);
}
/// <summary>
/// 将一个 member 元素及其 score 值加入到有序集 key 当中
/// 如果这个 member 已经是有序集的成员,那么更新这个 member 的 score 值,
/// 并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。
/// </summary>
public bool SortedSetAdd(RedisKey key, RedisValue member, double score, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetAdd(key, member, score, when, flags);
}
/// <summary>
/// 将一组 SortedSetEntry 加入到有序集 key 当中
/// 如果某个 SortedSetEntry 已经是有序集的成员,那么更新这个 member 的 score 值,
/// 并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。
/// </summary>
/// <returns>添加到排序集合中的元素的数量,不包括已被更新的元素。</returns>
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, CommandFlags flags)
{
return redisDatabase.SortedSetAdd(key, values,flags);
}
/// <summary>
/// 将一组 SortedSetEntry 加入到有序集 key 当中
/// 如果某个 SortedSetEntry 已经是有序集的成员,那么更新这个 member 的 score 值,
/// 并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。
/// </summary>
/// <returns>添加到排序集合中的元素的数量,不包括已被更新的元素。</returns>
public long SortedSetAdd(RedisKey key, SortedSetEntry[] values, When when = When.Always, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetAdd(key, values,when, flags);
}
/// <summary>
/// 计算两个排序集合的集合操作,并将结果存储在目的地中,可选地执行特定聚合(默认为求和)
/// </summary>
public long SortedSetCombineAndStore(SetOperation operation, RedisKey destination, RedisKey first, RedisKey second, Aggregate aggregate = Aggregate.Sum, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetCombineAndStore(operation, destination, first, second, aggregate, flags);
}
/// <summary>
/// 计算多个排序集(可选地使用每个集合权重)的集合操作,并将结果存储在目的地中,可选地执行特定聚合(默认为和)。
/// </summary>
public long SortedSetCombineAndStore(SetOperation operation, RedisKey destination, RedisKey[] keys, double[] weights = null, Aggregate aggregate = Aggregate.Sum, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetCombineAndStore(operation, destination, keys, weights,aggregate,flags);
}
/// <summary>
/// 通过递减来减少存储在键中的排序集合中成员的分数。
/// 如果成员不存在于排序的集合中则以减量添加作为其得分如其先前得分为0
/// </summary>
public double SortedSetDecrement(RedisKey key, RedisValue member, double value, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetDecrement(key, member, value, flags);
}
/// <summary>
/// 按增量存储按键存储的排序集合中成员的分数。
/// 如果成员不存在于排序的集合中则以增量作为其得分添加如其先前的得分为0.0
/// </summary>
public double SortedSetIncrement(RedisKey key, RedisValue member, double value, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetIncrement(key, member, value, flags);
}
/// <summary>
/// 获取指定条件的元素个数
/// </summary>
/// <param name="key">键</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <param name="exclude">在执行范围查询时排他性设置默认情况下Start/Stop 限制是包含的;但是,两者也可以单独指定为排他性的。</param>
/// <param name="flags">命令参数</param>
/// <returns>
/// 指定条件的元素个数
/// </returns>
public long SortedSetLength(RedisKey key, double min = double.NegativeInfinity, double max = double.PositiveInfinity, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetLength(key, min, max, exclude, flags);
}
/// <summary>
/// 当排序的集合中的所有元素都以相同的分数插入时为了强制词典编排该命令返回在排序的集合中的元素的数量其中的值为min和max之间的值。
/// </summary>
public long SortedSetLengthByValue(RedisKey key, RedisValue min, RedisValue max, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetLengthByValue(key, min, max, exclude, flags);
}
/// <summary>
/// 通过分数返回有序集合指定区间内的成员
/// 默认从最低到最高得分排序,字典顺序用于等分元素。
/// 启动和停止都是基于零的索引其中0是第一个元素1是下一个元素等等。
/// 负数时 -1是最后一个元素-2是倒数第二元素类推。
/// </summary>
public RedisValue[] SortedSetRangeByRank(RedisKey key, long start = 0, long stop = -1, Order order = Order.Ascending, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRangeByRank(key, start, stop, order, flags);
}
/// <summary>
/// 同SortedSetRangeByRank 只是返回值更详细
/// </summary>
public SortedSetEntry[] SortedSetRangeByRankWithScores(RedisKey key, long start = 0, long stop = -1, Order order = Order.Ascending, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRangeByRankWithScores(key, start, stop, order, flags);
}
/// <summary>
/// 通过分数返回有序集合指定区间内的成员
/// </summary>
public RedisValue[] SortedSetRangeByScore(RedisKey key, double start = double.NegativeInfinity, double stop = double.PositiveInfinity, Exclude exclude = Exclude.None, Order order = Order.Ascending, long skip = 0, long take = -1, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRangeByScore(key, start, stop, exclude, order, skip, take, flags);
}
/// <summary>
/// 同SortedSetRangeByScore返回值为SortedSetEntry[]
/// </summary>
public SortedSetEntry[] SortedSetRangeByScoreWithScores(RedisKey key, double start = double.NegativeInfinity, double stop = double.PositiveInfinity, Exclude exclude = Exclude.None, Order order = Order.Ascending, long skip = 0, long take = -1, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRangeByScoreWithScores(key, start, stop, exclude, order, skip, take, flags);
}
/// <summary>
/// 通过字典区间返回有序集合的成
/// </summary>
public RedisValue[] SortedSetRangeByValue(RedisKey key, RedisValue min = default(RedisValue), RedisValue max = default(RedisValue), Exclude exclude = Exclude.None, long skip = 0, long take = -1, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRangeByValue(key, min, max, exclude, skip, take, flags);
}
/// <summary>
/// 返回有序集合中指定成员的索引
/// </summary>
public long? SortedSetRank(RedisKey key, RedisValue member, Order order = Order.Ascending, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRank(key, member, order, flags);
}
/// <summary>
/// 移除有序集合中的一个成员
/// </summary>
public bool SortedSetRemove(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRemove(key, member, flags);
}
/// <summary>
/// 移除有序集合中的多个成员
/// </summary>
public long SortedSetRemove(RedisKey key, RedisValue[] members, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRemove(key, members, flags);
}
/// <summary>
/// 移除有序集合中给定的排名区间的所有成员
/// </summary>
public long SortedSetRemoveRangeByRank(RedisKey key, long start, long stop, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRemoveRangeByRank(key, start, stop, flags);
}
/// <summary>
/// 移除有序集合中给定的分数区间的所有成员
/// </summary>
public long SortedSetRemoveRangeByScore(RedisKey key, double start, double stop, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRemoveRangeByScore(key, start, stop, exclude, flags);
}
/// <summary>
/// 移除有序集合中给定的字典区间的所有成员
/// </summary>
public long SortedSetRemoveRangeByValue(RedisKey key, RedisValue min, RedisValue max, Exclude exclude = Exclude.None, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetRemoveRangeByValue(key, min ,max ,exclude, flags);
}
/// <summary>
/// 迭代有序集合中的元素(包括元素成员和元素分值)
/// </summary>
public IEnumerable<SortedSetEntry> SortedSetScan(RedisKey key, RedisValue pattern, int pageSize, CommandFlags flags)
{
return redisDatabase.SortedSetScan(key, pattern, pageSize, flags);
}
/// <summary>
/// 迭代有序集合中的元素(包括元素成员和元素分值)
/// </summary>
public IEnumerable<SortedSetEntry> SortedSetScan(RedisKey key, RedisValue pattern = default(RedisValue), int pageSize = 10, long cursor = 0, int pageOffset = 0, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetScan(key, pattern, pageSize, cursor, pageOffset, flags);
}
/// <summary>
/// 返回有序集中,成员的分数值
/// </summary>
public double? SortedSetScore(RedisKey key, RedisValue member, CommandFlags flags = CommandFlags.None)
{
return redisDatabase.SortedSetScore(key, member, flags);
}
}
}