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.

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

zrange 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

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.

we use zrevrank .For example ,we want to know the member d's rank.
zrank set- d
zrevrank set- 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- a
zrem set- b c

zremrangebyrank set-

zremrangebyscore set-

zremrangebylex set- (e (j

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

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.

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", )));

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 w ...
- 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 ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- 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 o ...
- 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 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#? ...
- 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 ...
- Redis 命令 - Sorted Sets
ZADD key score member [score member ...] Add one or more members to a sorted set, or update its scor ...
随机推荐
- Convert BSpline Curve to Arc Spline in OpenCASCADE
Convert BSpline Curve to Arc Spline in OpenCASCADE eryar@163.com Abstract. The paper based on OpenCA ...
- ubuntu系统下如何修改host
Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...
- log4net使用手册
1. log4net简介 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.Java平台下,它还 ...
- arcgis api for js入门开发系列四地图查询(含源代码)
备注:由于实现本篇功能的需求,修改了地图数据的dlsearch.mxd,然后更新了地图服务,需要的在文章最后有提供最新的mxd以及源代码下载的 上一篇实现了demo的地图工具栏,本篇新增地图查询功能, ...
- 去IOE的一点反对意见以及其他
某天在机场听见两老板在聊天,说到他们目前销售的报表老跟不上的问题,说要请一个人,专门合并和分析一些发过来的excel表格,我真想冲上去说,老板,你需要的是一个信息处理的系统,你需要咨询么.回来一直耿耿 ...
- docker4dotnet #1 – 前世今生 & 世界你好
作为一名.NET Developer,这几年看着docker的流行实在是有些眼馋.可惜的是,Docker是基于Linux环境的,眼瞧着那些 java, python, node.js, go 甚至连p ...
- MySQL 优化之 MRR (Multi-Range Read:二级索引合并回表)
MySQL5.6中引入了MRR,专门来优化:二级索引的范围扫描并且需要回表的情况.它的原理是,将多个需要回表的二级索引根据主键进行排序,然后一起回表,将原来的回表时进行的随机IO,转变成顺序IO.文档 ...
- 在 Ubuntu 14.10 中借用 Windows 的字体
在前一篇随笔中,我详细讨论了字体的分类及用途,也以 Fedora 20 为例,展示了字体配置的思路和方法.我在配置 Fedora 20 系统字体的时候,采用的是一种釜底抽薪的方法,完全抛开了系统原有的 ...
- AWS开发人员认证考试样题解析
最近在准备AWS的开发人员考试认证.所以特意做了一下考试样题.每道题尽量给出了文档出处以及解析. Which of the following statements about SQS is true ...
- APP测试点总结(功能,交互,死机崩溃状态分析,容易出错的检查点)
APP测试点总结(功能,交互,死机崩溃状态分析,容易出错的检查点) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近涉足APP端测试,常见检查点总结如下: 一.业务方面: 1. 注册( ...