• Redis SortedSet有序集合


    #region SortedSet

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <param name="expireTime">超时时间</param>
    /// <returns></returns>
    public static bool SortedSetAdd(string key, object value, double score, DateTime? expireTime = null) {
    string json = JsonConvert.SerializeObject(value);
    var result = SortedSetAdd(key, json, score, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));
    return result;
    }

    public static long SortedSetAdd<T>(string key, List<T> list, Func<T, dynamic> getSorts, DateTime? expireTime = null) {
    long result = 0;
    try {
    List<SortedSetEntry> listSortedSet = new List<SortedSetEntry>();
    foreach (var item in list) {
    listSortedSet.Add(new SortedSetEntry(JsonConvert.SerializeObject(item), double.Parse(getSorts(item))));
    }
    result = _db.SortedSetAdd(key, listSortedSet.ToArray());
    _db.KeyExpire(key, expireTime.HasValue ? expireTime : DateTime.Now.AddMinutes(timeOut));
    return result;
    }
    catch (Exception) {

    throw;
    }
    }
    /// <summary>
    /// 获取Sorted所有的值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public static List<T> SortedGetAll<T>(string key) {
    try {
    List<T> result = new List<T>();
    SortedSetEntry[] arr = _db.SortedSetRangeByScoreWithScores(key);
    foreach (var item in arr) {
    if (item.Element.HasValue) {
    string element = item.Element.ToString();
    result.Add(JsonConvert.DeserializeObject<T>(element));
    }
    }
    return result;
    }
    catch (Exception ex) {
    throw;
    }
    }
    public static bool SetRemove(string key, double start, double stop) {
    var flag = _db.SortedSetRemoveRangeByScore(key, start, stop).ToString();
    if (flag == "the number of elements removed") {
    return true;
    }
    return false;
    }
    /// <summary>
    /// 返回SortedSet,默认情况下从低到高。
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="start"></param>
    /// <param name="stop"></param>
    /// <param name="order"></param>
    /// <returns></returns>
    public static List<T> SortedSetRangeByRank<T>(string key, long start = 0, long stop = -1, Order order = Order.Ascending) {
    try {
    var result = new List<T>();
    RedisValue[] value = _db.SortedSetRangeByRank(key, start, stop, order);
    foreach (var item in value) {
    if (!item.IsNullOrEmpty) {
    result.Add(JsonConvert.DeserializeObject<T>(item));
    }
    }
    return result;
    }
    catch (Exception) {
    throw;
    }
    }
    /// <summary>
    /// 返回返回SortedSet元素个数
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static long SortedSetLength(string key) {
    return _db.SortedSetLength(key);
    }

    /// <summary>
    /// 移除SortedSet元素
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool SortedSetRemove(string key, string value) {
    return _db.SortedSetRemove(key, value);
    }

    /// <summary>
    /// 根据范围移除SortedSet
    /// </summary>
    /// <param name="key"></param>
    /// <param name="start"></param>
    /// <param name="stop"></param>
    /// <returns></returns>
    public static bool SortedSetRemoveRangeByScore(string key, double start, double stop) {
    var flag = _db.SortedSetRemoveRangeByScore(key, start, stop).ToString();
    if (flag == "the number of elements removed")
    return true;
    return false;
    }

    #endregion

  • 相关阅读:
    arduino链接GY521(MPU6050)模块
    I2C Python Library ITG3205 API
    [翻译]AxureInteractive Prototypes原型设计工具Axure学习第2.3节
    [Java]XML数据的请求和DOM技术解析
    [Linux]VI相关操作
    mysql数据库连接错误问题
    关于管理单元初始化失败的解决方法
    彻底明白Java的IO系统(网上找的,还没看,先放这)
    一个关于C++ Inline关键字的引发的一个错误
    orcale 中日期类型相加的处理
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11613401.html
Copyright © 2020-2023  润新知