Redis in Action JOSIAH L. CARLSON MANNING Shelter Island

ZSETs offer the ability to store a mapping of members to scores (similar to the keys and values  of HASHes).  These  mappings  allow  us  to  manipulate  the  numeric  scores, and fetch and scan over both members and scores based on the sorted order of the scores.
In chapter 1, we showed a brief example that used ZSETs as a way of sorting submitted articles based on time and how many up-votes they had received, and in chapter 2, we had an example that used ZSETs as a way of handling the expiration of old cookies. In this section, we’ll talk about commands that operate on ZSETs. You’ll learn how to  add  and  update  items  in ZSETs,  as  well  as  how  to  use  the ZSET  intersection  and union commands. When finished with this section, you’ll have a much clearer under-standing about how ZSETs work, which will help you to better understand what we did with them in chapter 1, and how we’ll use them in chapters 5, 6, and 7.

conn.zadd('zset-key', 'a', 3, 'b', 2, 'c', 1);
//ZADD key-name score member [score member ...]—Adds members with the given scores to the ZSET
conn.zcard('zset-key');
//ZCARD key-name—Returns the number of members in the ZSET -->Knowing how large a ZSET is can tell us in some cases if it’s necessary to trim our ZSET
conn.zincrby('zset-key', 'c', 3);
//ZINCRBY key-name increment member—Increments the member in the ZSET
conn.zscore('zset-key', 'b');
//ZSCORE key-name member—Returns the score of the member in the ZSET --->Fetching scores of individual members can be useful if we’ve been keeping counters or toplists
conn.zrank('zset-key', 'c');
//ZRANK key-name member—Returns the position of the given member in the ZSET
conn.zcount('zset-key', 0, 3);
//ZCOUNT key-name min max—Returns the number of members with scores between the provided minimum and maximum -->Counting the number of itemswith a given range of scores canbe quite useful for some tasks
conn.zrem('zset-key', 'b');
//ZREM key-name member [member ...]—Removes the members from the ZSET, returning the number of members that were removed --->Removing membersis as easy as adding them
conn.zrange('zset-key', 0, -1, withscore=True);
//ZRANGE key-name start stop [WITHSCORES]—Returns the members and optionally the scores for the members with ranks between start and stop --->For debugging, we usually fetch theentire ZSET with this ZRANGE call, but real use caseswill usually fetch items a relatively small group at a time

Sorted sets的更多相关文章

  1. Redis 命令 - Sorted Sets

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

  2. redis数据类型:sorted sets类型及操作

    sorted sets类型及操作: sorted set是set的一个升级版本,它是在set的基础上增加了一个顺序 属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会 自动重新按新的值 ...

  3. redis的有序集合(Sorted Sets)数据类型

    和Sets相比,Sorted Sets增加了一个权重参数score,使得集合中的元素能够按score进行有序排列,比如一个存储全班同学成绩的Sorted Sets,其集合value可以是同学的学号,而 ...

  4. Redis数据类型:Sorted Sets操作指令

    Redis数据类型:Sorted Sets操作指令 Sorted Sets常用操作指令 Sorted Sets,本质是一个有序的Sets,其实在原来的Sets集合中对每一个元素新增了一个属性Score ...

  5. 四:redis的sets类型 - 相关操作(有序和无序集合)

    ================四十五种(有序和无序集合):sets种类(它是一个集)=============      简介:  set它代表的集合.加入是随意添加----->无序集合    ...

  6. redis sets类型及操作

    sets类型及操作set是集合,它是string类型的无序集合.通过hash table实现,添加.删除.查找的复杂度都是0(1).对集合我们可以实现取交际.差集并集.通过这些操作我们可以实现SNS中 ...

  7. 缓存数据库-redis数据类型和操作(sorted set)

    一:Redis 有序集合(sorted set) Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是 ...

  8. Redis的数据类型(lists、Sets)

    lists类型 Redis 列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素到列表的头部(左边)或者尾部(右边) LPUSH 命令插入一个新的元素到头部, 而 RPUSH 插入一个新元素导 ...

  9. 使用 Redis 的 sorted set 实现用户排行榜

    要求:实现一个用户排行榜,用户数量有很多,排行榜存储的是用户玩游戏的分数,对排行榜的读取压力比较大,如何实现? 思路分析: 实现排行榜,可以考虑使用 Redis 的 zset 结构: 用户数量很多的话 ...

随机推荐

  1. 关于C中函数传参的一点理解

    一般来说c传值分为传值与传指针,Java里没有指针,因此只有传值,但是Java里传值分为简单变量传值和引用型变量传值,从本质上来说这两者没啥区别. 下面主要说的是传参时对原变量的影响: 最初练习创建单 ...

  2. 【ArcGIS】Web AppBuilder For ArcGIS 配置使用

    一.Portal注册 2.Web AppBuilder配置 输入https://XXXX.YYYY.com.cn:3344/webappbuilder/打开配置界面 填写Portal的Url和AppI ...

  3. 判断元素是否存时,使用isset会比in_array快得多

    情境 有时候,我们需要判断一个元素是否存在于已有数据中(以此来获得非重复值),这时候,使用isset来判断会比in_array快得多很多!! 测试 1)准备测试数据 $exists_a = []; $ ...

  4. iOS开发--UILabel可以显示\n

    UILabel*label; //设置换行 label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = ; 换行符还是“\ ...

  5. 集群瓶颈为什么是磁盘io

    阅读本文思考: 1.对磁盘IO了解多少 2.为什么是磁盘IO是瓶颈,有没有自己的答案 想了解磁盘io可以查看此帖:集群瓶颈:磁盘IO必读 (磁盘IO:磁盘输出输出) 集群的瓶颈提出多种看法,其中网络和 ...

  6. Mac终端解压命令集合

    tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压1 ...

  7. 说说C与汇编之间的互相联系(转)

    在嵌入式系统开发中,目前使用的主要编程语言是C和汇编,C++已经有相应的编译器,但是现在使用还是比较少的.在稍大规模的嵌入式软件中,例如含有OS,大部分的代码都是用C编写的,主要是因为C语言的结构比较 ...

  8. thinkphp3.2 判断星期几

    后台 $w=date('w'); $week=array( "=>"星期日", "=>"星期一", "=>&qu ...

  9. 【Linux基础】Linux基础命令行学习笔记

    绝对路径:cd /home/python相对路径:cd Downloads . 表示:当前那路径..表示:当前路径的上一层../.. 表示:当前路径的上二层 没有...或者以上的 ls: ls 查看当 ...

  10. 日记整理---->2016-11-25

    2017-03-02开始,记录的一些知识点.岁月长,三更漏.漫漫回廊,依稀人空瘦.借酒消愁入断肠,倚剑笑我,我独自寻殇. 一.vx中的v-bind和{{}}的区别 <td class=" ...