1 String

1.01 SET

  1. SET [key] [value]:新增或更新键值
  • 键不存在,则新增键,并返回OK
  • 键存在,则更新键,并返回OK
redis > SET name "kolbe"
OK
  1. SET [key] [value] NX:新增键值
  • 键不存在,则新增成功,并返回OK
  • 键存在,则新增失败,并返回(nil)
# 首先新增一个值
redis > SET name "kolbe"
OK

# 当键存在时,则新增失败
redis > SET name "kolbe" NX
(nil)

# 当键不存在时,则新增成功
redis > SET name "kolbet" NX
OK
  1. SET [key] [value] XX:更新值
  • 值不存在,则更新失败,并返回nil
  • 值存在,则更新成功,并返回OK
# 当键不存在时,则更新失败
redis > SET name "kolbe" XX
(nil)

# 新增一个值
redis > SET name "kolbe"
OK

# 当键存在时,则更新成功
redis > SET name "kolbe" XX
OK

1.02 GET

  1. GET [key]:获取值
  • 值存在,则返回值
  • 值不存在,则返回(nil)
# 当键不存在时,则返回空
redis > GET name
(nil)

redis > SET name "kolbe"
OK

# 当键存在时,则返回对应的值
redis > GET name
"kolbe"

1.03 GETSET

  1. GETSET [key] [value]:获取旧值,并设置新值
  • 值存在,则返回旧值,并设置新值
  • 值不存在,则返回(nil),并设置新值
# 首先设置一个值
redis > SET name "kolbe"
OK

# 获取旧值,并设置新值
redis > GETSET name "kolbet"
"kolbe"

# 获取新值
redis > GET name
"kolbet"

# 获取的键不存在时,则返回(nil),并设置新值
redis > GETSET age "18"
(nil)

# 获取新设置的值
redis > GET age
"18"

1.04 MSET

  1. MSET [key] [value] ([key] [value]...):设置多个键的值
  • 键不存在,则新增键值
  • 键存在,则更新键的值
redis > MSET name "kolbe" age "18"
OK

redis > GET name
"kolbe"

redis > GET age
"18"

1.05 MGET

  1. MGET [key] ([key]...):获取多个值
  • 键存在,则返回键值
  • 键不存在,则返回(nil)
# 先设置多个键值对
redis > MSET name "kolbe" age "18"
OK

# 获取多个键值
redis > MGET name age school
1) "kolbe"
2) "18"
3) (nil)

1.06 MSETNX

  1. MSETNX [key] [value] ([key] [value]):给多个键设置值
  • 只要有一个键存在,则全部设置失败,返回(integer) 0
  • 键都不存在,则设置成功,返回(integer) 1
redis > SET name "kolbe"
OK

redis > MSETNX name "kolbe" age "18"
(integer) 0

redis > DEL name
(integer) 1

redis > MSETNX name "kolbe" age "18"
(integer) 1

redis > MGET name age
1) "kolbe"
2) "18"

1.07 STRLEN

  1. STRLEN [key]:返回指定键字符串值的长度
  • 当值存在时,返回字符串长度
  • 当值不存在时,返回(integer) 0

redis > SET name "kolbe"
OK

redis > STRLEN name
(integer) 5

redis > DEL name

redis > STRLEN name
(integer) 0

1.08 GETRANGE

  1. GETRANGE [key] [start] [end]:获取字符串值指定索引范围上的内容,其中start end为闭区间
  • 如果索引范围在字符串的长度范围内,则返回指定范围的字符串
  • 如果索引范围在字符串的长度范围外,则返回""
  • 如果索引值为正值,则从左至右取值,索引起始值为0
  • 如果索引值为负值,则从右至左取值,索引起始值为-1
redis > SET name "kolbe"
OK

redis > GETRANGE name 0 2
"ko"

redis > GETRANGE name -2 -1
"be"

redis > GETRANGE name 6 10
""

1.09 SETRANGE

  1. SETRANGE [key] [index] [value]:对字符串值的指定索引范围进行设置
redis > SET name "kolbe"
OK

