微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 score

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  score -  

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.

Now the rank of a member.both Ascending and Descending.For ascending,we use 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

Now a member's score,we can use zscore to get its score.To get the score of member e,

we use  score - 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(score ascending ( item db.sortedSetRangeByRank(,,- Console.Write(item + Console.WriteLine( ( item db.sortedSetRangeByRankWithscores( Console.WriteLine(.Format(score {1}score)); score Console.WriteLine(score ( 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 + score db.sortedSetRemoveRangeByscore(, Console.WriteLine(score: ( item db.sortedSetRangeByRank(,Order.Ascending)) Console.Write(item + db.sortedSetRemoveRangeByValue(,, Console.WriteLine( ( item db.sortedSetRangeByRank(,Order.Ascending)) Console.Write(item + Console.WriteLine( score Console.WriteLine(.Format(score of e is {0},db.sortedSetscore(, Console.WriteLine(.Format(,db.sortedSetLength( Console.WriteLine(.Format(score of d increase by 1 is {0},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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