zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    BZOJ-3211花神游历各国 并查集+树状数组
    HDU-1754I Hate It 线段树区间最值
    POJ-2777Count Color 线段树+位移
    BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值
    HDU-1394 Minimum Inversion Number 线段树+逆序对
    HDU-1698 JUST A HOOK 线段树
    学习笔记 --- 线段树
    poj 2155 Matrix---树状数组套树状数组
    hdu 1166 敌兵布阵--BIT
    学习笔记 BIT(树状数组)
  • 原文地址:https://www.cnblogs.com/yyjspace/p/11613401.html
Copyright © 2011-2022 走看看