redis > SETRANGE name 5 " say hello"
(integer) 15

redis > GET name
"kolbe say hello"

1.10 APPEND

  1. APPEND [key] [value]:追加新的内容值到末尾
  • 当键存在时,则将新的内容追加到值末尾
  • 当键不存在时,则将键的值初始化成空字符串,并执行追加操作
redis > SET name "kolbe"
OK

redis > APPEND name "t"
(integer) 6

redis > GET name
"kolbet"

redis > DEL name
(integer) 1

redis > APPEND name "kolbe"
(integer) 5

redis > GET name
"kolbe"

1.11 INCRBY

  1. INCRBY [key] [value]:对整数值进行加法操作
  • 当键存在时,则对键对应的数值进行加法操作
  • 当键不存在时,则将新建键值,并将值设置为0,并执行加法操作
redis > SET num "1"
OK

redis > INCRBY num 1000
(integer) 1001

1.12 DECRBY

  1. DECRBY [key] [value]:对整数值进行减法操作
  • 当键存在时,则对键对应的数值进行减法操作
  • 当键不存在时,则将新建键值,并将值设置为0,并执行减法操作
redis > SET num "1000"
OK

redis > DECRBY num 1
(integer) 999

1.13 INCR

  1. INCR [key]:对整数值进行加1操作
redis > SET num "1"
OK

redis > INCR num
(integer) 2

1.14 DECR

  1. INCR [key]:对整数值进行减1操作
redis > SET num "2"
OK

redis > DECR num
(integer) 1

1.15 INCRBYFLOAT

  1. INCRBYFLOAT [key] [value]:对浮点型数值进行加法操作
  • 当键存在时,则执行加法操作
  • 当键不存在时,则先将键的值初始化为0,再执行加法操作
  • 当键为非数字类型时,将抛异常(error) ERR value is not a valid float
  • 该指令最多保留小数点后17位,超过这个范围将被截断
redis > SET num "3.14"
OK

redis > INCRBYFLOAT "1.1"
"4.24"

redis > GET num
"4.24"

2 Hash

2.01 HSET

  1. HSET [key] [field] [value]:为散列中的指定字段设置值
  • 当key不存在或field不存在时,则执行新增操作,并返回(integer) 1
  • 当key存在且field存在时,则执行更新操作,并返回(integer) 0
redis > HSET student name "kolbe"
(integer) 1

redis > HSET student name "kolbet"
(integer) 0

2.02 HSETNX

  1. HSETNX [key] [field] [value]:为散列中的指定字段设置值,仅当字段不存在时设置成功
  • 当key不存在或field不存在时,则执行新增操作,并返回(integer) 1
  • 当key存在且field存在时,则设置失败,并返回(integer) 0
redis > DEL student
(integer) 1

redis > HSETNX student name "kolbe"
(integer) 1

redis > HSETNX student name "kolbet"
(integer) 0

redis > HGET student name
"kolbe"

2.03 HGET

  1. HGET [key] [field]:获取散列指定字段的值
  • 当key和field存在时,则返回字段的值
  • 当key或field不存在时,则返回(nil)
redis > HSET student name "kolbe"
(integer) 1

redis > HGET student name
"kolbe"

redis > DEL student
(integer) 1

redis > HGET student name
(nil)

2.04 HINCRBY

  1. HINCRBY [key] [field] [value]:为散列中的字段值执行加法操作
  • 当key且field存在时,则执行加法操作
  • 当key或field不存在时,则先将字段初始化成0,再执行加法操作
redis > HSET student age 18
(integer) 1

redis > HINCRBY student age 1
(integer) 19

redis > HGET student age
"19"

redis > DEL student

redis > HINCRBY student age 10
(integer) 10

redis > HGET student age
"10"

2.05 HINCRBYFLOAT

  1. HINCRBYFLOAT [key] [field] [value]:为散列中的字段值执行浮点加法
  • 当key且field存在时,则执行加法操作
  • 当key或field不存在时,则先将字段初始化成0,再执行加法操作
