Interview must be! Redis data types and data structures

One, Redis data type

  There are nine data types in the new version of Redis (version>5.0), including the commonly used basic data types, including String (string), List (list), Hash (hash), Set (collection) and Sorted Set (with Ordinal set); and four advanced data types: Bitmaps (bit set), HyperLogLogs (cardinality statistics algorithm), Geospatial Indexes (geospatial index), Streams (stream information).
  This article focuses on the five basic data types we commonly use:

1.1 String

  String is the most basic type of redis, and a key corresponds to a value. The string type is binary safe. The string can contain any data and can store up to 512MB.
Common commands

SET key value
设置指定 key 的值
GET key
获取指定 key 的值。
GETRANGE key start end
返回 key 中字符串值的子字符
GETSET key value
将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
GETBIT key offset
对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
MGET key1 [key2..]
获取所有(一个或多个)给定 key 的值。
SETBIT key offset value
对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。
SETEX key seconds value
将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)。
SETNX key value
只有在 key 不存在时设置 key 的值。
SETRANGE key offset value
用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始。
STRLEN key
返回 key 所储存的字符串值的长度。
MSET key value [key value ...]
同时设置一个或多个 key-value 对。
MSETNX key value [key value ...]
同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。
PSETEX key milliseconds value
这个命令和 SETEX 命令相似,但它以毫秒为单位设置 key 的生存时间,而不是像 SETEX 命令那样,以秒为单位。
INCR key
将 key 中储存的数字值增一。
INCRBY key increment
将 key 所储存的值加上给定的增量值(increment) 。
INCRBYFLOAT key increment
将 key 所储存的值加上给定的浮点增量值(increment) 。
DECR key
将 key 中储存的数字值减一。
DECRBY key decrement
key 所储存的值减去给定的减量值(decrement) 。
APPEND key value
如果 key 已经存在并且是一个字符串, APPEND 命令将指定的 value 追加到该 key 原来值(value)的末尾。

1.2 List

  List is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. List is often used as a message queue service to complete the message exchange between multiple programs. The list type can store up to 232-1 elements (4294967295, each list can store more than 4 billion).

BLPOP key1 [key2 ] timeout
移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOP key1 [key2 ] timeout
移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOPLPUSH source destination timeout
从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
LINDEX key index
通过索引获取列表中的元素
LINSERT key BEFORE|AFTER pivot value
在列表的元素前或者后插入元素
LLEN key
获取列表长度
LPOP key
移出并获取列表的第一个元素
LPUSH key value1 [value2]
将一个或多个值插入到列表头部
LPUSHX key value
将一个值插入到已存在的列表头部
LRANGE key start stop
获取列表指定范围内的元素
LREM key count value
移除列表元素
LSET key index value
通过索引设置列表元素的值
LTRIM key start stop
对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。
RPOP key
移除列表的最后一个元素,返回值为移除的元素。
RPOPLPUSH source destination
移除列表的最后一个元素,并将该元素添加到另一个列表并返回
RPUSH key value1 [value2]
在列表中添加一个或多个值
RPUSHX key value
为已存在的列表添加值

1.3 Hash (hash)

  Redis hash is a collection of key-value (key=>value) pairs; it is a mapping table of field and value of string type. Each hash can store 232-1 key-value pairs (more than 4 billion).

HDEL key field1 [field2]
删除一个或多个哈希表字段
HEXISTS key field
查看哈希表 key 中,指定的字段是否存在。
HGET key field
获取存储在哈希表中指定字段的值。
HGETALL key
获取在哈希表中指定 key 的所有字段和值
HINCRBY key field increment
为哈希表 key 中的指定字段的整数值加上增量 increment 。
HINCRBYFLOAT key field increment
为哈希表 key 中的指定字段的浮点数值加上增量 increment 。
HKEYS key
获取所有哈希表中的字段
HLEN key
获取哈希表中字段的数量
HMGET key field1 [field2]
获取所有给定字段的值
HMSET key field1 value1 [field2 value2 ]
同时将多个 field-value (域-值)对设置到哈希表 key 中。
HSET key field value
将哈希表 key 中的字段 field 的值设为 value 。
HSETNX key field value
只有在字段 field 不存在时,设置哈希表字段的值。
HVALS key
获取哈希表中所有值。
HSCAN key cursor [MATCH pattern] [COUNT count]
迭代哈希表中的键值对。

