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.

相关文章

在笔者近 3 年的 Java 一线开发经历中,尤其是一些移动端、用...
这一篇文章拖了有点久,虽然在项目中使用分布式锁的频率比较...
本文梳理总结了一些 Java 互联网项目中常见的 Redis 缓存应用...
书接上回,消息通知系统(notification-system)作为一个独立...
Redis 是目前互联网后端的热门中间件之一,在许多方面都有深...
在Java Spring 项目中,数据与远程数据库的频繁交互对服务器...