redis > HSET student age 18
(integer) 1

redis > HINCRBYFLOAT student age 1.1
(integer) 19.1

redis > HGET student age
"19.1"

redis > DEL student

redis > HINCRBYFLOAT student age 10.1
(integer) 10.1

redis > HGET student age
"10.1"

2.06 HSTRLEN

  1. HSTRLEN [key] [field]:获取散列字段值长度
  • 当key存在时,返回field对应的值长度
  • 当key不存在时,返回(integer) 0
redis > HSET student name "kolbe"
(integer) 1

redis > HSTRLEN student name
(integer) 5

redis > DEL student
(integer) 1

redis > HSTRLEN student name
(integer) 0

2.07 HEXISTS

  1. HEXISTS [key] [field]:检测散列字段是否存在
  • 当key和field存在时,返回(integer) 1
  • 当key或field不存在时,返回(integer) 0
redis > HSET student name "kolbe"
(integer) 1

redis > HEXISTS student name
(integer) 1

redis > HEXISTS student age
(integer) 0

2.08 HDEL

  1. HDEL [key] [field]:删除散列字段
  • 当key和field存在时,返回(integer) 1
  • 当key或field不存在时,返回(integer) 0
redis > HSET student name "kolbe"
(integer) 1

redis > HDEL student name
(integer) 1

redis > HGET student name
(nil)

redis > HDEL student name
(integer) 0

2.09 HLEN

  1. HLEN [key]:返回散列包含字段的数量
  • 当key存在时,返回字段的数量
  • 当key不存在时,返回(integer) 0
redis > HSET student name "kolbe"
(integer) 1

redis > HSET student age "18"
(integer) 1

redis > HLEN student
(integer) 2

redis > DEL student
(integer) 1

redis > HLEN student
(integer) 0

2.10 HMSET

  1. HMSET [key] [field] [value] ([field] [value]...):一次性设置散列中多个字段的值
  • 当key和field存在时,执行更新操作,返回OK
  • 当key或field不存在时,执行新增操作,返回OK
redis > HMSET student name "kolbe" age "18"
OK

redis > HGET student name
"kolbe"

redis > HGET student age
"18"

2.11 HMGET

  1. HMGET [key] [field] ([field]...):一次性获取散列中多个字段的值
  • 当key和field存在时,返回字段的值
  • 当key或field不存在时,返回(nil)
redis > HMSET student name "kolbe" age "18"
OK

redis > HMGET student name sex age
1) "kolbe"
2) (nil)
3) "18"

2.12 HKEYS

  1. HKEYS [key]:获取散列所有的字段列表
  • 当key存在时,返回字段列表
  • 当key不存在时,返回 (empty list or set)
redis > HMSET student name "kolbe" age "18"
OK

redis > HKEYS student
1) "name"
2) "age"

2.13 HVALS

  1. HVALS [key]:获取散列所有的字段值列表
  • 当key存在时,返回字段值列表
  • 当key不存在时,返回 (empty list or set)
redis > HMSET student name "kolbe" age "18"
OK

redis > HKVALS student
1) "kolbe"
2) "18"

2.14 HGETALL

  1. HGETALL [key]:获取散列所有的字段和值列表
  • 当key存在时,返回字段和值列表
  • 当key不存在时,返回 (empty list or set)
redis > HMSET student name "kolbe" age "18"
OK

redis > HGETALL student
1) "name"
2) "kolbe"
3) "age"
4) "18"

3 List

列表是有序的,但是允许出现重复元素

3.01 LPUSH

  1. LPUSH [key] [value] ([value]...):将元素推入队列的左端
  • 当key不存在时,则新建队列,并将元素推入队列左端,并返回当前队列的元素数量
  • 当key存在时,则直接将元素推入队列左端,并返回当前队列的元素数量
redis > LPUSH students "kolbe" "henry" "mary"
(integer) 3

redis > LRANGE student 0 10
1) "mary"
2) "henry"
3) "kolbe"