1.4 Set

  Set is an unordered collection of string type. Like List, the efficiency is very high when performing insertion and deletion and determining whether an element exists. The biggest advantage of sets is that they can perform intersection, union, and subtraction operations. The maximum number of members in a set is 232-1 (4294967295, each set can store more than 4 billion members).

SADD key member1 [member2]
向集合添加一个或多个成员
SCARD key
获取集合的成员数
SDIFF key1 [key2]
返回第一个集合与其他集合之间的差异。
SDIFFSTORE destination key1 [key2]
返回给定所有集合的差集并存储在 destination 中
SINTER key1 [key2]
返回给定所有集合的交集
SINTERSTORE destination key1 [key2]
返回给定所有集合的交集并存储在 destination 中
SISMEMBER key member
判断 member 元素是否是集合 key 的成员
SMEMBERS key
返回集合中的所有成员
SMOVE source destination member
将 member 元素从 source 集合移动到 destination 集合
SPOP key
移除并返回集合中的一个随机元素
SRANDMEMBER key [count]
返回集合中一个或多个随机数
SREM key member1 [member2]
移除集合中一个或多个成员
SUNION key1 [key2]
返回所有给定集合的并集
SUNIONSTORE destination key1 [key2]
所有给定集合的并集存储在 destination 集合中
SSCAN key cursor [MATCH pattern] [COUNT count]
迭代集合中的元素

1.5 Zset (Ordered Set)

  Zset, like set, is also a collection of string type elements, and duplicate members are not allowed. Zset is inserted ordered, that is, automatically sorted.
  Each element of Zset is associated with a double type score. Redis uses scores to sort the members of the collection from small to large. The members of zset are unique, but the score (score) can be repeated.

ZADD key score1 member1 [score2 member2]
向有序集合添加一个或多个成员,或者更新已存在成员的分数
ZCARD key
获取有序集合的成员数
ZCOUNT key min max
计算在有序集合中指定区间分数的成员数
ZINCRBY key increment member
有序集合中对指定成员的分数加上增量 increment
ZINTERSTORE destination numkeys key [key ...]
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
ZLEXCOUNT key min max
在有序集合中计算指定字典区间内成员数量
ZRANGE key start stop [WITHSCORES]
通过索引区间返回有序集合指定区间内的成员
ZRANGEBYLEX key min max [LIMIT offset count]
通过字典区间返回有序集合的成员
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]
通过分数返回有序集合指定区间内的成员
ZRANK key member
返回有序集合中指定成员的索引
ZREM key member [member ...]
移除有序集合中的一个或多个成员
ZREMRANGEBYLEX key min max
移除有序集合中给定的字典区间的所有成员
ZREMRANGEBYRANK key start stop
移除有序集合中给定的排名区间的所有成员
ZREMRANGEBYSCORE key min max
移除有序集合中给定的分数区间的所有成员
ZREVRANGE key start stop [WITHSCORES]
返回有序集中指定区间内的成员,通过索引,分数从高到低
ZREVRANGEBYSCORE key max min [WITHSCORES]
返回有序集中指定分数区间内的成员,分数从高到低排序
ZREVRANK key member
返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
ZSCORE key member
返回有序集中,成员的分数值
ZUNIONSTORE destination numkeys key [key ...]
计算给定的一个或多个有序集的并集,并存储在新的 key 中
ZSCAN key cursor [MATCH pattern] [COUNT count]
迭代有序集合中的元素(包括元素成员和元素分值)

Two, Redis data structure

