Home Database Mysql Tutorial Nosql之Redis: zset(有序集)数据类型及操作命令

Nosql之Redis: zset(有序集)数据类型及操作命令

Jun 07, 2016 pm 04:29 PM
nosql redis zset operate data type

一:概述 zset全称为sorted-sets类型,和set数据类型有极为相似,都是字符串的集合,都不允许重复的成员 出现在一个set中.两者的主要区别是zset的每一个成员都会有一个分数(score)与之关联.redis正是通过分数来为集合中的成员进行从小到大的排序.zset的成员是唯

一:概述
zset全称为sorted-sets类型,和set数据类型有极为相似,都是字符串的集合,都不允许重复的成员
出现在一个set中.两者的主要区别是zset的每一个成员都会有一个分数(score)与之关联.redis正是通过分数来为集合中的成员进行从小到大的排序.zset的成员是唯一的,但分数(score)却可以重复.
在zset中添加、删除或更新一个成员都是非常快速的操作,其时间复杂度为集合中成员数量的对数.

Sorted-Sets中的成员在集合中的位置是有序的.

二:相关命令

1: zadd
命令格式: zadd key score member [[score] [member] …]

描述:将一个或多个 member 元素及其 score 值加入到有序集 key 当中.如果某个 member 已经是有序集的成员,那么更新这个 member 的 score 值,并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。score 值可以是整数值或双精度浮点数。如果 key 不存在,则创建一个空的有序集并执行 ZADD 操作。当 key 存在但不是有序集类型时,返回一个错误。

时间复杂度: O(M*log(N)), N 是有序集的基数, M 为成功添加的新成员的数量
返回值:被成功添加的新成员的数量,不包括那些被更新的、已经存在的成员。

操作命令如下:

#添加一个元素
redis 127.0.0.1:6379> zadd zset_list 11 test1
(integer) 1

#添加多个元素
redis 127.0.0.1:6379> zadd zset_list 9 test2 10 test3
(integer) 2

#查看元素
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test2″
2) “9″
3) “test3″
4) “10″
5) “test1″
6) “11″
redis 127.0.0.1:6379> zrange zset_list 0 -1
1) “test2″
2) “test3″
3) “test1″

# 添加已存在元素,且 score 值不变 操作不成功返回0
redis 127.0.0.1:6379> zadd zset_list 10 test1
(integer) 0

redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test2″
2) “9″
3) “test1″
4) “10″
5) “test3″
6) “10″

# 添加已存在元素,但是改变 score 值
redis 127.0.0.1:6379> zadd zset_list 7 test1
(integer) 0
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “7″
3) “test2″
4) “9″
5) “test3″
6) “10″
2:zrem
命令格式: ZREM key member [member ...]

描述:移除有序集 key 中的一个或多个成员,不存在的成员将被忽略。
当 key 存在但不是有序集类型时,返回一个错误。
时间复杂度:O(M*log(N)), N 为有序集的基数, M 为被成功移除的成员的数量。
返回值:被成功移除的成员的数量,不包括被忽略的成员。
操作命令如下:

redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “7″
3) “test2″
4) “9″
5) “test3″
6) “10″

#移除单个元素
redis 127.0.0.1:6379> zrem zset_list test1
(integer) 1
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test2″
2) “9″
3) “test3″
4) “10″
#移除多个
redis 127.0.0.1:6379> zrem zset_list test2 test3
(integer) 2
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
(empty list or set)

# 移除不存在元素
redis 127.0.0.1:6379> zrem zset_list non-exists-element
(integer) 0

3:zcard
描述:返回zset集合的成员数
时间复杂度:O(1)
返回值:当 key 存在且是有序集(zset)类型时,返回集合内的成员数。不存在返回0。
操作命令如下:
redis 127.0.0.1:6379> zcard zset_list
(integer) 0
redis 127.0.0.1:6379> zadd zset_list 1 test1
(integer) 1
redis 127.0.0.1:6379> zcard zset_list
(integer) 1