3.02 RPUSH

  1. RPUSH [key] [value] ([value]...):将元素推入队列的右端
  • 当key不存在时,则新建队列,并将元素推入队列右端,并返回当前队列的元素数量
  • 当key存在时,则直接将元素推入队列右端,并返回当前队列的元素数量
redis > RPUSH students "kolbe" "henry" "mary"
(integer) 3

redis > LRANGE student 0 10
1) "kolbe"
2) "henry"
3) "mary"

3.03 LPUSHX

  1. LPUSHX [key] [value] ([value]...):当队列不存在时,新建队列,并将元素推入队列的左端
  • 当key不存在时,则新建队列,并将元素推入队列左端,并返回当前队列的元素数量
  • 当key存在时,则操作失败,返回(integer) 0
redis > LPUSHX students "kolbe" "henry" "mary"
(integer) 3

redis > LRANGE student 0 10
1) "mary"
2) "henry"
3) "kolbe"

redis > LPUSHX students "tony" "pony"
(integer) 0

redis > LRANGE student 0 10
1) "mary"
2) "henry"
3) "kolbe"

3.04 RPUSHX

  1. RPUSHX [key] [value] ([value]...):当队列不存在时,新建队列,并将元素推入队列的右端
  • 当key不存在时,则新建队列,并将元素推入队列左端,并返回当前队列的元素数量
  • 当key存在时,则操作失败,返回(integer) 0
redis > RPUSHX students "kolbe" "henry" "mary"
(integer) 3

redis > LRANGE student 0 10
1) "kolbe"
2) "henry"
3) "mary"

redis > RPUSHX students "tony" "pony"
(integer) 0

redis > LRANGE student 0 10
1) "kolbe"
2) "henry"
3) "mary"

3.05 LPOP

  1. LPOP [key]:弹出队列的最左端元素
  • 当key存在时,则弹出队列最左端元素
  • 当key不存在则,则返回 (nil)
redis > LPUSH students "kolbe" "mary" "henry"
(integer) 3

redis > LRANGE students 0 10
1) "henry"
2) "mary"
3) "kolbe"

redis > LPOP students
"henry"

redis > DEL students
(integer) 1

redis > LPOP students
(nil)

3.06 RPOP

  1. RPOP [key]:弹出队列的最右端元素
  • 当key存在时,则弹出队列最右端元素
  • 当key不存在则,则返回 (nil)
redis > LPUSH students "kolbe" "mary" "henry"
(integer) 3

redis > LRANGE students 0 10
1) "henry"
2) "mary"
3) "kolbe"

redis > RPOP students
"kolbe"

redis > DEL students
(integer) 1

redis > RPOP students
(nil)

3.07 RPOPLPUSH

  1. RPOPLPUSH [key] [new_key]:将旧的队列中的右侧元素弹出并推到新的队列的左侧
redis > LPUSH students "kolbe" "mary" "henry"
(integer) 3

redis > LRANGE students 0 10
1) "henry"
2) "mary"
3) "kolbe"

redis > RPOPLUSH students new_students
"kolbe"

redis > LRANGE students 0 10
1) "henry"
2) "mary"

redis > LRANGE new_students 0 10
1) "henry"

3.08 LLEN

  1. LLEN [key]:获取队列的长度
  • 当key存在时,返回队列的长度
  • 当key不存在时,返回(integer) 0
redis > LPUSH students "kolbe" "mary"
(integer) 2

redis > LLEN students
(integer) 2

redis > DEL students
(integer) 1

redis > LLEN students
(integer) 0

3.09 LINDEX

  1. LINDEX [key] [value]:获取队列指定索引上的值
  • 当key存在且value没有超过队列长度时,返回索引上的值
  • 当key不存在,或value超过队列的长度时,返回(nil)
redis > LPUSH students "kolbe" "mary" "henry"
(integer) 3

redis > LINDEX students 0
"henry"

redis > LINDEX students 1
"mary"

redis > LINDEX students 2
"kolbe"

redis > LINDEX students 3
(nil)

