Redis operation Python series, redis basic command list | CSDN creation punch in

Posted by SsirhC on Mon, 07 Feb 2022 00:22:52 +0100

Reading guide

The list type is a simple list of strings, sorted in insertion order. Each list can store up to 232 - 1 elements (more than 4 billion). The list types mainly include the following application scenarios.

Personal use is more non paged data lists, or lists with less changes.

1. Message queue

List type lpop and rpush (or conversely, lpush and rpop) can realize the function of queue, so Redis's list type can be used to realize simple point-to-point message queue. However, I don't recommend using it in actual combat, because there are mature message queues such as Kafka, NSQ and RabbitMQ, and their functions have been perfect. Unless it is for a deeper understanding of message queues, I don't think it's necessary to build wheels again.

2. Ranking

The lrange command of list type can view the data in the queue in pages. The ranking list calculated every other period of time can be stored in the list type, such as JD's daily mobile phone sales ranking, the score ranking of students in each monthly test of the school, the anchor ranking of the year-end festival of fish fighting, etc. the following figure is the ranking list of yesterday's Golden Songs of cool dog music "K-song challenge arena". It is calculated once a day and stored in the list type. When accessing the interface, Get the golden challenge song through page and size paging. (make a small advertisement. Cool dog music "K-song challenge arena" can produce a batch of high-quality cover works every day. Friends who are interested in ordinary people's high-quality singing might as well listen to it.).

Create test class [demo11.py]

Add and read

lpush(name,values)

Add elements to the list corresponding to name, and each new element is added to the far left of the list

import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1", "Stone cutting", "Jade", "Broken dream")#Add to left
r.rpush("list1","Exterminate God")#Add to right
print(r.lrange('list1', 0, -1))#-1 represents all values

Get list length

import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1", "Stone cutting", "Jade", "Broken dream")#Add to left
r.rpush("list1","Exterminate God")#Add to right

len=r.llen("list1")
print("list The length is:",len)
print(r.lrange('list1', 0, len-1))#You can cross the border here

New (insert element at fixed index number position)

linsert(name, where, refvalue, value))

Insert a new value before or after a value in the list corresponding to name

Parameters:

  • Name - name of redis
  • where - BEFORE or AFTER
  • refvalue - benchmark value, that is, insert data before and after it
  • value - data to insert
import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1", "Stone cutting", "Jade", "Broken dream")#Add to left
r.rpush("list1","Exterminate God")#Add to right

r.linsert("list1", "after", "Jade", "Buddhas")#"Buddhas" are added to the right of "Pu Yu". If it is "befor", it is on the left
print(r.lrange("list1", 0, -1))

Modify (specify index number to modify)

r.lset(name, index, value)

Reassign an index position in the list corresponding to name

Parameters:

  • Name - name of redis
  • Index - the index position of the list
  • Value - the value to set
import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1", "Stone cutting", "Jade", "Broken dream")#Add to left
r.rpush("list1","Exterminate God")#Add to right

r.lset("list1", 2, "Buddhas")    # Change the element whose index number is 2 to "Buddhas"

print(r.lrange("list1", 0, -1))

Delete (specify a value to delete)

r.lrem(name, num, value)

Delete the specified value in the list corresponding to name

Parameters:

  • Name - name of redis
  • num - num=0, delete all specified values in the list;
  • num=2 from front to back, delete 2, num=1, from front to back, delete the first one on the left
  • num=-2 from back to front, delete 2
  • Value - the value to delete
import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1","Jade","Stone cutting", "Jade", "Broken dream","Jade","Jade","Jade","Jade","Buddhas","Jade","Jade")#Add to left
r.rpush("list1","Exterminate God")#Add to right
print(r.lrange("list1", 0, -1))

r.lrem("list1",1,"Jade")#Delete 1 from the left
print(r.lrange("list1", 0, -1))

r.lrem("list1",2,"Jade")#Delete 2 from the left
print(r.lrange("list1", 0, -1))

r.lrem("list1",-2,"Jade")#Delete 2 from the right
print(r.lrange("list1", 0, -1))

Delete and return

lpop(name)
rpop(name)

Get the first element on the left side of the list corresponding to name and remove it from the list. The return value is the first element

more:

rpop(name) indicates right to left operation

import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1","Jade","Stone cutting", "Jade", "Broken dream","Buddhas","Jade")#Add to left
r.rpush("list1","Exterminate God")#Add to right
print(r.lrange("list1", 0, -1))

one=r.lpop("list1")#Left delete
print("Left 1",one)
two=r.lpop("list1")#Left delete
print("Left 2",two)
three=r.rpop("list1")#Right delete
print("Right 1",three)
four=r.rpop("list1")#Right delete
print("Right 2",four)

print(r.lrange("list1", 0, -1))

Delete values outside the index

ltrim(name, start, end)

Remove values that are not between the start end indexes from the list corresponding to name

Parameters:

  • Name - name of redis
  • start - the starting position of the index
  • End - end of index
import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1","Jade","Stone cutting", "Jade", "Broken dream","Buddhas","Jade")#Add to left
r.rpush("list1","Exterminate God")#Add to right

print(r.lrange("list1", 0, -1))

r.ltrim("list1", 2, 4)#Save subscripts 2-4, that is, 234

print(r.lrange("list1", 0, -1))

Value (according to index number)

lindex(name, index)

Get the list elements according to the index in the list corresponding to name

import redis

r = redis.Redis(host='localhost', port=6379, decode_responses=True)
r.flushall()

r.lpush("list1","Jade","Stone cutting", "Jade", "Broken dream","Buddhas","Jade")#Add to left
r.rpush("list1","Exterminate God")#Add to right

print(r.lrange("list1", 0, -1))

print(r.lindex("list1", 2))#The subscript value is 2, which is "broken dream"

I hope it can be helpful to you. Thank you. Welcome to one click three times.

Topics: Python Redis list