Redis五种数据结构 - 由浅到深,一文讲懂

309 阅读16分钟

今天复习Redis,把五种数据结构及各种数据结构的用途梳理了一遍,在这里记录一下,看看有没有你不知道的吧

一、字符串 - string

是 Redis 五种数据类型中最基本的数据类型,一个 key 对应一个 value;

常用命令

127.0.0.1:6379> set wangxiaoer wangxiaoer  // 存入字符串键值对
OK
127.0.0.1:6379> mset wang wang xiao xiao er er  // 批量存入字符串键值对
OK
127.0.0.1:6379> keys *  // 获取所有键
1) "er"
2) "xiao"
3) "wang"
4) "wangxiaoer"
127.0.0.1:6379> setnx wang wang  // 存入一个不存在的键值对(存在的话则不保存)
(integer) 0
127.0.0.1:6379> keys * 
1) "er"
2) "xiao"
3) "wang"
4) "wangxiaoer"
127.0.0.1:6379> SETNX wangwang wangwang  //  存入一个不存在的键值对(不存在的话则保存)
(integer) 1
127.0.0.1:6379> keys *
1) "er"
2) "xiao"
3) "wangwang"
4) "wang"
5) "wangxiaoer"
127.0.0.1:6379> get wangxiaoer  // 获取一个字符串键值
"wangxiaoer"
127.0.0.1:6379> mget wang xiao er  // 批量获取字符串键值
1) "wang"
2) "xiao"
3) "er"
127.0.0.1:6379> EXPIRE wangwang 5  // 设置某个键值过期时间(过期时间单位 /秒)
(integer) 1
127.0.0.1:6379> keys *  // 此时还未过期
1) "er"
2) "xiao"
3) "wangwang"
4) "wang"
5) "wangxiaoer"
127.0.0.1:6379> keys *  // 此时 key 为 wangwang 的键值对已过期
1) "er"
2) "xiao"
3) "wang"
4) "wangxiaoer"
127.0.0.1:6379> set wangwangwang 1
OK
127.0.0.1:6379> keys *
1) "er"
2) "xiao"
3) "wang"
4) "wangwangwang"
5) "wangxiaoer"
127.0.0.1:6379> INCR wangwangwang  // 为键存储的对应的值 +1
(integer) 2
127.0.0.1:6379> INCR wangwangwang
(integer) 3
127.0.0.1:6379> get wangwangwang
"3"
127.0.0.1:6379> INCRBY wangwangwang 5  // 为键存储的对应的数字 +5
(integer) 8
127.0.0.1:6379> get wangwangwang
"8"
127.0.0.1:6379> INCR wangxiaoer  // 键存储的值不是数字不能进行添加
(error) ERR value is not an integer or out of range
127.0.0.1:6379> DECR wangwangwang   // 为键存储的对应的值 -1
(integer) 7
127.0.0.1:6379> get wangwangwang
"7"
127.0.0.1:6379> DECRBY wangwangwang 5  // 为键存储的对应的值 -5
(integer) 2
127.0.0.1:6379> get wangwangwang
"2"
127.0.0.1:6379> help @string  // 通过help + @数据类型 获取该数据类型的命令

  APPEND key value  // 对某个key 存储的值进行追加(对存储的数字类型、字符串类型的值都可以进行追加)
  summary: Append a value to a key
  since: 2.0.0

  BITCOUNT key [start end]  // 获取key中存储的值转化为二进制码中偏移位范围内为二进制码为1 的个数
  summary: Count set bits in a string
  since: 2.6.0

  BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
  summary: Perform arbitrary bitfield integer operations on strings
  since: 3.2.0

  BITOP operation destkey key [key ...]
  summary: Perform bitwise operations between strings
  since: 2.6.0

  BITPOS key bit [start] [end]
  summary: Find first bit set or clear in a string
  since: 2.8.7

  DECR key  // key 存储的数字减1
  summary: Decrement the integer value of a key by one
  since: 1.0.0

  DECRBY key decrement  // key 存储的数字减 decrement(是数字)
  summary: Decrement the integer value of a key by the given number
  since: 1.0.0
   GET key
  summary: Get the value of a key
  since: 1.0.0

  GETBIT key offset
  summary: Returns the bit value at offset in the string value stored at key
  since: 2.2.0

  GETRANGE key start end  // 获取 key 存储的值长度从 start 开始 end 结束的字符串
  summary: Get a substring of the string stored at a key
  since: 2.4.0

  GETSET key value  // 获取存在的键值并设置新的值
  summary: Set the string value of a key and return its old value
  since: 1.0.0

  INCR key  // key 存储的数字加1
  summary: Increment the integer value of a key by one
  since: 1.0.0

  INCRBY key increment  // key 存储的数字加 increment(为数字)
  summary: Increment the integer value of a key by the given amount
  since: 1.0.0

  INCRBYFLOAT key increment  // key 存储的数字加 increment(为浮点数)
  summary: Increment the float value of a key by the given amount
  since: 2.6.0

  MGET key [key ...]  // 批量获取多个键的值
  summary: Get the values of all the given keys
  since: 1.0.0

  MSET key value [key value ...]  // 批量设置多个键值对
  summary: Set multiple keys to multiple values
  since: 1.0.1

  MSETNX key value [key value ...]  // 批量设置多个不存在的键值对(具有原子性)
  summary: Set multiple keys to multiple values, only if none of the keys exist
  since: 1.0.1

  PSETEX key milliseconds value  // 设置有过期时间的键值对
  summary: Set the value and expiration in milliseconds of a key
  since: 2.6.0
  SET key value [expiration EX seconds|PX milliseconds] [NX|XX]  // 设置有过期时间的键值对
  summary: Set the string value of a key
  since: 1.0.0

  SETBIT key offset value
  summary: Sets or clears the bit at offset in the string value stored at key
  since: 2.2.0

  SETEX key seconds value  // 添加不存在的键值对并设置过期时间
  summary: Set the value and expiration of a key
  since: 2.0.0

  SETNX key value  // 添加不存在的键值对
  summary: Set the value of a key, only if the key does not exist
  since: 1.0.0

  SETRANGE key offset value  // 替换指定范围内的字符串为指定的值
  summary: Overwrite part of a string at key starting at the specified offset
  since: 2.2.0

  STRLEN key  // 获取指定键的值的长度
  summary: Get the length of the value stored in a key
  since: 2.2.0

