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. Go之简单并发

    func Calculate(id int) { fmt.Println(id) } 使用go来实现并发 func main() { for i := 0; i < 100; i++ { go ...

  2. Linux环境SVN命令行使用经验总结(转)

    在windows机器上开发得差不多了之后,打包传送到开发机编译,在开发机上解决编译错误. [缺点] 浪费时间在打包解包,机器间传输代码. 在windows机器上开发之后,check in代码进分支,在 ...

  3. CreateThreadpoolIo 函数小记

    函数原型如下: PTP_IO WINAPI CreateThreadpoolIo( _In_ HANDLE fl, _In_ PTP_WIN32_IO_CALLBACK pfnio, _Inout_o ...

  4. 删除ORACLE目录OCI.dll文件无法删除 (转)

    删除ORACLE目录OCI.dll文件无法删除 今天准备把虚拟机里的10g卸载安装11g来研究一些新特性 卸载没有用自带的UnInstall工具之前看warehouse的讲课视频凭记忆手动卸载了下删除 ...

  5. hadoop 随笔

    http://p-x1984.iteye.com/blog/859843 面试hadoop可能被问到的问题,你能回答出几个 ? 1.hadoop运行的原理? 2.mapreduce的原理? 3.HDF ...

  6. [Git] 解决 insufficient permission for adding an object to repository database

    [环境] OS: CentOS 6.5 Git: 1.7.1 [症状描述] Git 中心仓库路径 ~/project.git,克隆库路径 ~/project.clone,克隆库中包含一个文件 ~/pr ...

  7. Python时间戳与时间字符串互相转换实例代码

    #设a为字符串import timea = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组time.strptime(a,'%Y-%m-% ...

  8. 关于windows下基于php7.0.2下编写的第一个扩展

    网上的教程是比较多的,但是基于php7+windows的教程非常之少,通过几天的摸索及参考很多资料,终于发现如下可以运行. php7要求使用vc2015,同时安装sdk,我使用的是8.1的window ...

  9. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

  10. [Noi2016]区间[离散化+线段树维护+决策单调性]

    4653: [Noi2016]区间 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 621  Solved: 329[Submit][Status][D ...