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 ...
随机推荐
- 利用bootstrap的carousel.js实现轮播图动画
前期准备: 1.jquery.js. 2.bootstrap的carousel.js. 3.bootstrap.css. 如果大家不知道在哪下载,可以联系小颖,小颖把这些js和css可以发送给你. 一 ...
- MVC Core 网站开发(Ninesky) 2、栏目
栏目是网站的常用功能,按照惯例栏目分常规栏目,单页栏目,链接栏目三种类型,这次主要做添加栏目控制器和栏目模型两个内容,控制器这里会用到特性路由,模型放入业务逻辑层中(网站计划分数据访问.业务逻辑和We ...
- 使用Microsoft的IoC框架:Unity来对.NET应用进行解耦
1.IoC/DI简介 IoC 即 Inversion of Control,DI 即 Dependency Injection,前一个中文含义为控制反转,后一个译为依赖注入,可以理解成一种编程模式,详 ...
- 【干货分享】流程DEMO-费用报销
流程名: 费用报销 业务描述: 流程发起时,要选择需要关联的事务审批单,会检查是否超申请,如果不超申请,可以直接发起流程,如果超了申请,需要检查预算,如果预算不够,将不允许发起报销申请,如果预算够用, ...
- git init和git init -bare区别
1 Git init 和 git init –bare 的区别 用"git init"初始化的版本库用户也可以在该目录下执行所有git方面的操作.但别的用户在将更新push上来的 ...
- 跟着老男孩教育学Python开发【第四篇】:模块
双层装饰器示例 __author__ = 'Golden' #!/usr/bin/env python # -*- coding:utf-8 -*- USER_INFO = {} def ch ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- Netty构建分布式消息队列(AvatarMQ)设计指南之架构篇
目前业界流行的分布式消息队列系统(或者可以叫做消息中间件)种类繁多,比如,基于Erlang的RabbitMQ.基于Java的ActiveMQ/Apache Kafka.基于C/C++的ZeroMQ等等 ...
- webstorm license key
JetBrains WebStorm注册码 UserName: William License Key : ===== LICENSE BEGIN ===== 45550-12042010 00001 ...
- xcode8.1 插件失效的问题
1,查看 Xcode 插件安装目录 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins 鼠标点一下桌面, command+shi ...