imgimg

2.1 Simple dynamic string

  Redis does not use the string of the C language, but constructs its own simple dynamic string (SDS) abstract type to represent the string. SDS has the following advantages over C strings:

  • Because SDS records len, all strlen obtains the string length O(1), and traverses O(n) in C.
  • SDS prevents buffer overflow
    . Strings in C that do not record the length can easily cause buffer overflow. When the SDS API modifies the SDS, the API first checks whether the space is enough, and the space is not automatically expanded before modifying (automatically avoid buffer overflow)
  • Reduce the number of memory redistributions caused by modifying strings.
    SDS touches the relationship between the length of the string and the length of the underlying array through unused space. With the free attribute, the length of the buf array is not necessarily +1, because it can contain Unused bytes. Unused space helps SDS realize the following two optimization strategies:
    • Space pre-allocation (increment) is
      used to optimize SDS string growth operations
    • Lazy space release (minus) is
      used to optimize SDS string shortening operations
  • Binary security
    SDS processes the data stored in the buf array without any changes, so the buf attribute is called a byte array —> redis does not use the buf byte array to store characters, but to store binary data.
    ​ 1. Save binary data , The stored data does not change.
    2. Use the len attribute instead of an empty string or judge whether the string ends

2.2 Doubly linked list

  The doubly linked list is read and written sequentially, accessed element by element through the array subscript or the pointer of the linked list. The operation complexity is basically O(N), and the operation efficiency is relatively low; the doubly linked list will record the offset between the head and the end of the table. Adding or deleting elements at the head and tail of the list can be directly positioned by offset, so their complexity is only O(1), which can achieve fast operation.

2.3 Compressed list

  The compressed list is actually similar to an array, and each element in the array stores a piece of data. Unlike the array, the compressed list has three fields zlbytes, zltail and zllen in the header, which represent the length of the list, the offset at the end of the list, and the number of entries in the list; the compressed list also has a zlend at the end of the list, which means End of the list. In the compressed list, if we want to find and locate the first element and the last element, we can directly locate it by the length of the three fields in the header, and the complexity is O(1). When searching for other elements, it is not so efficient. You can only search one by one. At this time, the complexity is O(N).

2.4 Hash table

  In Redis, key-value pairs are stored through hash tables. A hash table is an array, and each element in the array is called a hash bucket. Each hash bucket stores key-value pair data. The elements in the hash bucket are not the value itself, but the pointer to the specific value.
The biggest advantage of hash tables is that you can quickly find key-value pairs with O(1) time complexity.
  When the hash table stores a large amount of data, the hash table will have conflict problems and possible operation blockage caused by rehash. The way Redis resolves hash conflicts is chain hashing. Chained hashing means that multiple elements in the same hash bucket are stored in a linked list, and they are connected by pointers in turn.
  Redis uses rehash to solve the problem of too long hash list. Rehash is to increase the number of existing hash buckets, so that gradually increasing entry elements can be distributed among more buckets, reducing the number of elements in a single bucket, thereby reducing conflicts in a single bucket.
  Redis uses two global hash tables by default, which are used in polling. The rehash process is divided into three steps:

  • Assign more space to hash table 2
  • Remap and copy the data in hash table 1 to hash table 2
  • Free up space in hash table 1

2.5 Jump table

  The ordered linked list can only find the elements one by one, which makes the operation very slow, so there is a jump list. Specifically, the jump list adds a multi-level index on the basis of the linked list, and realizes rapid data positioning through several jumps of the index position. When the amount of data is large, the search complexity of the jump table is O(logN).

2.6 Integer array

  Integer arrays are read and written sequentially, accessed element by element through array subscripts or linked list pointers. The operation complexity is basically O(N), and the operation efficiency is relatively low. In integer array data structures, these structures specifically record the number of elements. Statistics, so related operations can be completed efficiently.

Guess you like

Origin blog.csdn.net/qq_42979842/article/details/108248590