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

  For learning how many elements in the set,and who are them ? we can use the command zrange.
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 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

  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

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

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

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

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

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

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

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

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

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

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

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

  9. Redis 命令 - Sorted Sets

    ZADD key score member [score member ...] Add one or more members to a sorted set, or update its scor ...

随机推荐

  1. 引人瞩目的 CSS 变量(CSS Variable)

    这是一个令人激动的革新. CSS 变量,顾名思义,也就是由网页的作者或用户定义的实体,用来指定文档中的特定变量. 更准确的说法,应该称之为 CSS 自定义属性 ,不过下文为了好理解都称之为 CSS 变 ...

  2. Visual Studio 2012远程调试中遇到的问题

    有的时候开发环境没问题的代码在生产环境中会某些开发环境无法重现的问题,或者需要对生产环境代码进行远程调试该怎么办? Vs已经提供给开发者远程调试的工具 下面简单讲讲该怎么用,前期准备:1.本地登录账户 ...

  3. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  4. Web安全相关(三):开放重定向(Open Redirection)

    简介 那些通过请求(如查询字符串和表单数据)指定重定向URL的Web程序可能会被篡改,而把用户重定向到外部的恶意URL.这种篡改就被称为开发重定向攻击.   场景分析 假设有一个正规网站http:// ...

  5. C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素”

    Q: 在反序列化 Xml 字符串为 Xml 对象时,抛出如下异常. 即在 XML文档(0, 0)中有一个错误:缺少根元素. A: 首先看下代码: StringBuilder sb = new Stri ...

  6. Java 程序优化 (读书笔记)

    --From : JAVA程序性能优化 (葛一鸣,清华大学出版社,2012/10第一版) 1. java性能调优概述 1.1 性能概述 程序性能: 执行速度,内存分配,启动时间, 负载承受能力. 性能 ...

  7. PHP设计模式(四)单例模式(Singleton For PHP)

    今天讲单例设计模式,这种设计模式和工厂模式一样,用的非常非常多,同时单例模式比较容易的一种设计模式. 一.什么是单例设计模式 单例模式,也叫单子模式,是一种常用的软件设计模式.在应用这个模式时,单例对 ...

  8. canvas快速绘制圆形、三角形、矩形、多边形

    想看前面整理的canvas常用API的同学可以点下面: canvas学习之API整理笔记(一) canvas学习之API整理笔记(二) 本系列文章涉及的所有代码都将上传至:项目代码github地址,喜 ...

  9. JAVA的内存模型(变量的同步)

    一个线程中变量的修改可能不会立即对其他线程可见,事实上也许永远不可见. 在代码一中,如果一个线程调用了MyClass.loop(),将来的某个时间点,另一个线程调用了MyClass.setValue( ...

  10. linux-centos在VM中的网络配置

    1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...