redis data type-list (List)

Novice tutorial: https://www.runoob.com/redis/redis-lists.html

Introduction

The Redis 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.
Its bottom layer uses a doubly linked list, so the operation performance on both ends is very high, and the performance of the operation node through the index subscript will be poor.

Insert picture description here

Common commands

# 从左边/右边插入一个或多个值。
lpush/rpush  <key> <value1> <value2> <value3> ....

# 从左边/右边吐出一个值。值在键在,值光键亡。
lpop/rpop  <key>

# 列表右边吐出一个值,插到<key2>列表左边
rpoplpush  <key1> <key2>

# 按照索引下标遍历列表(从左到右)
lrange <key> <start> <stop>    0 到 -1表示取全部元素

# 按照索引下标获得指定index元素(从左到右)
lindex <key> <index>

# 获得列表长度
llen <key>

# 在<value>的左边插入<newvalue>
linsert  <key>  before <value> <newvalue>

# 从左往右数n个元素范围中删掉所有值为value的元素
lrem <key> <n> <value>

# 将列表key下标为index的值替换成value
lset <key> <index> <value>

data structure

The data structure of List is quickList.

First, when there are fewer list elements, a contiguous memory storage is used. This structure is a ziplist, which is a compressed list.

It stores all the elements next to each other, and allocates a piece of contiguous memory.

When the amount of data is relatively large, it will be changed to quicklist.

Because the additional pointer space required by the ordinary linked list is too large, it will waste space. For example, only int type data is stored in this list, and two additional pointers prev and next are required in the structure.

Insert picture description here

Redis combines linked lists and ziplists to form a quicklist. That is, multiple ziplists are stringed together using two-way pointers. This not only satisfies the fast insertion and deletion performance, but also does not appear too much space redundancy.