码迷,mamicode.com
首页 > 其他好文 > 详细

Basic Tutorials of Redis(5) - Sorted Set

时间:2016-09-06 21:08:24      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

  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 member has a 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 set-1 4 a
zadd set-1 5 b 6 c 7 d 8 e 8 f 5 g 6 h 7 i 8 j 8 k

技术分享

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

技术分享

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

find out their scores.

zrange set-1 0 -1 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  zrangebyscore set-1 0 6  

to finish this job.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  zrangebylex set-1 (a [k  can easily do this job.

技术分享

  We also can know 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 set-1 d
zrevrank set-1 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 set-1 a
zrem set-1 b c

技术分享

zremrangebyrank set-1 0 1

技术分享

zremrangebyscore set-1 0 7

技术分享

zremrangebylex set-1 (e (j

技术分享

  If we want to know a member‘s score,we can use zscore to get its score.To get the score of member e,

we use  zscore set-1 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  zcount set-1 0 10 .

技术分享

   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  zincrby set-1 1 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.

 1          //zadd
 2             db.SortedSetAdd("set-1", "a", 4);
 3             var set_1 = new SortedSetEntry[10]
 4             {
 5                 new SortedSetEntry("b",5),
 6                 new SortedSetEntry("c",6),
 7                 new SortedSetEntry("d",7),
 8                 new SortedSetEntry("e",8),
 9                 new SortedSetEntry("f",8),
10                 new SortedSetEntry("g",5),
11                 new SortedSetEntry("h",6),
12                 new SortedSetEntry("i",7),
13                 new SortedSetEntry("j",8),
14                 new SortedSetEntry("k",8)
15             };
16             db.SortedSetAdd("set-1", set_1);
17 
18             //zrange
19             Console.WriteLine("rank by score ascending");
20             foreach (var item in db.SortedSetRangeByRank("set-1", 0, -1, Order.Ascending))
21             {
22                 Console.Write(item + " ");
23             }
24             Console.WriteLine("");
25             foreach (var item in db.SortedSetRangeByRankWithScores("set-1"))
26             {
27                 Console.WriteLine(string.Format("the {0} with score {1}", item.Element, item.Score));
28             }
29             //zrangebyscore
30             Console.WriteLine("sorted by score");
31             foreach (var item in db.SortedSetRangeByScore("set-1",0,6))
32             {
33                 Console.Write(item + " ");
34             }
35             Console.WriteLine("");
36             //zrangebylex
37             Console.WriteLine("sorted by value");
38             foreach (var item in db.SortedSetRangeByValue("set-1","a","z"))
39             {
40                 Console.Write(item + " ");
41             }
42             Console.WriteLine("");
43             //zrank
44             Console.WriteLine(string.Format("d rank in {0} by ascending", db.SortedSetRank("set-1", "d", Order.Ascending)));
45             Console.WriteLine(string.Format("d rank in {0} by descending", db.SortedSetRank("set-1", "d", Order.Descending)));
46 
47             //zrem
48             db.SortedSetRemove("set-1", "a");
49             db.SortedSetRemove("set-1", new RedisValue[2] { "b", "c" });
50             Console.WriteLine("after removing - 1:");
51             foreach (var item in db.SortedSetRangeByRank("set-1", 0, -1, Order.Ascending))
52             {
53                 Console.Write(item + " ");
54             }
55             
56             //zrembyrangebyrank
57             db.SortedSetRemoveRangeByRank("set-1", 0, 1);
58             Console.WriteLine("\nafter removing by rank:");
59             foreach (var item in db.SortedSetRangeByRank("set-1", 0, -1, Order.Ascending))
60             {
61                 Console.Write(item + " ");
62             }
63             //zremrangeby score
64             db.SortedSetRemoveRangeByScore("set-1", 0, 7);
65             Console.WriteLine("\nafter removing by score:");
66             foreach (var item in db.SortedSetRangeByRank("set-1", 0, -1, Order.Ascending))
67             {
68                 Console.Write(item + " ");
69             }
70             //zremrangebylex
71             db.SortedSetRemoveRangeByValue("set-1", "d", "g");
72             Console.WriteLine("\nafter removing by value:");
73             foreach (var item in db.SortedSetRangeByRank("set-1", 0, -1, Order.Ascending))
74             {
75                 Console.Write(item + " ");
76             }
77             Console.WriteLine("");
78             //zscore
79             Console.WriteLine(string.Format("the score of e is {0}", db.SortedSetScore("set-1", "e")));
80             //zcount
81             Console.WriteLine(string.Format("{0} members in set-1", db.SortedSetLength("set-1")));
82             //zincrby
83             Console.WriteLine(string.Format("the score of d increase by 1 is {0}", db.SortedSetIncrement("set-1", "d", 1)));
  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.

Basic Tutorials of Redis(5) - Sorted Set

标签:

原文地址:http://www.cnblogs.com/catcher1994/p/5846967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!