Redis 的数据类型 - Zset 集合类型:有序集合

ZADD:将元素及其分数添加到集合中

  语法:ZADD key courseScore member [courseScore member]

    ZADD courseScore 100 tom

    ZADD courseScore 99 jerry 88 mario 77 jack 66 lucy 55 chris

    ZADD courseScore 60 tom 67 apple 56 cherry  #这里 tom 已经存在了,直接略过不执行,返回值为 2 #

    ZADD courseScore 12.3 lenovo

    ZADD courseScore +inf maxInt -inf minInt

    ZSCORE courseScore maxInt

    ZSCORE courseScore minInt

127.0.0.1:6379> ZADD courseScore 100 tom
(integer) 1

127.0.0.1:6379> ZADD courseScore 99 jerry 88 mario 77 jack 66 lucy 55 chris
(integer) 5

127.0.0.1:6379> ZADD courseScore 60 tom 67 apple 56 cherry
(integer) 2

127.0.0.1:6379> ZADD courseScore 12.3 lenovo
(integer) 1

127.0.0.1:6379> ZADD courseScore +inf maxInt -inf minInt
(integer) 1

127.0.0.1:6379> ZSCORE courseScore maxInt
"inf"

127.0.0.1:6379> ZSCORE courseScore minInt
"-inf"

ZSCORE:获得指定元素的分数

  语法:ZSCORE key member

    ZSCORE courseScore mario

    ZSCORE courseScore lenovo

127.0.0.1:6379> ZSCORE courseScore mario
"88"

127.0.0.1:6379> ZSCORE courseScore lenovo
"12.300000000000001"

ZRANGE:按照元素分数从小到大的顺序返回指定索引 start 到 stop 之间所有元素(包含两端)

  语法:ZRANGE key start stop [WITHSCORES]

    ZRANGE courseScore 0 -1

    ZRANGE courseScore 0 -1 WITHSCORES

    ZRANGE courseScore 0 2 WITHSCORES

    ZRANGE courseScore 0 2000 WITHSCORES

    ZRANGE courseScore 1000 2000 WITHSCORES

    ZADD courseScore 60 test1 60 test2 60 test3 60 test4

    ZRANGE courseScore 0 -1 WITHSCORES

  #当两个元素的分数相同的时候,Redis 在排序按照字典的顺序 (0<9<A<Z<a<z) ,如果使用的是 UTF-8 的编码方式的中文同样按照字典顺序排列#

127.0.0.1:6379> ZRANGE courseScore 0 -1
 1) "minInt"
 2) "minInx"
 3) "lenovo"
 4) "chris"
 5) "cherry"
 6) "tom"
 7) "lucy"
 8) "apple"
 9) "jack"
10) "mario"
11) "jerry"
12) "maxInt"

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "minInt"
 2) "-inf"
 3) "minInx"
 4) "-inf"
 5) "lenovo"
 6) "12.300000000000001"
 7) "chris"
 8) "55"
 9) "cherry"
10) "56"
11) "tom"
12) "60"
13) "lucy"
14) "66"
15) "apple"
16) "67"
17) "jack"
18) "77"
19) "mario"
20) "88"
21) "jerry"
22) "99"
23) "maxInt"
24) "inf"

127.0.0.1:6379> ZRANGE courseScore 0 2 WITHSCORES
1) "minInt"
2) "-inf"
3) "minInx"
4) "-inf"
5) "lenovo"
6) "12.300000000000001"

127.0.0.1:6379> ZRANGE courseScore 0 2000 WITHSCORES
 1) "minInt"
 2) "-inf"
 3) "minInx"
 4) "-inf"
 5) "lenovo"
 6) "12.300000000000001"
 7) "chris"
 8) "55"
 9) "cherry"
10) "56"
11) "tom"
12) "60"
13) "lucy"
14) "66"
15) "apple"
16) "67"
17) "jack"
18) "77"
19) "mario"
20) "88"
21) "jerry"
22) "99"
23) "maxInt"
24) "inf"