3.10 LRANGE

  1. LRANGE [key] [start_index] [end_index]:获取队列中指定索引段的值
  • 当key存在且索引未超出队列索引范围时,返回指定索引上的值列表
  • 当key不存在或索引超出队列索引范围时,返回(empty list or set)
redis > LPUSH students "kolbe" "mary" "henry"
(integer) 3

redis > LRANGE student 0 1
1) "henry"
2) "mary"

3.11 LSET

  1. LSET [key] [index] [value]:为指定索引设置新的元素
  • 当key存在且index在范围内时,返回OK
  • 当key不存在,返回(error) ERR no such key
  • 当key存在且index超出范围时,返回(error) ERR index out of range
redis > LPUSH students "kolbe" "henry"
(integer) 2

redis > LRANGE students 0 -1
1) "henry"
2) "kolbe"

redis > LSET students 0 "mary"
OK

redis > LRANGE students 0 -1
1) "mary"
2) "kolbe"

redis > LSET students 3 "pony"
(error) ERR index out of range

redis > DEL students
(integer) 1

redis > LSET students 0 "mary"
(error) ERR no such key

3.12 LINSERT

  1. LINSERT [key] BEFORE|AFTER [old_value] [value]:将元素插入列表
redis > LPUSH students "kolbe" "pony" "henry"
(integer) 3

redis > LRANGE students 0 -1
1) "henry"
2) "pony"
3) "kolbe"

redis > LINSERT students BEFORE "pony" "tony"
(integer) 4

redis > LRANGE students 0 -1
1) "henry"
2) "tony"
3) "pony"
4) "kolbe"

3.13 LTRIM

  1. LTRIM [key] [start_index] [end_index]:修剪列表保留start ~ end之间的元素
redis > LPUSH students "kolbe" "pony" "tony" "mary"
(integer) 4

redis > LRANGE students 0 -1
1) "mary"
2) "tony"
3) "pony"
4) "kolbe"

redis > LTRIM students 1 2
OK

redis > LRANGE students 0 -1
1) "tony"
2) "pony"

3.14 LREM

  1. LREM [key] [count] [value]:从列表中移除指定元素
redis > LPUSH students "kolbe" "pony" "tony" "pony"
(integer) 4

redis > LRANGE students 0 -1
1) "pony"
2) "tony"
3) "pony"
4) "kolbe"

redis > LREM students 0 "pony"
(integer) 2

redis > LRANGE students 0 -1
1) "tony"
2) "kolbe"

3.15 BLPOP

  1. BLPOP list [list...] timeout:从列表list的左侧中阻塞弹出元素,timeout(秒)为超时时间,可以接受多个list值,当列表为空时,将阻塞等待直至超时
redis > LPUSH students "tony" "pony" 

redis > BLPOP students 10
1) "students"
2) "pony"

3.16 BRPOP

  1. BRPOP list [list...] timeout:从列表list的右侧中阻塞弹出元素,timeout(秒)为超时时间,可以接受多个list值,当列表为空时,将阻塞等待直至超时
redis > LPUSH students "tony" "pony"

redis > RPOP students 10
1) "students"
2) "tony"

4 Set

集合是无序的,但是不允许出现重复元素

4.01 SADD

  1. SADD set element [element ...]将一个或多个元素添加到集合中
redis > SADD database "redis"
(integer) 1

redis > SADD database "oracle"
(integer) 1

# 往集合里添加重复元素将被忽略
redis > SADD database "redis"
(integer) 0

4.02 SREM

  1. SREM set element [element ...]从集合中移除一个或多个元素
redis > SADD database "redis"
(integer) 1

redis > SADD database "oracle"
(integer) 1

redis > SREM database "redis"
(integer) 1

4.03 SMOVE

  1. SMOVE source_set target_set element:将元素element从source_set集合中移到target_set集合
redis > SADD database_a "redis"
(integer) 1

redis > SADD database_b "oracle"
(integer) 1

redis > SMOVE database_a database_b "redis"
(integer) 1

redis > SMEMBERS database_b
1) "oracle"
2) "redis"

4.04 SMEMBERS

  1. SMEMBERS set:获取集合中的所有元素
