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 ...
随机推荐
- ASP.NET Core应用中如何记录和查看日志
日志记录不仅对于我们开发的应用,还是对于ASP.NET Core框架功能都是一项非常重要的功能特性.我们知道ASP.NET Core使用的是一个极具扩展性的日志系统,该系统由Logger.Logger ...
- OVS 中的各种网络设备 - 每天5分钟玩转 OpenStack(128)
上一节我们启用了 Open vSwitch,本节将查看当前的网络状态并介绍 Open vSwitch 涉及的各种网络设备 初始网络状态 查看一下当前的网络状态. 控制节点 ifconfig 显示控制节 ...
- Node.js:path、url、querystring模块
Path模块 该模块提供了对文件或目录路径处理的方法,使用require('path')引用. 1.获取文件路径最后部分basename 使用basename(path[,ext])方法来获取路径的最 ...
- jQuery.template.js 简单使用
之前看了一篇文章<我们为什么要尝试前后端分离>,深有同感,并有了下面的评论: 我最近也和前端同事在讨论这个问题,比如有时候前端写好页面给后端了,然后后端把这些页面拆分成很多的 views, ...
- PHP代码优化
1 代码优化 1 尽量静态化 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和 ...
- CSS知识总结(八)
CSS常用样式 8.变形样式 改变元素的大小,透明,旋转角度,扭曲度等. transform : none | <transform-function> <transform-fun ...
- window7系统怎么找到开始运行命令
右击开始->属性->开始菜单->自定义>点击运行命令(选择)->确定
- 在Redhat上为.Net 项目构建基于Jenkins + Github + Mono 的持续集成环境
在Redhat enterprise 6.5 的服务器上,为在gutub 上的 .net 项目构建一个持续集成环境,用到了Jenkins和mono.因公司的服务器在内网,访问外网时要通过代理,所以在很 ...
- 从零开始,DIY一个jQuery(1)
从本篇开始会陪大家一起从零开始走一遍 jQuery 的奇妙旅途,在整个系列的实践中,我们会把 jQuery 的主要功能模块都了解和实现一遍. 这会是一段很长的历程,但也会很有意思 —— 作为前端领域的 ...
- App开发的新趋势
移动开发这些年,移动开发者人数越来越多,类似的培训公司发展也很快,不过伴随着的是移动应用的需求这几年发展更为旺盛.要开发好的App,纯原生开发肯定是最佳选择.但是这么多年发展,原生开发的难度并没有降低 ...