127.0.0.1:6379> ZRANGE courseScore 1000 2000 WITHSCORES
(empty list or set)

127.0.0.1:6379> ZADD courseScore 60 test1 60 test2 60 test3 60 test4
(integer) 4

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "minInt"
 2) "-inf"
 3) "minInx"
 4) "-inf"
 5) "lenovo"
 6) "12.300000000000001"
 7) "chris"
 8) "55"
 9) "cherry"
10) "56"
11) "test1"
12) "60"
13) "test2"
14) "60"
15) "test3"
16) "60"
17) "test4"
18) "60"
19) "tom"
20) "60"
21) "lucy"
22) "66"
23) "apple"
24) "67"
25) "jack"
26) "77"
27) "mario"
28) "88"
29) "jerry"
30) "99"
31) "maxInt"
32) "inf"

ZREVRANGE:和 ZRANGE 相反,按照分数从大到小的顺序

  语法:ZREVRANGE key start stop [WITHSCORES]

    ZREVRANGE courseScore 0 -1 WITHSCORES

127.0.0.1:6379> ZREVRANGE courseScore 0 -1 WITHSCORES
 1) "maxInt"
 2) "inf"
 3) "jerry"
 4) "99"
 5) "mario"
 6) "88"
 7) "jack"
 8) "77"
 9) "apple"
10) "67"
11) "lucy"
12) "66"
13) "tom"
14) "60"
15) "test4"
16) "60"
17) "test3"
18) "60"
19) "test2"
20) "60"
21) "test1"
22) "60"
23) "cherry"
24) "56"
25) "chris"
26) "55"
27) "lenovo"
28) "12.300000000000001"
29) "minInx"
30) "-inf"
31) "minInt"
32) "-inf"

ZRANGEBYSCORE:获得指定分数范围内的元素,按照分数从小到大的顺序,返回的是分数在指定的 min 到 max 的元素

  语法:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

    ZRANGEBYSCORE courseScore 80 90   #获得分数80~90之间的所有元素

    ZRANGEBYSCORE courseScore 60 90 WITHSCORES   #获得分数60~90之间的所有元素

    ZRANGEBYSCORE courseScore 60 (88 WITHSCORES

    ZRANGEBYSCORE courseScore (60 (88 WITHSCORES   #通过 ‘(‘ 代表不包端点#

    ZRANGEBYSCORE courseScore 90 +inf WITHSCORES

    ZRANGEBYSCORE courseScore 80 +inf WITHSCORES LIMIT 0 3

    ZRANGEBYSCORE courseScore 60 +inf WITHSCORES LIMIT 3 3

127.0.0.1:6379> ZRANGEBYSCORE courseScore 80 90
1) "mario"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 90 WITHSCORES
 1) "test1"
 2) "60"
 3) "test2"
 4) "60"
 5) "test3"
 6) "60"
 7) "test4"
 8) "60"
 9) "tom"
10) "60"
11) "lucy"
12) "66"
13) "apple"
14) "67"
15) "jack"
16) "77"
17) "mario"
18) "88"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 (88 WITHSCORES
 1) "test1"
 2) "60"
 3) "test2"
 4) "60"
 5) "test3"
 6) "60"
 7) "test4"
 8) "60"
 9) "tom"
10) "60"
11) "lucy"
12) "66"
13) "apple"
14) "67"
15) "jack"
16) "77"

127.0.0.1:6379> ZRANGEBYSCORE courseScore (60 (88 WITHSCORES
1) "lucy"
2) "66"
3) "apple"
4) "67"
5) "jack"
6) "77"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 90 +inf WITHSCORES
1) "jerry"
2) "99"
3) "maxInt"
4) "inf"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 80 +inf WITHSCORES LIMIT 0 3
1) "mario"
2) "88"
3) "jerry"
4) "99"
5) "maxInt"
6) "inf"

127.0.0.1:6379> ZRANGEBYSCORE courseScore 60 +inf WITHSCORES LIMIT 3 3
1) "test4"
2) "60"
3) "tom"
4) "60"
5) "lucy"
6) "66"

ZREVRANGEBYSCORE:获得指定分数范围内的元素,按照元素的分数从大到小的顺序返回 min 和 max 之间的元素

  语法:ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

    ZREVRANGEBYSCORE courseScore 90 60 WITHSCORES

127.0.0.1:6379> ZREVRANGEBYSCORE courseScore 90 60 WITHSCORES
 1) "mario"
 2) "88"
 3) "jack"
 4) "77"
 5) "apple"
 6) "67"
 7) "lucy"
 8) "66"
 9) "tom"
10) "60"
11) "test4"
12) "60"
13) "test3"
14) "60"
15) "test2"
16) "60"
17) "test1"
18) "60"

ZINCRBY:操作某个元素的分数,返回操作之后的分数

  语法:ZINCRBY key increment member

    ZINCRBY courseScore 5 tom

    ZINCRBY courseScore -15 tom

127.0.0.1:6379> ZINCRBY courseScore 5 tom
"65"

127.0.0.1:6379> ZINCRBY courseScore -15 tom
"50"

ZCARD:获得集合中元素的数量

  语法:ZCARD key

    ZCARD courseScore

127.0.0.1:6379> ZCARD courseScore
(integer) 16

ZCONUT:获得指定分数内的元素的数量

  语法:ZCOUNT key min max

    ZCOUNT courseScore 80 90

127.0.0.1:6379> ZCOUNT courseScore 80 90
(integer) 1

ZREM:删除一个或者多个元素,返回删除元素的个数

  语法:ZREM key member ...

    ZREM courseScore tom

127.0.0.1:6379> ZREM courseScore tom
(integer) 1

ZREMRANGEBYRANK:按照排名范围删除元素,按照分数从小到大的顺序删除所指定的排名范围内的所有元素

  语法:ZREMRANGEBYRANK key start stop

    ZREMRANGEBYRANK courseScore 0 5

    ZRANGE courseScore 0 -1 WITHSCORES

127.0.0.1:6379> ZREMRANGEBYRANK courseScore 0 5
(integer) 6

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "test4"
 2) "60"
 3) "lucy"
 4) "66"
 5) "apple"
 6) "67"
 7) "jack"
 8) "77"
 9) "mario"
10) "88"
11) "jerry"
12) "99"
13) "tom"
14) "100"
15) "maxInt"
16) "inf"

ZREMRANGEBYSCORE:按照分数范围删除元素

  语法:ZREMRANGEBYSCORE key min max

    ZREMRANGEBYSCORE courseScore 60 70

    ZRANGE courseScore 0 -1 WITHSCORES

127.0.0.1:6379> ZREMRANGEBYSCORE courseScore 60 70
(integer) 3

127.0.0.1:6379> ZRANGE courseScore 0 -1 WITHSCORES
 1) "jack"
 2) "77"
 3) "mario"
 4) "88"
 5) "jerry"
 6) "99"
 7) "tom"
 8) "100"
 9) "maxInt"
10) "inf"

ZRANK:获得指定元素的排名,根据分数从小到大的顺序

  语法:ZRANK key member

    ZRANK courseScore tom

127.0.0.1:6379> ZRANK courseScore tom
(integer) 3

ZREVRANK:获得指定元素的排名,根据分数从大到小的顺序

  语法:ZREVRANK key member

    ZREVRANK courseScore tom

127.0.0.1:6379> ZREVRANK courseScore tom
(integer) 1

