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

时间: 2024-08-26 05:31:34

Basic Tutorials of Redis(5) - Sorted Set的相关文章

Basic Tutorials of Redis(9) -First Edition RedisHelper

After learning the basic opreation of Redis,we should take some time to summarize the usage. And I wrote my first edition RedisHelper.Here is the code: The Interface IRedis: 1 public interface IRedis 2 { 3 ITransaction GetTransaction(int db = 0, bool

Basic Tutorials of Redis(4) -Set

This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that the data was stored in the database randomly.And there are 15 commands you can use in Redis,the same as Hash. For storing the data to database,we can

Basic Tutorials of Redis(2) - Commands of Strings

This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you both the native commands and the usage of the StackExchange.Redis.The version of Redis is 3.2.3 and the vesion of StackExchange.Redis is 1.1.604-alph

Basic Tutorials of Redis(1) - Install And Configure Redis

Nowaday, Redis became more and more popular , many projects use it in the cache module and the store module. There are already many people wrote posts about Redis.And I am vary pleasure to join them to share my learing of Redis. But I am new in this

Basic Tutorials of Redis(6) - List

Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with them.I expect that you won't mix them and have a clear mind of them. There are 17 commands we can use in List. Push and pop are the base opreation of t

Basic Tutorials of Redis(3) -Hash

When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#?As for me, the first time I saw the Hash,I considered is as the HashTable.Actually,Hash can identify with HashTable,the same as DataRow.A row data of

Basic Tutorials of Redis(7) -Publish and Subscribe

This post is mainly about the publishment and subscription in Redis.I think you may subscribe some offiial accounts on wechat,and when the authors published something new to their accounts,you will get them in your wechat.The instance can make you un

redis之sorted sets类型及操作

sorted sets类型及操作 sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列的mysql表,一列存value,一列存顺序.操作中key理解为zset的名字. 和set一样sorted set也是string类型元素的集合,不同的是每个元素都会关联一个double类型的score.sorted set的实现是skip list和hash table的混合体.

Redis学习-sorted set数据类型

sorted set 是有序集合,它在 set 的基础上增加了一个顺序属性,这一属性在添加修 改元素的时候可以指定,每次指定后,会自动重新按新的值调整顺序. zadd key score member 添加元素到集合,元素在集合中存在则更新对应 score zrange key start stop 类似 lrange 操作从集合中去指定区间的元素.返回的是有序结果 zrange key start stop withscores 同上,并获取其赋予的序号 zrevrange key start