redis > SADD database "redis" "oracle"
(integer) 2

redis > SMEMBERS database
1) "oracle"
2) "redis"

4.05 SCARD

  1. SCARD set:获取集合中的元素数量
redis > SADD database "redis" "oracle" "mysql"
(integer) 3

redis > SCARD database
(integer) 3

4.06 SISMEMBER

  1. SISMEMBER set element:检测指定元素是否存在于集合中
redis > SADD database "redis" "oracle"
(integer) 2

redis > SISMEMBER database "redis"
(integer) 1

redis > SISMEMBER database "mysql"
(integer) 0

4.06 SRANDMEMBER

  1. SRANDMEMBER set [count]:随机获取集合中的元素,元素并不会从集合中删除
redis > SADD database "mysql" "oracle" "redis"
(integer) 3

redis > SRANDMEMBER database 2
1) "redis"
2) "oracle"

redis > SMEMBERS database
1) "redis"
2) "oracle"
3) "mysql"

4.07 SPOP

  1. SPOP set [count]:随机从集合中移除指定数量的元素,默认移除1个
redis > SADD database "mysql" "oracle" "redis"
(integer) 3

redis > SPOP database 2
1) "redis"
2) "oracle"

redis > SMEMBERS database
1) "mysql"

4.08 SINTER

  1. SINTER set [set ...]:获取给定的所有集合的交集
redis > SADD database_a "redis" "oracle"
(integer) 2

redis > SADD database_b "redis" "mysql"
(integer) 2

redis > SINTER database_a database_b
1) "redis"

4.09 SINTERSTORE

  1. SINTERSTORE target_set set [set ...]:获取给定集合的交集并存储到target_set集合中
redis > SADD database_a "redis" "oracle"
(integer) 2

redis > SADD database_b "redis" "mysql"
(integer) 2

redis > SINTERSTORE database_c database_a database_b
(integer) 1

redis > SMEMBERS database_c
1) "redis"

4.10 SUNION

  1. SUNION set [set ...]:获取给定的所有集合的并集
redis > SADD database_a "redis" "oracle"
(integer) 2

redis > SADD database_b "redis" "mysql"
(integer) 2

redis > SUNION database_a database_b
1) "redis"
2) "mysql"
3) "oracle"

4.11 SUNIONSTORE

  1. SUNIONSTORE target_set set [set ...]:获取给定集合的并集并存储到target_set集合中
redis > SADD database_a "redis" "oracle"
(integer) 2

redis > SADD database_b "redis" "mysql"
(integer) 2

redis > SUNIONSTORE database_c database_a database_b
(integer) 1

redis > SMEMBERS database_c
1) "redis"
2) "oracle"
3) "mysql"

4.12 SDIFF

  1. SDIFF set [set ...]:从左至右依次对给定集合执行差集计算,将前面得出的结果用于与后面的集合做差集
redis > SADD database_a "redis" "oracle" "mysql"
(integer) 3

redis > SADD database_b "redis" "mongo" "es"
(integer) 3

redis > SADD database_c "oracle" "hbase" "mongo"
(integer) 3

redis > SDIFF database_a database_b database_c
1) "mysql"

注:
1)使用 database_a - database_b 得出:oracle, mysql
2)使用 得出的结果集 - database_c 得出:mysql

4.13 SDIFFSTORE

  1. SDIFFSTORE taget_set set [set ...]:从左至右依次对给定集合执行差集计算,将前面得出的结果用于与后面的集合做差集并存储到target_set中
redis > SADD database_a "redis" "oracle" "mysql"
(integer) 3

redis > SADD database_b "redis" "mongo" "es"
(integer) 3

redis > SADD database_c "oracle" "hbase" "mongo"
(integer) 3

redis > SDIFFSTORE database_d database_a database_b database_c
(integer) 1

redis > SMEMBERS database_d
1) "mysql"

5 Sorted Set