ZINTERSTORE:计算有序集合的交集,并将结果保存起来

  语法:ZINTERSTORE destination numkeys [WEIGHTS weight weight...] [AGGREGATE SUM | MIN | MAX]

    ZADD testSortedSet1 1 a 2 b 3 c

    ZADD testSortedSet2 10 a 20 b 30 c

    ZINTERSTORE resTestSorted1 2 testSortedSet1 testSortedSet2

    ZRANGE resTestSorted1 0 -1 WITHSCORES

    ZINTERSTORE resTestSorted2 2 testSortedSet1 testSortedSet2 AGGREGATE SUM  #返回多个集合中的分数的和#

    ZRANGE resTestSorted2 0 -1 WITHSCORES  # 1+10 2+20 3+30 #

    ZINTERSTORE resTestSorted3 2 testSortedSet1 testSortedSet2 AGGREGATE MIN  #返回多个集合中分数最小的值#

    ZRANGE resTestSorted3 0 -1 WITHSCORES  # 1 2 3 #

    ZINTERSTORE resTestSorted4 2 testSortedSet1 testSortedSet2 AGGREGATE MAX  #返回多个集合中分数最小的值#

    ZRANGE resTestSorted4 0 -1 WITHSCORES  # 10 20 30 #

    ZINTERSTORE resTestSorted5 2 testSortedSet1 testSortedSet2 WEIGHTS 2 0.2  

    #返回第一个集合分数的 2 倍,第二个集合分数的 0.2倍,这里第三个参数没写默认使用 SUM 求两次重赋值的和#

    ZRANGE resTestSorted5 0 -1 WITHSCORES  # 1x2+10x0.2 2x2+20x0.2 3x2+30x0.2 #

127.0.0.1:6379> ZADD testSortedSet1 1 a 2 b 3 c
(integer) 3

127.0.0.1:6379> ZADD testSortedSet2 10 a 20 b 30 c
(integer) 3

127.0.0.1:6379> ZINTERSTORE resTestSorted1 2  testSortedSet1 testSortedSet2
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted1 0 -1
1) "a"
2) "b"
3) "c"

127.0.0.1:6379> ZRANGE resTestSorted1 0 -1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "22"
5) "c"
6) "33"

127.0.0.1:6379> ZINTERSTORE resTestSorted2 2 testSortedSet2 testSortedSet2 AGGREGATE SUM
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted2 0 -1 WITHSCORES
1) "a"
2) "20"
3) "b"
4) "40"
5) "c"
6) "60"

127.0.0.1:6379> ZINTERSTORE resTestSorted2 2 testSortedSet1 testSortedSet2 AGGREGATE SUM
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted2 0 -1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "22"
5) "c"
6) "33"

127.0.0.1:6379> ZINTERSTORE resTestSorted3 2 testSortedSet1 testSortedSet2 AGGREGATE MIN
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted3 0 -1 WITHSCORES
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"

127.0.0.1:6379> ZINTERSTORE resTestSorted4 2 testSortedSet1 testSortedSet2 AGGREGATE MAX
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted4 0 -1 WITHSCORES
1) "a"
2) "10"
3) "b"
4) "20"
5) "c"
6) "30"

127.0.0.1:6379> ZINTERSTORE resTestSorted5 2 testSortedSet1 testSortedSet2 WEIGHTS 2 0.2
(integer) 3

127.0.0.1:6379> ZRANGE resTestSorted5 0 -1 WITHSCORES
1) "a"
2) "4"
3) "b"
4) "8"
5) "c"
6) "12"

ZUNIONSTORE:计算有序集合并集,将结果保存起来

  语法:ZUNIONSTORE destination numkeys key key ... [WEIGHTS weight weight...] [AGGREGATE SUM | MIN | MAX]

    ZADD TESTUNIONSET1 1 a 2 b 3 c

    ZADD TESTUNIONSET2 4 d 5 e 6 f 7 a

    ZUNIONSTORE uniRes1 2 TESTUNIONSET1 TESTUNIONSET2  #返回多个集合的交际,这里第二个参数没写使用默认值 SUM #

    ZRANGE uniRes1 0 -1 WITHSCORES  # a=1+7 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes2 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE SUM  #返回多个集合的交际 #

    ZRANGE uniRes2 0 -1 WITHSCORES  # a=1+7 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes3 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MIN

    ZRANGE uniRes3 0 -1 WITHSCORES  # a=1 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes4 2 TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MAX

    ZRANGE uniRes4 0 -1 WITHSCORES  # a=7 b=2 c=3 d=4 e=5 f=6 #

    ZUNIONSTORE uniRes5 2 TESTUNIONSET1 TESTUNIONSET2 WEIGHTS 2 2

    ZRANGE uniRes5 0 -1 WITHSCORES  # a=1x2+7x2 b=2x2 c=3x2 d=4x2 e=5x2 f=6x2 #

