• 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

  • 相关阅读:
    enter事件处方方法
    vue 关于父组件无法触发子组件的事件的解决方法
    iview表格高度自适应只需要三步即可
    子组件触发父组件的方法
    组件之间的方法
    年与日时分秒各种格式 补0位的方法
    //定义一个函数,删除首个数组元素
    input标签内容改变的触发事件
    jQuery表单校验jquery.validate.js的使用
    jQuery BlockUI 实现锁屏
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11613401.html
Copyright © 2020-2023  润新知