Home  >  Article  >  Database  >  Introduction to redis list types

Introduction to redis list types

尚
forward
2020-03-24 09:19:431697browse

Introduction to redis list types

Redis lists are simple string lists, sorted in insertion order. You can add an element to the head (left) or tail (right) of the list

Recommended: redis introductory tutorial

A list can contain up to 232 - 1 elements (4294967295, over 4 billion elements per list).

{
    'numbers': [1, 2, 3, 4, 5, 6, 7,8]
    'letters': ['a', 'b', 'c', 'd', 'e', 'f', 'g','h']
}

This is the list type. The elements in the list must be strings, and data types cannot be nested.

All operations of the list type are list operations corresponding to the key, (think about it, there should be, add, delete, insert at the specified position, delete at the specified position, pop up, cut, some in python, I think redis They should all be there)

LPUSH key value [value ...] #Add elements to the left

redis 127.0.0.1:6379> lpush numbers 1
(integer) 1
redis 127.0.0.1:6379> lpush numbers 2 3
(integer) 3

RPUSH key value [value ...] #Add data to the right

LLEN key #Get the number of elements in the list

redis 127.0.0.1:6379> llen numbers
(integer) 3

LRANGE key sart stop # Get list fragments Similar to Python's slicing function, the difference is that the returned data when slicing contains stop position data

redis 127.0.0.1:6379> lrange numbers 0 -1   #获取所有的列表内数据
"3"
"2"
"1"
redis 127.0.0.1:6379> lrange numbers 0 0
"3"
redis 127.0.0.1:6379> lrange numbers 0 5
"3"
"2"
"1"
redis 127.0.0.1:6379> lrange numbers 0 3
"3"
"2"
"1"

LPOP key # Pop an element from the left

redis 127.0.0.1:6379> lrange numbers 0 -1
"3"
"2"
"1"
redis 127.0.0.1:6379> lpop numbers
"3"
redis 127.0.0.1:6379> lrange numbers 0 -1
"2"
"1"

RPOP #Pop an element from the right The principle is the same as above

LREM key count value

# Delete the specified value in the list

#count >0 Delete the value equal to value starting from the left position of count

redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"3"
"2"
"1"
redis 127.0.0.1:6379> lrem numbers 1 3
(integer) 1
redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"2"
"1"

#count >0 Delete the value equal to value starting from the left position of count

#count=0 Delete all elements whose value is value

LINDEX key index #Get the element value of the specified index

redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"1"
redis 127.0.0.1:6379> lindex numbers 3
(nil)  #没有返回nil
redis 127.0.0.1:6379> lindex numbers 1
"1"
redis 127.0.0.1:6379> lindex numbers 0
"4"

LSET key index value # When setting the value of the specified position, you must pay attention to replacing the original position, unless the original position has no value

redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"1"
redis 127.0.0.1:6379> lset numbers 1 2
OK
redis 127.0.0.1:6379> lset numbers 1 3
OK
redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"3"

NOTE: Out of range setting is an error

redis 127.0.0.1:6379> lset numbers 2 3
(error) ERR index out of range

LTRIM key start end #Cut the original list, elements outside the specified range will be deleted

redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"3"
"2"
"1"
redis 127.0.0.1:6379> ltrim numbers 1 2
OK
redis 127.0.0.1:6379> lrange numbers 0 -1
"3"
"2"

LINSERT key before|after pivot value #To the list Insert the value. You can tell by looking at the name. Should you insert it before or after the pivot?

redis 127.0.0.1:6379> lrange numbers 0 -1
"3"
"2"
redis 127.0.0.1:6379> linsert numbers after 2 1
(integer) 3
redis 127.0.0.1:6379> lrange numbers 0 -1
"3"
"2"
"1"
redis 127.0.0.1:6379> lrange numbers 0 -1
"3"
"2"
"1"
redis 127.0.0.1:6379> linsert numbers before 3 4
(integer) 4
redis 127.0.0.1:6379> lrange numbers 0 -1
"4"
"3"
"2"
"1"

RPOPLPUSH source destination #Pop an element from the right side of the source and add it to the left side of the destination

redis> LRANGE alpha 0 -1         # 查看所有元素
"a"
"b"
"c"
"d"

redis> RPOPLPUSH alpha reciver   # 执行一次 RPOPLPUSH 看看
"d"
redis> LRANGE alpha 0 -1
"a"
"b"
"c"

redis> LRANGE reciver 0 -1
"d"

Related recommendations:

mysql video tutorial: https://www.php.cn/course/list/51.html

The above is the detailed content of Introduction to redis list types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete