Sorted sets
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的更多相关文章
- Redis 命令 - Sorted Sets
ZADD key score member [score member ...] Add one or more members to a sorted set, or update its scor ...
- redis数据类型:sorted sets类型及操作
sorted sets类型及操作: sorted set是set的一个升级版本,它是在set的基础上增加了一个顺序 属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会 自动重新按新的值 ...
- redis的有序集合(Sorted Sets)数据类型
和Sets相比,Sorted Sets增加了一个权重参数score,使得集合中的元素能够按score进行有序排列,比如一个存储全班同学成绩的Sorted Sets,其集合value可以是同学的学号,而 ...
- Redis数据类型:Sorted Sets操作指令
Redis数据类型:Sorted Sets操作指令 Sorted Sets常用操作指令 Sorted Sets,本质是一个有序的Sets,其实在原来的Sets集合中对每一个元素新增了一个属性Score ...
- 四:redis的sets类型 - 相关操作(有序和无序集合)
================四十五种(有序和无序集合):sets种类(它是一个集)============= 简介: set它代表的集合.加入是随意添加----->无序集合 ...
- redis sets类型及操作
sets类型及操作set是集合,它是string类型的无序集合.通过hash table实现,添加.删除.查找的复杂度都是0(1).对集合我们可以实现取交际.差集并集.通过这些操作我们可以实现SNS中 ...
- 缓存数据库-redis数据类型和操作(sorted set)
一:Redis 有序集合(sorted set) Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是 ...
- Redis的数据类型(lists、Sets)
lists类型 Redis 列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素到列表的头部(左边)或者尾部(右边) LPUSH 命令插入一个新的元素到头部, 而 RPUSH 插入一个新元素导 ...
- 使用 Redis 的 sorted set 实现用户排行榜
要求:实现一个用户排行榜,用户数量有很多,排行榜存储的是用户玩游戏的分数,对排行榜的读取压力比较大,如何实现? 思路分析: 实现排行榜,可以考虑使用 Redis 的 zset 结构: 用户数量很多的话 ...
随机推荐
- NetBpm 安装篇(1)
尊重别人劳动成果 转载注明出处:http://www.cnblogs.com/anbylau2130/p/3875718.html 官方主页 http://www.netbpm.org/docs/in ...
- eclipse 安装 ndk 组件
新安装的eclipse没有ndk组件, 我使用的安装包是:android-ndk32-r10b-windows-x86_64.zip,打开preferences如下
- lua 注释
1. 单行注释 -- 功能等同于C++中的// 2. 多行注释 --[[ 注释的内容 ]] 功能等同于C++中的 /**/ 3. 多行注释 --[====[ 注释和内容 ]=== ...
- OpenGL 知识二
OpenGL综述 September 14, 2014 学习OpenGL是学习计算机图形学的一个工具,因为计算机上图形的显示要依靠底层的软件和硬件,学习图形学除了学习基本的概念,线,曲面,图形生成,变 ...
- 【RF库XML测试】Add Element
Name:Add ElementSource:XML <test library>Arguments:[ source | element | index=None | xpath=. ] ...
- 关于 Handler 与 opener
我们可以使用 urllib.request.Request() 构造请求对象,但是对于一些更高级的操作,比如 Cookies 处理.代理设置 .身份验证等等,Request() 是处理不了的这时就需要 ...
- 来数一数XML解析成为Dataset数据
最近在看一些接口,所以目标就是写接口啦,但是我想说的是公司的业务还不曾了解,所以自己先来做一个小小的demo练习吧,主要知道需要和xml有关系的,但是之前从来没有接触过解析xml文件的,在玩撒谎能够搜 ...
- java 读写文件例子
在linux下可以读写中文 import java.io.*; import java.text.SimpleDateFormat; import java.util.*; public class ...
- 《C++ Primer Plus》16.2 智能指针模板类
智能指针是行为类似于指针的类对象,单这种对象还有其他功能.本节介绍三个可帮助管理动态内存分配的智能指针类.先来看看需要哪些功能以及这些功能是如何实现的.请看下面的函数:void remodel(std ...
- 数据流-------C#文件和byte[]互换问题
今天使用FileInfo.CopyTo的时候出现问题,当然并不是使用的问题,而是一些细节. 不过报错的时候,一度让我认为,copyto这个方法,给的参数必须是文件夹,而不是文件.所以就有了下面的查找 ...