Redis core knowledge of the type of list

I do not say that all api, api want this thing with his help @list on the line.

First, the characteristics

Type list ordered repeat structure where the lift is not refers to the size or number in descending order according a rule, but rather sequentially into elements.

Second, the positive and negative index

such as

lpush lp a b c d e f
# lrange的结果是f e d c b a
lrange lp 0 -1
# 得到b a
lrange lp -2 -1

Two knowledge points

  • Abcdef out into the reverse is true, because lpush used, each time to the left put.
  • lrange display element actually supports 0-1 / -2-1 this negative, precisely because of the negative index.
Types of a b c d e f
Forward Index 5 4 3 2 1 0
Negative index -1 -2 -3 -4 -5 -6

三, fire

1、lindex

1.1, the concept of

Syntax: lindex key index
find the n-th element, does not know how long the list, but want to find the last element, then the direct use of negative index lindex key -1can, looking for the penultimate element is thelindex key -1
Here Insert Picture Description

1.2 demo

Here Insert Picture Description

Positive and negative control index table, quite right.

2, lset

2.1, the concept of

The new value of the setting value of the index element. For example, I want to set the key of the last element is xxx, then
lset key -1 xxx
Here Insert Picture Description

2.2 demo

Here Insert Picture Description

3, other api

Own help @listview.

Fourth, the simulation data structure

1, analog Stack

With the command stack can be simulated, such as lpush + lpop equivalent to last out, last in first out.

2, the analog queue

Reverse command queue can be simulated, such as lpush + rpop corresponds FIFO.

3, analog array

lindex + lset which are based on the standard operation, the array function can be achieved.

4, the analog blocking unicast queue

blpop, many real-world business scenarios there is also the use of simulation blpop blocking queue, similar to the simple mq function.

V. Summary

  • Feature
  • Positive and negative index
  • Common api
  • Analog data structure
Published 29 original articles · won praise 33 · views 8427

Guess you like

Origin blog.csdn.net/ctwctw/article/details/105044581
Recommended