常用场景: 单值缓存:可以保存字符串、数字、图片信息、网址等等信息; 对象缓存:保存一个对象的json格式数据,或者保存一个对象各个属性的值

set user:1 value(json格式的数据)
mset user:1:name wangxiaoer user:1:age 18
mget user:1:name user:1:age

简单的分布式锁:

setnx user:1 true  // 设置成功返回1,设置失败返回0,
执行业务代码  // 上面的设置成功后执行
del user:1  // 执行成功后删除锁(key)
为了防止死锁,在设置时还可以设置键值对的过期时间,过期释放

计数器:微信公众号文章被阅读、点赞、在看的次数

incrby article:xxxx:read  // 阅读
incrby article:xxxx:praise  // 点赞
incrby article:xxxx:share  // 在看

分布式系统全局序列号:获取后放在本地内存进行使用,使用完毕后再进行获取

incrby serialUID 1000  // 一次性多取一些值,节省redis的性能

二、哈希 - hash

键值是一个map,值本身也是一个map,具体结构如下:

常用命令 命令都是以h开头:

127.0.0.1:6379> hset user name wangxiaoer  // 存储一个hash表key的键值
(integer) 1
127.0.0.1:6379> hsetnx user name wangxiaoer  // 存储一个不存在的hash表key的键值
(integer) 0
127.0.0.1:6379> hset user age 18
(integer) 1
127.0.0.1:6379> hset user gender 男
(integer) 1
127.0.0.1:6379> hget user name  // 获取hash表中key对应的filed的值
"wangxiaoer"
127.0.0.1:6379> HSETNX user1 name xiaoer
(integer) 1
127.0.0.1:6379> HMSET user1 age 18 gender 男
OK
127.0.0.1:6379> hlen user
(integer) 3
127.0.0.1:6379> HGETALL user  // 获取hash表key中所有的值
1) "name"
2) "wangxiaoer"
3) "age"
4) "18"
5) "gender"
6) "\xe7\x94\xb7"
127.0.0.1:6379> HINCRBY user age 12 
(integer) 30
127.0.0.1:6379> hmset user 1:name wang 1:age 18 2:name xiao 2:age 19  // 批量设置
OK
127.0.0.1:6379> HGETALL user
1) "1:name"
2) "wang"
3) "1:age"
4) "18"
5) "2:name"
6) "xiao"
7) "2:age"
8) "19"
127.0.0.1:6379> Help @hash  // 通过help + @数据类型 获取该数据类型的命令

  HDEL key field [field ...]  // 批量删除hash表中key中filed对应的键值
  summary: Delete one or more hash fields
  since: 2.0.0

  HEXISTS key field  // 判断hash表中的filed是否存在对应的键值
  summary: Determine if a hash field exists
  since: 2.0.0

  HGET key field  // 获取hash表中的filed对应的键值
  summary: Get the value of a hash field
  since: 2.0.0

  HGETALL key  // 获取hash表key中所有的值
  summary: Get all the fields and values in a hash
  since: 2.0.0

  HINCRBY key field increment  // 对hash表中的filed对应的数字键值添加 increment
  summary: Increment the integer value of a hash field by the given number
  since: 2.0.0

  HINCRBYFLOAT key field increment  // 对hash表中的filed对应的数字键值添加 increment(浮点数)
  summary: Increment the float value of a hash field by the given amount
  since: 2.6.0

  HKEYS key  // 获取hash表中所有的filed
  summary: Get all the fields in a hash
  since: 2.0.0

  HLEN key  // 获取hash表中filed 的数量
  summary: Get the number of fields in a hash
  since: 2.0.0
  HMGET key field [field ...]  // 批量获取hash表中的filed对应的键值
  summary: Get the values of all the given hash fields
  since: 2.0.0

  HMSET key field value [field value ...]  // 批量设置hash表中的filed对应的键值
  summary: Set multiple hash fields to multiple values
  since: 2.0.0

  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate hash fields and associated values
  since: 2.8.0

  HSET key field value  // 设置hash表中的filed对应的键值
  summary: Set the string value of a hash field
  since: 2.0.0

  HSETNX key field value  // 设置hash表中不存在的filed对应的键值
  summary: Set the value of a hash field, only if the field does not exist
  since: 2.0.0

  HSTRLEN key field  // 获取hash表中filed 的键值的长度
  summary: Get the length of the value of a hash field
  since: 3.2.0

  HVALS key  // 获取hash表key中所有的值(不包括filed)
  summary: Get all the values in a hash
  since: 2.0.0