127.0.0.1:6379> ZADD TESTUNIONSET1 1 a 2 b 3 c
(integer) 3

127.0.0.1:6379> ZADD TESTUNIONSET2 4 d 5 e 6 f 7 a
(integer) 4

127.0.0.1:6379> ZUNIONSTORE uniRes1 2  TESTUNIONSET1 TESTUNIONSET2
(integer) 6

127.0.0.1:6379> ZRANGE uniRes1 0 -1 WITHSCORES
 1) "b"
 2) "2"
 3) "c"
 4) "3"
 5) "d"
 6) "4"
 7) "e"
 8) "5"
 9) "f"
10) "6"
11) "a"
12) "8"

127.0.0.1:6379> ZUNIONSTORE uniRes2 2  TESTUNIONSET1 TESTUNIONSET2 AGGREGATE SUM
(integer) 6

127.0.0.1:6379> ZRANGE uniRes2 0 -1 WITHSCORES
 1) "b"
 2) "2"
 3) "c"
 4) "3"
 5) "d"
 6) "4"
 7) "e"
 8) "5"
 9) "f"
10) "6"
11) "a"
12) "8"

127.0.0.1:6379> ZUNIONSTORE uniRes3 2  TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MIN
(integer) 6

127.0.0.1:6379> ZRANGE uniRes3 0 -1 WITHSCORES
 1) "a"
 2) "1"
 3) "b"
 4) "2"
 5) "c"
 6) "3"
 7) "d"
 8) "4"
 9) "e"
10) "5"
11) "f"
12) "6"

127.0.0.1:6379> ZUNIONSTORE uniRes4 2  TESTUNIONSET1 TESTUNIONSET2 AGGREGATE MAX
(integer) 6

127.0.0.1:6379> ZRANGE uniRes4 0 -1 WITHSCORES
 1) "b"
 2) "2"
 3) "c"
 4) "3"
 5) "d"
 6) "4"
 7) "e"
 8) "5"
 9) "f"
10) "6"
11) "a"
12) "7"

127.0.0.1:6379> ZUNIONSTORE uniRes5 2  TESTUNIONSET1 TESTUNIONSET2  WEIGHTS 2 2
(integer) 6

127.0.0.1:6379> ZRANGE uniRes5 0 -1 WITHSCORES
 1) "b"
 2) "4"
 3) "c"
 4) "6"
 5) "d"
 6) "8"
 7) "e"
 8) "10"
 9) "f"
10) "12"
11) "a"
12) "16"
时间: 2024-10-06 06:22:52

Redis 的数据类型 - Zset 集合类型:有序集合的相关文章

Redis源码解析(四):redis之数据类型哈希表、列表、集合和有序集合

哈希表也是redis支持的数据结构之一,它使用REDIS_ENCODING_ZIPLIST(压缩列表) 和REDIS_ENCODING_HT(数据字典) 两种编码方式. 当哈希表使用压缩列表时,它使用如下的结构存储数据(详见ziplist.c): +---------+------+------+------+------+------+------+------+------+---------+ | ZIPLIST | | | | | | | | | ZIPLIST | | ENTRY |

Redis常用命令(三)有序集合键、HyperLogLog键

