Basic Tutorials of Redis(5) - Sorted Set

  The last post is mainly about the unsorted set,in this post I will show you the sorted set playing an important

role in Redis.There are many command added after the version 2.8.9.OK,let's see the below picture firstly.There

are 24 commands to handle the sorted set,the same as the string.

  

  Many commands are similar with the Set.Both of them are the set,the sorted set's 

playing a important role on sorting.We can use the zadd to store the sorted set.The following example demonstrates

the usage of zadd.

zadd - - b c d e f g h i j k

  For learning how many elements in the set,and who are them ? we can use the command zrange.
zrange - -

  zrange can also make us know the scores of the elements,we should open seletion the withscores to 

find out their scores.

zrange - - withscores

  There are another intresting commands to get the members.zrangebyscore can find out the members by

their scores.For an instance,I want to find out the members' scores between 0 and 6,so I will use  -  

zrangebylex can find out the members by the lexicographical order when some of them are in

the same scores.Now I want to find out the members that order by lexicography when the score are the same

while in the range (a,k],so using  - (a [k  can easily do this job.

zrank.For descending

we use zrevrank .For example,we want to know the member d's rank.

zrank -- d

   There are also many command that we can use to remove the member from the set.Using zrem to remove one or

more members,Using zremrangebyrank to remove the members by their ranks.Using the zremrangebyscore to remove

the members by their scores.Using the zremrangebylex to remove the members by their rank and lexicography.

zrem -- b c

zremrangebyrank -

zremrangebyscore -

zremrangebylex - (e (j

zscore to get its score.To get the score of member e,

we use  - e .For learning how many members in the set by the range of score,we can use zcount

to get the amount.To get the amount of the set by the score's range [0,10],we can use  -  .

   Can we modify the scores of the members?Of course we can.Not only the exists member but also the

member not in the set.If the member not exists in the set,Redis will store a new member to the set.For

example,I want to modify the score of d which is not exists in the set.I will use  - d  to finish

this easy job.And the result is that the set will has a new member with score 1.

  OK,thoes commands are what I want to show you for sorted set.Let's go on to see how StackExchange.Redis

Handle the sorted set.

db.SortedSetAdd(,, set_1 = SortedSetEntry[ SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, SortedSetEntry(, db.SortedSetAdd( Console.WriteLine( ( item db.SortedSetRangeByRank(,,- Console.Write(item + Console.WriteLine( ( item db.SortedSetRangeByRankWithScores( Console.WriteLine(.Format( Console.WriteLine( ( item db.SortedSetRangeByScore(, Console.Write(item + Console.WriteLine( Console.WriteLine( ( item db.SortedSetRangeByValue(, Console.Write(item + Console.WriteLine( Console.WriteLine(.Format(,db.SortedSetRank(, Console.WriteLine(.Format(,Order.Descending))); db.SortedSetRemove(, db.SortedSetRemove(, RedisValue[] { , Console.WriteLine( ( item db.SortedSetRangeByRank(,Order.Ascending)) Console.Write(item + db.SortedSetRemoveRangeByRank(, Console.WriteLine( ( item db.SortedSetRangeByRank(,Order.Ascending)) Console.Write(item + db.SortedSetRemoveRangeByScore(, Console.WriteLine( ( item db.SortedSetRangeByRank(,Order.Ascending)) Console.Write(item + db.SortedSetRemoveRangeByValue(,, Console.WriteLine( ( item db.SortedSetRangeByRank(,Order.Ascending)) Console.Write(item + Console.WriteLine( Console.WriteLine(.Format(,db.SortedSetScore(, Console.WriteLine(.Format(,db.SortedSetLength( Console.WriteLine(.Format(,db.SortedSetIncrement(,)));
  When you debug the above code,the results are as follow.

  The next post of this series is the basic opreation of List in Redis.Thanks for your reading.

相关文章

文章浏览阅读1.3k次。在 Redis 中,键(Keys)是非常重要的概...
文章浏览阅读3.3k次,点赞44次,收藏88次。本篇是对单节点的...
文章浏览阅读8.4k次,点赞8次,收藏18次。Spring Boot 整合R...
文章浏览阅读978次,点赞25次,收藏21次。在Centos上安装Red...
文章浏览阅读1.2k次,点赞21次,收藏22次。Docker-Compose部...
文章浏览阅读2.2k次,点赞59次,收藏38次。合理的JedisPool资...