常用场景

对象存储:

hmset user 1:name wangxiaoer 1:age 18

电商购物车:

以用户id为key 以商品信息为field 以商品的数量为value

购物车操作如下:
添加商品:hset cart:1001 10086 1
增加商品数量:hincrby cart:1001 10086 1
获取商品总数:hlen cart:1001
删除商品:hdel cart:1001 10086
获取购物车中的所有商品:hgetall cart:10086

hash 与 string 的比较 优点:

同类数据归类整合存储,方便数据管理;
相比 string 操作消耗内存与 CPU 更小;
相比 string 存储更节省空间;

缺点:

过期功能只能作用在key上,不能使用在field上;
Redis 集群架构下不适合大规模使用(会出现数据倾斜);

三、列表 - list

其实就是链表,而且是双端链表,表中的元素是有序的,切元素是可以重复的,表的左右两端都可以进行插入和删除操作; 常用命令 命令通常是以l开头:

127.0.0.1:6379> LPUSH wang 12 23 34 45 56 56 57  // 将一个或者多个value插入到key列表的表头(左侧)
(integer) 7
127.0.0.1:6379> RPUSH wang 75 75 65 54 43 32 21  // 将一个或者多个value插入到key列表的表尾(右侧)
(integer) 14
127.0.0.1:6379> LPOP wang  // 从表头取出一个元素
"57"
127.0.0.1:6379> RPOP wang  // 从表头尾出一个元素
"21"
127.0.0.1:6379> BLPOP wang 5  // 从表头取出一个元素,若列表中无元素则阻塞指定时间
1) "wang"
2) "56"
127.0.0.1:6379> BRPOP wang 5  // 从表尾取出一个元素,若列表中无元素则阻塞指定时间
1) "wang"
2) "32"
127.0.0.1:6379> LRANGE wang 0 -1  // 取出指定范围内的元素(表头为下标为0,表尾为-1)
 1) "56"
 2) "45"
 3) "34"
 4) "23"
 5) "12"
 6) "75"
 7) "75"
 8) "65"
 9) "54"