有序集合同时具有“有序”和“集合”两种性质,将每一个成员和成员的分值关联,其中成员以字符串方式存储,分值以浮点数存储
有序集合中的成员不能重复,但是分值可以相同,当分值相同时,将按字母进行排序,可以使用字符+inf或者 -inf表示无穷大和无穷小

5.01 ZADD

  1. ZADD set score member [score member ...]:向有序集合中添加一个或多个成员,如果成员已存在则更新
redis > ZADD database 2 "mysql" 1 "oracle"
(integer) 2
  1. ZADD set NX score member [score member ...]:向有序集合新增成员,如果成员已存在则不操作
redis > ZADD database NX 3 "mysql"
(integer) 0
  1. ZADD set XX score member [score member ...]:向有序集合更新成员,如果成员不存在,则不操作
redis > ZADD database XX 4 "mysql"
(integer) 0
  1. ZADD set [XX|NX] CH score member [score member ...]:向有序集成更新或新增成员,并返回修改的数量
redis > ZADD database XX CH 4 "mysql"
(integer) 1

5.02 ZREM

  1. ZREM set member:从有序集合中删除元素,如果元素不存在则忽略
redis > ZADD database 3 "mysql" 4 "oracle"
(integer) 2

redis > ZREM database "mysql"
(integer) 1

5.03 ZSCORE

  1. ZSCORE set member:获取有序集合的成员分值
redis > ZADD database 3 "mysql"
(integer) 1

redis > ZSCORE database "mysql"
"3"

5.04 ZINCRBY

  1. ZINCRBY set score member:对成员的分值进行加法或减法操作
redis > ZADD database 3 "mysql"
(integer) 1

redis > ZINCRBY database 1000 "mysql"
"1003"

redis > ZSCORE database "mysql"
"1003"

5.05 ZCARD

  1. ZCARD set:获取有序集合中成员的个数
redis > ZADD database 3 "mysql" 4 "oracle"
(integer) 2

redis > ZCARD database
(integer) 2

5.06 ZRANK

  1. ZRANK set member:获取有序集合中成员的升序排名
redis > ZADD database 3 "mysql" 4 "oracle"
(integer) 2

redis > ZRANK database "mysql"
(integer) 0

5.07 ZREVRANK

  1. ZREVRANK set member:获取有序集合中成员的降序排名
redis > ZADD database 3 "mysql" 4 "oracle"
(integer) 2

redis > ZREVRANK database "mysql"
(integer) 1

5.08 ZRANGE

  1. ZRANGE set start end:获取有序集合中指定索引范围内的成员,按升序排序,其中start和end是闭区间
redis > ZADD database 1 "mysql" 2 "oracle" 3 "postgresql" 4 "hbase"
(integer) 4

redis > ZRANGE database 0 2
1) "mysql"
2) "oracle"
3) "postgresql"

5.09 ZREVRANGE

  1. ZREVRANGE set start end:获取有序集合中指定索引范围内的成员,按降序排序,其中start和end是闭区间
redis > ZADD database 1 "mysql" 2 "oracle" 3 "postgresql" 4 "hbase"
(integer) 4

redis > ZREVRANGE database 0 2
1) "postgresql"
2) "oracle"
3) "mysql"

5.10 ZCOUNT

  1. ZCOUNT set min_score max_score:统计指定分值范围内成员的数量
redis > ZADD database 1 "mysql" 2 "oracle" 3 "postgresql" 4 "hbase"
(integer) 4

redis > ZCOUNT database 0 2
(integer) 2

5.11 ZREMRANGEBYRANK

  1. ZREMRANGEBYRANK set start end:移除指定排名范围内的成员,区间为闭区间
redis > ZADD database 1 "mysql" 2 "oracle" 3 "postgresql" 4 "hbase"
(integer) 4

redis > ZREMRANGEBYRANK 0 1
(integer) 2

5.13 ZREMRANGEBYSCORE

  1. ZREMRANGEBYSCORE set min_score max_score:移除指定分值范围内的成员,区间为闭区间
redis > ZADD database 1 "mysql" 2 "oracle" 3 "postgresql" 4 "hbase"
(integer) 4

