Skip to content

Redis 数据结构

Redis 可以存储键与 5 种不同的数据结构类型之间的映射:

  1. STRING(字符串)
  2. LIST(列表)
  3. SET(集合)
  4. HASH(散列)
  5. ZSET(有序集合)
结构类型结构存储的值结构的读写能力
STRING可以是字符串、整数或者浮点数对整个字符串或者字符串的其中一部分执行操作;
对整数和浮点数执行自增(increment)或者自减(decrement)操作
LIST一个链表,链表上的每个节点都包含了一个字符串从链表的两端推入或者弹出元素;
根据偏移量对链表进行修剪(trim);
读取单个或者多个元素;
根据值查找或者移除元素
SET包含字符串的无序收集器(unordered collection),并且被包含的每个字符串都是独一无二、各不相同的添加、获取、移除单个元素;
检查一个元素是否存在于集合中;
计算交集、并集、差集;
从集合里面随机获取元素
HASH包含键值对的无序散列表添加、获取、移除单个键值对;
获取所有键值对
ZSET字符串成员(member)与浮点数分支(score)之间的有序映射,元素的排列顺序由分支的大小决定添加、获取、移除单个元素;
根据分支范围(range)或者成员来获取元素

1. STRING (字符串)

类似于其他编程语言的字符串,但其值还可以保存 整数 和 浮点数。

命令行为
GET获取存储在给定键中的值
SET设置存储在给定键中的值
DEL删除存储在给定键中的值(这个命令可以用于所有类型
bash
localhost:0>set hello world
"OK"
localhost:0>get hello
"world"
localhost:0>del hello
"1"
localhost:0>get hello
null
localhost:0>

2. LIST (列表)

列表可以有序的存储多个字符串,其元素可以重复。
感觉类似于汇编中的队列,但是可以从两个方向推入和弹出,使用起来更加的灵活。

命令行为
RPUSH将给定值推入列表的右端
LRANGE获取列表在给定范围上的所有值
LINDEX获取列表在给定位置上的单个元素
LPOP从列表的左端弹出一个值,并返回被弹出的值
bash
localhost:0>rpush list-key item
"1"
localhost:0>rpush list-key item2
"2"
localhost:0>rpush list-key item
"3"
localhost:0>lrange list-key 0 -1
 1)  "item"
 2)  "item2"
 3)  "item"
localhost:0>lindex list-key 1
"item2"
localhost:0>lpop list-key
"item"
localhost:0>lrange list-key 0 -1
 1)  "item2"
 2)  "item"
localhost:0>

3. SET (集合)

类似于 Java 中的 Set<String>。储存一个字符串的集合,其元素不能重复。

命令行为
SADD将给定元素添加到集合
SMEMBERS返回集合包含的所有元素
SISMEMBER检查给定元素是否存在于集合中
SREM如果给定的元素存在于集合中,那么移除这个元素
bash
localhost:0>sadd set-key item
"1"
localhost:0>sadd set-key item2
"1"
localhost:0>sadd set-key item3
"1"
localhost:0>sadd set-key item
"0"
localhost:0>smembers set-key
 1)  "item3"
 2)  "item"
 3)  "item2"
localhost:0>sismember set-key item4
"0"
localhost:0>sismember set-key item
"1"
localhost:0>srem set-key item2
"1"
localhost:0>srem set-key item2
"0"
localhost:0>smembers set-key
 1)  "item3"
 2)  "item"
localhost:0>

4. HASH (散列)

储存多个键值对之间的映射。类似于 C# 中的 Dictionary<string, string>

命令行为
HSET在散列里面关联起给定的键值对
HGET获取指定散列键的值
HGETALL获取散列包含的所有键值对
HDEL如果给定键存在于散列表中,那么移除这个键
bash
localhost:0>hset hash-key sub-key1 value1
"1"
localhost:0>hset hash-key sub-key2 value2
"1"
localhost:0>hset hash-key sub-key1 value1
"0"
localhost:0>hgetall hash-key
 1)  "sub-key1"
 2)  "value1"
 3)  "sub-key2"
 4)  "value2"
localhost:0>hdel hash-key sub-key2
"1"
localhost:0>hdel hash-key sub-key2
"0"
localhost:0>hget hash-key sub-key1
"value1"
localhost:0>hgetall hash-key
 1)  "sub-key1"
 2)  "value1"
localhost:0>

5. ZSET (有序集合)

有序集合和散列一样,都用于存储键值对:有序集合的键被称为成员member),每个成员都是各不相同的;而有序集合的值则被称为分值score),分支必须为浮点数。成员根据相关联的分值进行排序,分值则根据大小进行排序。

有序集合既可以根据成员访问,也可以根据分值访问。

有点类似于 C# 中的 Dictionary<string, float> ,但 Dictionary 并没有排序的功能。

命令行为
ZADD将一个带有给定分值的成员添加到有序集合里面
ZRANGE根据元素在有序排列中所处的位置,从有序集合里面获取多个元素
ZRANGEBYSCORE获取有序集合在给定分值范围内的所有元素
ZREM如果给定成员存在于有序集合,那么移除这个成员
bash
localhost:0>zadd zset-key 728 member1
"1"
localhost:0>zadd zset-key 982 member0
"1"
localhost:0>zadd zset-key 982 member0
"0"
localhost:0>zrange zset-key 0 -1 withscores
 1)  "member1"
 2)  "728"
 3)  "member0"
 4)  "982"
localhost:0>zrangebyscore zset-key 0 800 withscores
 1)  "member1"
 2)  "728"
localhost:0>zrem zset-key member1
"1"
localhost:0>zrem zset-key member1
"0"
localhost:0>zrange zset-key 0 -1 withscores
 1)  "member0"
 2)  "982"
localhost:0>

参考

  1. 《Redis 实战》 -- Josiah L. Carlson

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.