10) "43"
127.0.0.1:6379> help @list

  BLPOP key [key ...] timeout
  summary: Remove and get the first element in a list, or block until one is available
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: Remove and get the last element in a list, or block until one is available
  since: 2.0.0

  BRPOPLPUSH source destination timeout  // 从一个链表的表尾取出一个元素插入另一个链表的表头,如果取不到则阻塞指定时间
  summary: Pop a value from a list, push it to another list and return it; or block until one is available
  since: 2.2.0

  LINDEX key index  // 获取链表指定下标的元素
  summary: Get an element from a list by its index
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot value  // 在指定元素的前方或者后方插入一个value
  summary: Insert an element before or after another element in a list
  since: 2.2.0
  
  LLEN key  // 获取列表的长度
    summary: Get the length of a list
  since: 1.0.0

  LPOP key
  summary: Remove and get the first element in a list
  since: 1.0.0

  LPUSH key value [value ...]
  summary: Prepend one or multiple values to a list
  since: 1.0.0

  LPUSHX key value  // 向已存在的列表中添加元素value
  summary: Prepend a value to a list, only if the list exists
  since: 2.2.0

  LRANGE key start stop  // 获取链表指定范围内的元素(超出范围则取出范围内存在的全部元素)
  summary: Get a range of elements from a list
  since: 1.0.0

  LREM key count value  // 移除链表中指定数量的指定值
  summary: Remove elements from a list
  since: 1.0.0

  LSET key index value  // 将链表中指定下标的值设置为指定值(超出范围则报错)
  summary: Set the value of an element in a list by its index
  since: 1.0.0

  LTRIM key start stop  // 移除链表中指定范围内的所有元素
  summary: Trim a list to the specified range
  since: 1.0.0

  RPOP key
  summary: Remove and get the last element in a list
  since: 1.0.0

  RPOPLPUSH source destination    // 从一个链表的表尾取出一个元素插入另一个链表的表头
  summary: Remove the last element in a list, prepend it to another list and return it
  since: 1.2.0

  RPUSH key value [value ...]
  summary: Append one or multiple values to a list
  since: 1.0.0

常用场景 常用的数据结构:

Stack(栈):LPUSH + LPOP
Queue(队列):LPUSH + RPOP
Blocking Queue(阻塞队列):LPUSH + BRPOP

微博或者订阅号的消息流(栈): 先收到的消息放在后面,后收到(最新)的消息排在最前面

四、集合 - set

和 list 一样的是也存储多个元素,不同的是:元素不能重复,元素是无序的,支持集合间操作(交集、并集、差集) 常用命令