4:zcount
命令格式:ZCOUNT key min max
描述:返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。
时间复杂度: O(log(N)+M), N 为有序集的基数, M 为值在 min 和 max 之间的元素的数量。
返回值:score 值在 min 和 max 之间的成员的数量。
操作命令如下:
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “1″
3) “test2″
4) “100″
5) “test3″
6) “200″
7) “test4″
8) “300″
redis 127.0.0.1:6379> zcount zset_list 100 200
(integer) 2
redis 127.0.0.1:6379> zcount zset_list 100 300
(integer) 3
5: zscore
命令格式:ZSCORE key member
描述:返回有序集 key 中,成员 member 的 score 值。
如果 member 元素不是有序集 key 的成员,或 key 不存在,返回 nil 。
时间复杂度:O(1)
操作命令如下:
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “1″
3) “test2″
4) “100″
5) “test3″
6) “200″
7) “test4″
8) “300″
redis 127.0.0.1:6379> zscore zset_list test2
“100″

6:zincrby
命令格式:ZINCRBY key increment member
描述:为有序集 key 的成员 member 的 score 值加上增量 increment 。
时间复杂度:O(log(N))
返回值: 返回member 成员的新 score 值,以字符串形式表示。
操作命令如下:
redis 127.0.0.1:6379> zscore zset_list test2
“100″
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> zincrby zset_list 10 test2
“110″
redis 127.0.0.1:6379> zincrby zset_list -6 test2
“104″
7:zrange
命令格式: ZRANGE key start stop [WITHSCORES]
描述:返回指定区间的成员。其中成员位置按 score 值递增(从小到大)来排序。 WITHSCORES选项是用来让成员和它的score值一并返回.(在前面我们已经用到过)
时间复杂度:O(log(N)+M), N 为有序集的基数,而 M 为结果集的基数。
返回值:返回指定区间的成员列表.
操作命令如下:

redis 127.0.0.1:6379> zrange zset_list 0 -1
1) “test1″
2) “test2″
3) “test3″
4) “test4″
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “1″
3) “test2″
4) “104″
5) “test3″
6) “200″
7) “test4″
8) “300″

#当给定区间不存在于有序集时的情况
redis 127.0.0.1:6379> zrange zset_list 10000 30000 withscores
(empty list or set)

7:zrevrange
命令格式:ZREVRANGE key start stop [WITHSCORES]
描述:和zrange一样使用,唯一不同是其成员位置按 score 值递减(从大到小)来排列。
8:zrangebyscore
命令格式:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
描述:返回有序集key中所有score值介于min与max之间(包括等于)的成员.成员按score值递增(从小到大)排列 。min 和 max 可以是 -inf 和 +inf
可选limit参数指定返回结果的数量及区间。
时间复杂度:O(log(N)+M), N 为有序集的基数, M 为被结果集的基数。
返回值:指定区间内,带有 score 值(可选)的有序集成员的列表。
操作命令如下:
redis 127.0.0.1:6379> zrangebyscore zset_list -inf +inf
1) “test1″
2) “test2″
3) “test3″
4) “test4″
redis 127.0.0.1:6379> zrangebyscore zset_list -inf +inf withscores
1) “test1″
2) “1″
3) “test2″
4) “104″
5) “test3″
6) “200″
7) “test4″
8) “300″
redis 127.0.0.1:6379> zrangebyscore zset_list -inf 100 withscores
1) “test1″
2) “1″

#显示大于100 小于等于700的成员
redis 127.0.0.1:6379> zrangebyscore zset_list (100 700
1) “test2″
2) “test3″
3) “test4″

#显示条件 100 redis 127.0.0.1:6379> zrangebyscore zset_list (100 (700
1) “test2″
2) “test3″
3) “test4″
8:zrevrangebyscore
命令格式: zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]
描述:和zrangebyscoreg一样,唯一不同的是成员按 score 值递减(从大到小)的次序排列。

9:zrank
命令格式: zrank key member
描述:返回有序集key中成员member的排名。成员按 score 值递增(从小到大)顺序排列。
排名以0开始,也就是说score 值最小的为0.
时间复杂度:O(log(N))
返回值:返回成员排名,member不存在返回nil.

