The exemplary type SortedSet StackExchange.Redis

Original: StackExchange.Redis exemplary type of SortedSet

 1, increase operational

Copy the code
            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);
Copy the code

            RedisCacheHelper.Instance.ZSortIncr ( " zsortkey " , " Dl " , 2 );    // increase 2 
            RedisCacheHelper.Instance.ZSortDecr ( " zsortkey " , " A1 " , 1 );    // subtract 1

Copy the code
            // Get length 
            var getLength = RedisCacheHelper.Instance.SortedSetLength ( " zsortkey " ); 
            Console.WriteLine (getLength); 

            // remove an element 
            var getOK = RedisCacheHelper.Instance.ZSortedRemove ( " zsortkey " , " . 1 " ); 
            Console.WriteLine (getOK); 

            // get a ranked list of elements 
            var getSort = RedisCacheHelper.Instance.ZSortedRank ( " zsortkey " , " 2 " ); 
            Console.WriteLine (getSort); 

            //Gets the value of an ordered set of elements
             // extensions have 
             // SortedSetRangeByRank acquisition member according to the index value, the default is ascending order, you can get member values within the specified index
             // SortedSetRangeByScore: Score acquisition member according to the value, the default is ascending may be obtained score specify the start and end of the member, and take back the skip for paging
             // SortedSetRangeByValue: acquisition member according to the value member, the default is ascending, can obtain the value of the specified start and end member, and take back the skip by with a page
             // SortedSetRangeByRankWithScores: acquiring member and the score value, can only return a value in the start-stop of the index (default ascending), the skip back and take a paging 

            var getValue = RedisCacheHelper.Instance.ZSortedScore ( " zsortkey " , " . 4 " ); 
            Console.WriteLine (getValue);
Copy the code

 

scenes to be used:

Similar to the usage scene set Redis sorted set, the difference is not self-ordering the set, and can provide the sorted set a priority (Score) parameters to sort through the additional user is a member, and is inserted into the ordered, i.e., automatic sorting . When you need an ordered list of non-repetition and collection, you can select the sorted set of data structures, such as twitter's public timeline can be stored up to post time as the score, time is automatically sorted when such acquisition.

Method to realize:

Redis sorted set of internal use HashMap and jump table (SkipList) to ensure the orderly and store data, put HashMap are members of the maps to score, and jump table is stored in all the members of the sort is in store HashMap the score, using a jump table structure can be obtained relatively high search efficiency, and relatively simple in implementation.

 

Here attached Helper class

#region SortSet ordered collection type /// <Summary> /// increased, it may increase the time a Member, can be increased once more 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  to false ;
                 the else 
                    return _db.SortedSetAdd (Key, Member, Score); 
            } 
            the catch (Exception EX) 
            { 
                the throw ; 
            } 
        } 

        ///  <Summary> 
        /// of values from Score increasing
         /// If this value does not exist member, the operation member is performed increases, and returns the current value Score
         ///  </ Summary> 
        ///  <param name = "Key"> of the Key. </ param> 
        ///  <param name = "Member"> of 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); 
            } 
            the catch (Exception EX) 
            { 
                the throw ; 
            } 
        } 

        ///  <Summary> 
        /// to decrement Score values of
         /// If this value does not exist member is performed to increase member operation, and returns the current value Score
         ///  </ Summary> 
        ///  <param name = "Key"> of The Key. </ param> 
        ///  <param name = "Member"> of The Member. </ param> 
        ///  <param name = "Score"> of The Score. </ param> 
        ///  <param name = "DB"> of 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  to false ; 
            } 
        } 


        ///  <Summary> 
        /// Get a ranked list of elements, ranking
         ///  </ 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 ) 
        { 
            the try 
            { 
                var _db = the GetDatabase (DB);
                if ((object) Key == null )
                     return  0 ;
                 the else 
                    return _db.SortedSetRank (Key, mumber) ?? 0 ; 

            } 
            the catch (Exception) 
            { 
                return  0 ; 
            } 
        } 

        ///  <Summary> 
        /// Gets an ordered set value elements
         ///  </ Summary> 
        ///  <param name = "Key"> of the Key. </ param> 
        ///  <param name = "Member"> of the Member. </ param> 
        ///  <param name = "DB"> of 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

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12514953.html