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
时间: 2024-10-21 21:33:25

Sorted sets的相关文章

Common Element in Two Sorted Sets

There are 2 sorted sets.Find the common elements of those sets e.g. A={1,2,3,4,5,6} B={5,6,7,8,9} o/p C={5,6} Complexity should ne 0(n+m) where n and m is the size of the first and second set respectively Which data structure should be used to store

Redis(2.8.3) 命令学习 - Sorted Sets

ZADD key score member [score member ...] Add one or more members to a sorted set, or update its score if it already exists 127.0.0.1:6379> ZADD foo 1 one (integer) 1 127.0.0.1:6379> ZADD foo 2 two (integer) 1 127.0.0.1:6379> ZADD foo 3 three (int

redis之sorted sets类型及操作

sorted sets类型及操作 sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列的mysql表,一列存value,一列存顺序.操作中key理解为zset的名字. 和set一样sorted set也是string类型元素的集合,不同的是每个元素都会关联一个double类型的score.sorted set的实现是skip list和hash table的混合体.

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

Redis数据类型:Sorted Sets操作指令 Sorted Sets常用操作指令 Sorted Sets,本质是一个有序的Sets,其实在原来的Sets集合中对每一个元素新增了一个属性Score,用于排序. ZADD 将指定的元素及Score添加到集合.如果集合中存在该元素,则更新其Score. 如果集合不存在,会先创建一个集合然后在添加元素及Score. 127.0.0.1:6379> ZADD sortset 1 name (integer) 1 ZRANGE 返回指定下标开始到结束下

Redis详解:sorted sets数据类型及操作

sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列的mysql表,一列存value,一列存顺序.操作中key理解为zset的名字. 系列文章: Redis详解:strings数据类型及操作 Redis详解:hashes数据类型及操作 Redis详解:lists数据类型及操作 Redis详解:sets数据类型及操作 和set一样sorted set也是string类

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

================四五种(有序和无序集合):sets类型(就是集合)============= 一介绍:  set表示集合,添加是是随意添加的----->无序集合 set是集合,它是string类型的无序集合. set是通过hash table实现的,添加,删除和查找的复杂度都是0(1). 对集合我们可以取并集.交集.差集. 通过这写操作我们可以实现sns中的好友推荐和blog的tag功能 1:sadd 向名称key的set中添加元素(唯一的) 例:sadd myset1 one 

redis sets类型及操作

sets类型及操作set是集合,它是string类型的无序集合.通过hash table实现,添加.删除.查找的复杂度都是0(1).对集合我们可以实现取交际.差集并集.通过这些操作我们可以实现SNS中的好友推荐和blog的tag(标签)功能 sadd    向集合中添加元素,成功返回1,失败返回0,重复值添加为失败    例如:    sadd myset1 hello smembers    查看集合中的元素    例如:    smembers myset1 scard    查看集合中的元

EPI (Heap) Merge Multiple sorted arrays.

#include <vector> #include <iostream> #include <queue> using namespace std; /* Write a program that takes an input a set of sorted sequences and compute the union of these sequences as a sorted sequence. For example: [3, 5, 7], [0, 6, 8]

NoSQL -- redis 安装 主从 配置详解 常用命令

Redis 也是key-value存储系统,官方站点 http://redis.io,但相对于memcache,有如下优势: 1.支持更多地value类型(string.hash.lists.sets.sorted sets等): 2.支持数据持久化,预防服务重启后需要重新存储: redis 有两种文件格式:全量数据(RDB=redis database).增量请求(aof=append only file). 前者是将内存中的数据写进磁盘,便于下次读取文件时直接进行加载,快照形式: 后者是将r