127.0.0.1:6379> SADD wang 12 12 12 12 23 34 45 56  // 向集合中添加一个或者多个元素(重复的元素会去除)
(integer) 5
127.0.0.1:6379> SREM wang 12  // 从集合中移除某个元素
(integer) 1
127.0.0.1:6379> SMEMBERS wang  // 获取指定集合中的所有元素
1) "23"
2) "34"
3) "45"
4) "56"
127.0.0.1:6379> SCARD wang  // 获取集合中元素的数量
(integer) 4
127.0.0.1:6379> SISMEMBER wang 12  // 判断元素是否在集合中
(integer) 0
127.0.0.1:6379> SISMEMBER wang 23
(integer) 1
127.0.0.1:6379> SRANDMEMBER wang 2  // 获取集合中指定数量的元素(无序)
1) "34"
2) "23"
127.0.0.1:6379> SRANDMEMBER wang 2
1) "45"
2) "23"
127.0.0.1:6379> SPOP wang 2  // 取出集合中指定数量的元素(无序)
1) "45"
2) "34"
127.0.0.1:6379> SADD xiaoer 12 23 34 56 67 78 
(integer) 6
127.0.0.1:6379> SINTER wang xiaoer  // 对两个集合取交集
1) "23"
2) "56"
127.0.0.1:6379> SINTERSTORE wangxiaoer wang xiaoer  // 对两个集合取交集并存储为新的集合
(integer) 2
127.0.0.1:6379> SRANDMEMBER wangxiaoer 3
1) "23"
2) "56"
127.0.0.1:6379> SUNION wang xiaoer  // 对两个集合取并集
1) "12"
2) "23"
3) "34"
4) "56"
5) "67"
6) "78"
127.0.0.1:6379> SDIFF wang xiaoer  // 对两个集合取差集
(empty list or set)
127.0.0.1:6379> help @set

  SADD key member [member ...]
  summary: Add one or more members to a set
  since: 1.0.0

  SCARD key
  summary: Get the number of members in a set
  since: 1.0.0

  SDIFF key [key ...]
  summary: Subtract multiple sets
  since: 1.0.0

  SDIFFSTORE destination key [key ...]  // 将两个集合的差集存储为新的集合
  summary: Subtract multiple sets and store the resulting set in a key
  since: 1.0.0

  SINTER key [key ...]
  summary: Intersect multiple sets
  since: 1.0.0

  SINTERSTORE destination key [key ...]
  summary: Intersect multiple sets and store the resulting set in a key
  since: 1.0.0

  SISMEMBER key member
  summary: Determine if a given value is a member of a set
  since: 1.0.0

  SMEMBERS key
  summary: Get all the members in a set
  since: 1.0.0

  SMOVE source destination member  // 将集合中的元素移动到另一个集合中
  summary: Move a member from one set to another
  since: 1.0.0

  SPOP key [count]
  summary: Remove and return one or multiple random members from a set
  since: 1.0.0

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0

  SREM key member [member ...]
  summary: Remove one or more members from a set
  since: 1.0.0

  SSCAN key cursor [MATCH pattern] [COUNT count]  // 对集合中的元素匹配指定数量指定格式的元素
  summary: Incrementally iterate Set elements
  since: 2.8.0

  SUNION key [key ...]
  summary: Add multiple sets
  since: 1.0.0

  SUNIONSTORE destination key [key ...]  // 取两个集合的交集并存储为新的集合
  summary: Add multiple sets and store the resulting set in a key
  since: 1.0.0

常用场景 抽奖小程序:

将参与的人员添加到抽奖池中:SADD lottery wangxiaoer
查看奖池中人员信息:SMEMBERS lottery
从奖池中抽2人(人员不移除):SRANDMEMBER lottery 2
从奖池中抽2人(人员移除):SPOP lottery 2

微信点赞标签:

点赞:SADD like:article001 userId
取消点赞:SREM like:article001 userId
检查用户是否点过赞:SISMEMBER like:article001 userId
获取点赞的用户列表:SMEMBERS  like:article001
获取点赞用户数:SCARD  like:article001

微博关注模型:

获取 wangxiaoer 关注的人:SMEMBERS wangxiaoer
获取 gaoyuanyuan 关注的人:SMEMBERS gaoyuanyuan
获取 zhangziyi 关注的人:SMEMBERS zhangziyi
获取 wangxiaoer 和 gaoyuanyuan 共同关注的人:SINTER wangxiaoer gaoyuanyuan
获取 wangxiaoer 关注的也关注 gaoyuanyuan 的人:SISMEMNER zhangziyi gaoyuanyuan
获取 wangxiaoer 可能认识的人:SDIFF gaoyuanyuan wangxiaoer

商品筛选:

添加品牌:SADD brand:huawei mate40
添加新旧:SADD oldnew:new mate40
添加尺寸:SADD size:6.1 mate40
获取华为新的6.1存的手机:SINTER brand:huawei oldnew:new size:6.1

五、有序集合 - zset

有序集合是一种特殊的集合,保留了集合中元素不能重复的属性,区别是集合中的元素是可以排序的; 常用命令 命令通常以z开头:

127.0.0.1:6379> ZADD wang 1 wang 2 xiao 3 er  // 向集合中添加指定分值的值
(integer) 3
127.0.0.1:6379> ZREM wang wang  // 移除集合中指定的元素
(integer) 1
127.0.0.1:6379> ZSCORE wang xiao  // 获取集合中指定的元素的分值
"2"
127.0.0.1:6379> ZINCRBY wang 3 xiao  // 对集合中指定元素添加指定的数值
"5"
127.0.0.1:6379> ZCARD wang  // 获取集合中元素的数量
(integer) 2
127.0.0.1:6379> ZRANGE wang 1 2  // 获取集合中指定范围内的元素(正序)
1) "xiao"
127.0.0.1:6379> ZREVRANGE wang 0 2  // 获取集合中指定范围内的元素(倒序)
1) "xiao"
2) "er"
127.0.0.1:6379> ZADD xiao 3 xiao 5 er
(integer) 2
127.0.0.1:6379> ZINTERSTORE wangxiao 2 wang xiao  // 获取两个集合中元素的交集并存储
(integer) 2
127.0.0.1:6379> ZRANGE wangxiao 0 -1
1) "er"
2) "xiao"
127.0.0.1:6379> help @sorted_set

  BZPOPMAX key [key ...] timeout  // 从一个或者多个集合中移除分数最高的元素,如果没有则阻塞(指定时间)
  summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  BZPOPMIN key [key ...] timeout  // 从一个或者多个集合中移除分数最低的元素,如果没有则阻塞(指定时间)
  summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
  since: 5.0.0

  ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  summary: Add one or more members to a sorted set, or update its score if it already exists
  since: 1.2.0

  ZCARD key
  summary: Get the number of members in a sorted set
  since: 1.2.0

  ZCOUNT key min max  // 统计集合中指定分数范围内的元素数量
  summary: Count the members in a sorted set with scores within the given values
  since: 2.0.0

  ZINCRBY key increment member  // 对集合中指定元素增加指定的数值
  summary: Increment the score of a member in a sorted set
  since: 1.2.0

  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

  ZLEXCOUNT key min max  // 统计集合中指定词典范围内的元素数量
  summary: Count the number of members in a sorted set between a given lexicographical range
  since: 2.8.9

  ZPOPMAX key [count]  // 移除指定集合中移除指定数量最高分数的值
  summary: Remove and return members with the highest scores in a sorted set
  since: 5.0.0

  ZPOPMIN key [count]  // 移除指定集合中移除指定数量最低分数的值
  summary: Remove and return members with the lowest scores in a sorted set
  since: 5.0.0

  ZRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index
  since: 1.2.0

  ZRANGEBYLEX key min max [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range
  since: 2.8.9

  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score
  since: 1.0.5

  ZRANK key member
  summary: Determine the index of a member in a sorted set
  since: 2.0.0

  ZREM key member [member ...]
  summary: Remove one or more members from a sorted set
  since: 1.2.0

  ZREMRANGEBYLEX key min max
  summary: Remove all members in a sorted set between the given lexicographical range
  since: 2.8.9

  ZREMRANGEBYRANK key start stop
  summary: Remove all members in a sorted set within the given indexes
  since: 2.0.0

  ZREMRANGEBYSCORE key min max
  summary: Remove all members in a sorted set within the given scores
  since: 1.2.0

  ZREVRANGE key start stop [WITHSCORES]
  summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
  since: 1.2.0
  
  ZREVRANGEBYLEX key max min [LIMIT offset count]
  summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
  since: 2.8.9

  ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
  summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
  since: 2.2.0

  ZREVRANK key member
  summary: Determine the index of a member in a sorted set, with scores ordered from high to low
  since: 2.0.0

  ZSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Incrementally iterate sorted sets elements and associated scores
  since: 2.8.0

  ZSCORE key member
  summary: Get the score associated with the given member in a sorted set
  since: 1.2.0

  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
  summary: Add multiple sorted sets and store the resulting sorted set in a new key
  since: 2.0.0

常用场景 排行榜:

点击新闻:ZADD hostnews:20201108 1 telangpu
获取某天排行前十的新闻:ZREVRANGE hostnews:20201108 0 9 WITHSCORES

备注: redis的key的类型为SDS(Simple Dynamic String:简单的动态字符串),该key值的长度会动态扩充,容量不够时长度会翻倍(达到1Mb时,每次扩容增加1Mb);


眼见为虚,记录为实,将看到的转换为自己的,加油!!!