数据库
首页 > 数据库> > StackExchange.Redis 之 SortedSet 类型示例

StackExchange.Redis 之 SortedSet 类型示例

作者:互联网

 1,增加操作

            RedisCacheHelper.Instance.ZSortadd("zsortkey", "A1", 5);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "B1", 6);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "C1", 2);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "D1", 1);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "1", 9);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "2", 8);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "3", 7);
            RedisCacheHelper.Instance.ZSortadd("zsortkey", "4", 1);

            RedisCacheHelper.Instance.ZSortIncr("zsortkey", "D1", 2);   //增加2
            RedisCacheHelper.Instance.ZSortDecr("zsortkey", "A1", 1);   //减去1

            //获取长度
            var getlength = RedisCacheHelper.Instance.SortedSetLength("zsortkey");
            Console.WriteLine(getlength);

            //移除某个元素
            var getok = RedisCacheHelper.Instance.ZSortedRemove("zsortkey", "1");
            Console.WriteLine(getok);

            //获取榜单某个元素的排名
            var getsort = RedisCacheHelper.Instance.ZSortedRank("zsortkey", "2");
            Console.WriteLine(getsort);

            //获取有序集合中的某个元素的值
            //扩展的有 
            //SortedSetRangeByRank   根据索引获取member值,默认是升序,可以获取指定索引内的member值
            //SortedSetRangeByScore:根据score获取member值,默认是升序,可以获取指定score开始和结束的member值,后面的skip和take用于分页
            //SortedSetRangeByValue:根据member获取member值,默认是升序,可以获取指定member开始和结束的值,后面的skip和take用于分页
            //SortedSetRangeByRankWithScores:获取member和score值,可以只返回 start-stop 这个索引排序内的值(默认升序),后面的skip和take用于分页

            var getvalue = RedisCacheHelper.Instance.ZSortedScore("zsortkey", "4");
            Console.WriteLine(getvalue);

 

这里附上Helper类

#region SortSet类型  有序集合

        /// <summary>
        /// 增加,可以一次增加一个member,也可以一次增加多个member
        /// </summary>
        /// <param name="key"></param>
        /// <param name="member"></param>
        /// <param name="score"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public bool ZSortadd(string key, string member, double score, int db = -1)
        {
            try
            {
                var _db = GetDatabase(db);
                if ((object)key == null)
                    return false;
                else
                    return _db.SortedSetAdd(key, member, score);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 对Score值自增
        /// 如果不存在这member值,则执行增加member操作,并返回当前Score值
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="member">The member.</param>
        /// <param name="score">The score.</param>
        /// <param name="db">The database.</param>
        /// <returns></returns>
        public double ZSortIncr(string key, string member, double score, int db = -1)
        {
            try
            {
                var _db = GetDatabase(db);
                if ((object)key == null)
                    return 0;
                else
                    return _db.SortedSetIncrement(key, member, score);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 对Score值自减
        /// 如果不存在这member值,则执行增加member操作,并返回当前Score值
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="member">The member.</param>
        /// <param name="score">The score.</param>
        /// <param name="db">The database.</param>
        /// <returns></returns>
        public double ZSortDecr(string key, string member, double score, int db = -1)
        {
            try
            {
                var _db = GetDatabase(db);
                if ((object)key == null)
                    return 0;
                else
                    return _db.SortedSetDecrement(key, member, score);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 长度
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public long SortedSetLength(string redisKey, int db = -1)
        {
            var _db = GetDatabase(db);
            return _db.SortedSetLength(redisKey);
        }

        /// <summary>
        /// 移除某个元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="memebr"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public bool ZSortedRemove(string key, string memebr, int db = -1)
        {
            try
            {
                var _db = GetDatabase(db);
                if ((object)key == null)
                    return false;
                else
                    return _db.SortedSetRemove(key, memebr);

            }
            catch (Exception)
            {
                return false;
            }
        }


        /// <summary>
        /// 获取榜单某个元素的排名、排行
        /// </summary>
        /// <param name="key"></param>
        /// <param name="mumber"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public double ZSortedRank(string key, string mumber, int db = -1)
        {
            try
            {
                var _db = GetDatabase(db);
                if ((object)key == null)
                    return 0;
                else
                    return _db.SortedSetRank(key, mumber) ?? 0;

            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取有序集合中的某个元素的值
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="member">The member.</param>
        /// <param name="db">The database.</param>
        /// <returns></returns>
        public double ZSortedScore(string key, string member, int db = -1)
        {
            try
            {
                var _db = GetDatabase(db);
                if ((object)key == null)
                    return 0;
                else
                    return Convert.ToDouble(_db.SortedSetScore(key, member));
            }
            catch (Exception ex)
            {
                throw;
            }
        }


        #endregion
View Code

 

标签:member,return,示例,db,zsortkey,SortedSet,Instance,key,StackExchange
来源: https://www.cnblogs.com/peterzhang123/p/12346499.html