9:zrevrank
命令格式: zrevrank key member
描述:返回有序集key中成员member的排名。成员按 score 值递增(从大到小)顺序排列。
排名以0开始,也就是说score 值最大的为0.
时间复杂度:O(log(N))
返回值:返回成员排名,member不存在返回nil.
10:zremrangebyrank
命令格式: ZREMRANGEBYRANK key start stop
描述:移除有序集 key 中,指定排名(rank)区间内的所有成员。区间分别以下标参数 start 和 stop 指出,包含 start 和 stop 在内。
下标参数 start 和 stop 都以 0 为底,也就是说,以 0 表示有序集第一个成员,以 1 表示有序集第二个成员,以此类推。
你也可以使用负数下标,以 -1 表示最后一个成员, -2 表示倒数第二个成员,以此类推。
时间复杂度:O(log(N)+M), N 为有序集的基数,而 M 为被移除成员的数量。
返回值:被移除成员的数量。

11:zremrangebyscore
命令格式:zremrangebyscore key min max
描述:移除score值介于min和max之间(等于)的成员
时间复杂度:O(log(N)+M), N 为有序集的基数,而 M 为被移除成员的数量。
返回值:被移除成员的数量。
操作如下:
# 移除所有score在 150 到 350 内的数据
redis> zremrangebyscore zset_list 100 200
(integer) 1

12:zunionstore
命令格式:ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
描述:计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
12: zinterstore
命令格式:ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
描述:计算给定的一个或多个有序集的交集。其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
时间复杂度:
O(N*K)+O(M*log(M)), N 为给定 key 中基数最小的有序集, K 为给定有序集的数量, M 为结果集的基数。
返回值:保存到 destination 的结果集成员数。
操作命令如下:
redis 127.0.0.1:6379> zrange z_ulist_1 0 -1 withscores
1) “jack”
2) “20″
3) “abc”
4) “30″
5) “bb”
6) “50″
7) “cc”
8) “50″
redis 127.0.0.1:6379> zadd z_ulist_2 20 bb 40 789 48 a980
(integer) 3
redis 127.0.0.1:6379> zinterstore z_ulist_x 2 z_ulist_1 z_ulist_2
(integer) 1
redis 127.0.0.1:6379> zrange z_ulist_x 0 -1 withscores
1) “bb”
2) “70″

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to 0x80242008 error when installing Windows 11 10.0.22000.100 Solution to 0x80242008 error when installing Windows 11 10.0.22000.100 May 08, 2024 pm 03:50 PM

1. Start the [Start] menu, enter [cmd], right-click [Command Prompt], and select Run as [Administrator]. 2. Enter the following commands in sequence (copy and paste carefully): SCconfigwuauservstart=auto, press Enter SCconfigbitsstart=auto, press Enter SCconfigcryptsvcstart=auto, press Enter SCconfigtrustedinstallerstart=auto, press Enter SCconfigwuauservtype=share, press Enter netstopwuauserv , press enter netstopcryptS

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

Golang API caching strategy and optimization Golang API caching strategy and optimization May 07, 2024 pm 02:12 PM

The caching strategy in GolangAPI can improve performance and reduce server load. Commonly used strategies are: LRU, LFU, FIFO and TTL. Optimization techniques include selecting appropriate cache storage, hierarchical caching, invalidation management, and monitoring and tuning. In the practical case, the LRU cache is used to optimize the API for obtaining user information from the database. The data can be quickly retrieved from the cache. Otherwise, the cache can be updated after obtaining it from the database.

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

Caching mechanism and application practice in PHP development Caching mechanism and application practice in PHP development May 09, 2024 pm 01:30 PM

In PHP development, the caching mechanism improves performance by temporarily storing frequently accessed data in memory or disk, thereby reducing the number of database accesses. Cache types mainly include memory, file and database cache. Caching can be implemented in PHP using built-in functions or third-party libraries, such as cache_get() and Memcache. Common practical applications include caching database query results to optimize query performance and caching page output to speed up rendering. The caching mechanism effectively improves website response speed, enhances user experience and reduces server load.

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

See all articles