zet 有序集合,元素为string类型,元素具有唯一性,不重复.每个元素都会关联一个double类型的score,表示权重,通过权重将元素从小到大排序.没有修改操作,虽然每个元素必不相同,但是score可以相同 zadd key score1 member1 score2 member2 ...  # 添加 zadd fruits 1.0 apple 2.4 banana 4 watermelon 5 orange # 返回指定范围内的元素,索引从左侧开始,第一个元素为0, # 索引可以是负数

Redis 的数据类型 - Hash 对象类型

#Hash更容易存储对象,比如在设置用户姓名,年龄,邮箱等属性时,用string需要分别来进行设置存储,通过Hash就可以把属性放到对象中,然后再存储对象,因此相对于string类型,Hash类型存储对象可以占用更少的字节# 在配置文件中可以通过配置 hash-max-ziplist-entries 512 #存储值得最大字节512字节# hash-max-ziplist-value 64 #字段数目,默认64# HSET:将 Hash 表 key 中域 field 设置成指定的 value H

Redis的数据类型 - String字符串类型

SET:设置key对应的值为value 语法:SET key value [EX seconds] [PX milliseconds] [NX|XX] #一个键最多存储512MB,如果key存在,同名进行覆盖# EX: seconds:设置键的key的过期时间SET key value EX seconds -- SETEX PX: milliseconds:以毫秒的形式设置过期时间SET key value PX milliseconds -- PSETEX NX: 只有键不存在的时候才可以设

Redis中有序集合的常用命令有哪些?

本文和大家分享的主要是redis 中有序集合类型的常用命令,一起来看看吧,希望对大家 学习redis有所帮助. 一.有序集合类型 有序集合类型,大家从名字上应该就可以知道,实际上就是在集合类型上加了个有序而已.Redis 中的有序集合类型,实际上是在集合类型上,为每个元素都关联一个分数,有序实际上说的是分数有序,我们根据分数的范围获取集合及其他操作.集合的元素依然是不能够相同的,但是分数可以相同. 下面列举有序集合和类型和列表类型的相似处: ① 两者都是有序的(废话!) ② 两者都可以获得某一范

【Redis源码剖析】 - Redis数据类型之有序集合zset

原创作品,转载请标明:http://blog.csdn.net/Xiejingfa/article/details/51231967 这周事情比较多,原本计划每周写两篇文章的任务看来是完不成了.今天为大家带来有序集合zset的源码分析. Redis中的zset主要支持以下命令: zadd.zincrby zrem.zremrangebyrank.zremrangebyscore.zremrangebyrank zrange.zrevrange.zrangebyscore.zrevrangebys

Redis常用命令入门4:集合类型

集合类型 之前我们已经介绍过了最基本的字符串类型.散列类型.列表类型,下面我们一起学习一下集合类型. 集合类型也是体现redis一个比较高价值的一个类型了.因为Redis的集合类型,所以我们可以很容易的在Redis中执行差集运算.交集运算.并集运算. 首先我们先介绍一下集合类型和列表类型的区别,其实学过面向对象的语言的同学应该都能猜到这些类型有什么不同. ①集合类型和列表类型还是都能存储2^32-1个字符串 ②集合类型是无序的,列表类型是有序的 ③集合类型是唯一的,列表类型的值是不唯一的 下面我

API的理解和使用——有序集合

有序集合常用的命令 命令 功能 zadd key score member [score member ... ] 添加元素 zcard key 计算成员个数 zscore key member 计算某个成员分数 zrank      key member zrevrank key member 计算成员排名 zrem key member [member ...] 删除成员 zincrby key increment member 增长成员分数 zrange      key start en

Swift中的集合类型

一.引子: 在2014年10月TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人员的追捧,其热衷程度并不亚于当红巨星Taylor Swift.相信在不远的将来,swift能够平稳发展,并逐步取代Objective-C. 二.swift的集合类型 下面回归主题.作为一名iOS开发者,我们已经非常熟悉诸如NSArray,NSDictionary,NSSet等常见集合类型,以及它们的可变同类NSMutableArray,NSM