Basic Tutorials of Redis(5) - Sorted Set

时间:2023-03-10 04:04:37
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.

  Basic Tutorials of Redis(5) - Sorted Set

  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-  a
zadd set- b c d e f g h i j k

Basic Tutorials of Redis(5) - Sorted Set

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

Basic Tutorials of Redis(5) - Sorted Set

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

find out their scores.

zrange set-  - withscores

Basic Tutorials of Redis(5) - Sorted Set

  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-

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- (a [k  can easily do this job.

Basic Tutorials of Redis(5) - Sorted Set

  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- d
zrevrank set- d

Basic Tutorials of Redis(5) - Sorted Set

  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- a
zrem set- b c

Basic Tutorials of Redis(5) - Sorted Set

zremrangebyrank set-  

Basic Tutorials of Redis(5) - Sorted Set

zremrangebyscore set-  

Basic Tutorials of Redis(5) - Sorted Set

zremrangebylex set- (e (j

Basic Tutorials of Redis(5) - Sorted Set

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

Basic Tutorials of Redis(5) - Sorted Set

  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- d  to finish

this easy job.And the result is that the set will has a new member with score 1.

Basic Tutorials of Redis(5) - Sorted Set

  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.

          //zadd
db.SortedSetAdd("set-1", "a", );
var set_1 = new SortedSetEntry[]
{
new SortedSetEntry("b",),
new SortedSetEntry("c",),
new SortedSetEntry("d",),
new SortedSetEntry("e",),
new SortedSetEntry("f",),
new SortedSetEntry("g",),
new SortedSetEntry("h",),
new SortedSetEntry("i",),
new SortedSetEntry("j",),
new SortedSetEntry("k",)
};
db.SortedSetAdd("set-1", set_1); //zrange
Console.WriteLine("rank by score ascending");
foreach (var item in db.SortedSetRangeByRank("set-1", , -, Order.Ascending))
{
Console.Write(item + " ");
}
Console.WriteLine("");
foreach (var item in db.SortedSetRangeByRankWithScores("set-1"))
{
Console.WriteLine(string.Format("the {0} with score {1}", item.Element, item.Score));
}
//zrangebyscore
Console.WriteLine("sorted by score");
foreach (var item in db.SortedSetRangeByScore("set-1",,))
{
Console.Write(item + " ");
}
Console.WriteLine("");
//zrangebylex
Console.WriteLine("sorted by value");
foreach (var item in db.SortedSetRangeByValue("set-1","a","z"))
{
Console.Write(item + " ");
}
Console.WriteLine("");
//zrank
Console.WriteLine(string.Format("d rank in {0} by ascending", db.SortedSetRank("set-1", "d", Order.Ascending)));
Console.WriteLine(string.Format("d rank in {0} by descending", db.SortedSetRank("set-1", "d", Order.Descending))); //zrem
db.SortedSetRemove("set-1", "a");
db.SortedSetRemove("set-1", new RedisValue[] { "b", "c" });
Console.WriteLine("after removing - 1:");
foreach (var item in db.SortedSetRangeByRank("set-1", , -, Order.Ascending))
{
Console.Write(item + " ");
} //zrembyrangebyrank
db.SortedSetRemoveRangeByRank("set-1", , );
Console.WriteLine("\nafter removing by rank:");
foreach (var item in db.SortedSetRangeByRank("set-1", , -, Order.Ascending))
{
Console.Write(item + " ");
}
//zremrangeby score
db.SortedSetRemoveRangeByScore("set-1", , );
Console.WriteLine("\nafter removing by score:");
foreach (var item in db.SortedSetRangeByRank("set-1", , -, Order.Ascending))
{
Console.Write(item + " ");
}
//zremrangebylex
db.SortedSetRemoveRangeByValue("set-1", "d", "g");
Console.WriteLine("\nafter removing by value:");
foreach (var item in db.SortedSetRangeByRank("set-1", , -, Order.Ascending))
{
Console.Write(item + " ");
}
Console.WriteLine("");
//zscore
Console.WriteLine(string.Format("the score of e is {0}", db.SortedSetScore("set-1", "e")));
//zcount
Console.WriteLine(string.Format("{0} members in set-1", db.SortedSetLength("set-1")));
//zincrby
Console.WriteLine(string.Format("the score of d increase by 1 is {0}", db.SortedSetIncrement("set-1", "d", )));
  When you debug the above code,the results are as follow.

Basic Tutorials of Redis(5) - Sorted Set

  The next post of this series is the basic opreation of List in Redis.Thanks for your reading.