redis > ZREMRANGEBYSCORE 1 2
(integer) 2

5.14 ZUNIONSTORE

  1. ZUNIONSTORE target_set set_count source_set [source_set]:对多个有序集合进行并集运算并存储到target_set目标有序集合中
redis > ZADD database_a 1 "mysql" 2 "oracle"
(integer) 2

redis > ZADD database_b 2 "mysql" 3 "hbase"
(integer) 2

redis > ZUNIONSTORE database_c 2 database_a database_b
(integer) 3

注:
相同的元素分值将进行加法聚合操作

  1. ZUNIONSTORE target_set set_count source_set [source_set] [AGGREGATE SUM|MIN|MAX]:对多个有序集合进行并集运算并存储到target_set目标有序集合中,并指定聚合函数
redis > ZADD database_a 1 "mysql" 2 "oracle"
(integer) 2

redis > ZADD database_b 2 "mysql" 3 "hbase"
(integer) 2

redis > ZUNIONSTORE database_c 2 database_a database_b AGGREGATE MIN
(integer) 3

5.15 ZINTERSTORE

  1. ZINTERSTORE target_set set_count source_set [source_set]:对多个有序集合进行交集运算并存储到target_set目标有序集合中
redis > ZADD database_a 1 "mysql" 2 "oracle"
(integer) 2

redis > ZADD database_b 2 "mysql" 3 "hbase"
(integer) 2

redis > ZINTERSTORE database_c 2 database_a database_b
(integer) 3

注:
相同的元素分值将进行加法聚合操作

  1. ZINTERSTORE target_set set_count source_set [source_set] [AGGREGATE SUM|MIN|MAX]:对多个有序集合进行交集运算并存储到target_set目标有序集合中,并指定聚合函数
redis > ZADD database_a 1 "mysql" 2 "oracle"
(integer) 2

redis > ZADD database_b 2 "mysql" 3 "hbase"
(integer) 2

redis > ZINTERSTORE database_c 2 database_a database_b AGGREGATE MIN
(integer) 3

6 HyperLogLog

6.1 PFADD

  1. PFADD key element [element ...]:将指定元素添加到HyperLogLog中
redis > PFADD database "mysql" "database" "oracle"
(integer) 1

6.2 PFCOUNT

  1. PFCOUNT key [key ...]:计算给定key的元素基数估算值
redis > PFCOUNT database
(integer) 4

6.3 PFMERGE

  1. PFMERGE target_key source_key [source_key ...]:将多个HyperLogLog合并
redis > PFADD database_a "mysql" 

7 BITMAP

7.1 SETBIT

  1. SETBIT bitmap offset value:给指定的二进制位设置值,返回被设置的二进制旧值
redis > SETBIT bitmap 0 1
(integer) 0

7.2 GETBIT

  1. GETBIT bitmap offset value:获取指定二进制位值
redis > SETBIT bitmap 0 1
(integer) 0

redis > GETBIT bitmap 0
(integer) 1

7.3 BIGCOUNT

  1. BITCOUNT bitmap:获取指定BITMAP中被设置成1的位数数量
redis > BITSET bitmap 0 1
(integer) 0

redis > BITSET bitmap 1 1
(integer) 1

redis > BITCOUNT bitmap 
(integer) 2

7.4 BITPOS

  1. BITOPS bitmap value:查找指定位图中,第一个被设置成指定二进制值的偏移量
redis > BITSET bitmap 2 1
(integer) 0

redis > BITPOS bitmap 1
(integer) 2

7.5 BITOP

  1. BIT AND target_bitmap source_bitmap [source_bitmap ...]:对给定的一个多个位图执行二进制AND运算

  2. BIT OR target_bitmap source_bitmap [source_bitmap ...]:对给定的一个多个位图执行二进制AND运算

  3. BIT XOR target_bitmap source_bitmap [source_bitmap ...]:对给定的一个多个位图执行二进制AND运算

  4. BIT NOT target_bitmap source_bitmap [source_bitmap ...]:对给定的一个多个位图执行二进制AND运算