加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Basic Tutorials of Redis(5) - Sorted Set

发布时间:2020-12-16 04:46:56 所属栏目:安全 来源:网络整理
导读: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

  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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读