Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of List Operation of redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the redis List operation case analysis, I believe most people still do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it together!

List operation: List in redis is stored in memory according to a name corresponding to a List. As shown in the figure:

1、lpush(name,values)

#Add elements to the list corresponding to name, and each new element is added to the leftmost part of the list #For example: # r.lpush('oo', 11,22,33) #Save order: 33, 22, 11 #Expand: # rpush(name, values) indicates right-to-left operation

2、lpushx(name,value)

#Add an element to the list corresponding to name, only if name already exists, the value is added to the far left of the list #More: # rpushx(name, value) indicates operation from right to left

3、llen(name)

#Number of list elements corresponding to name

4、linsert(name, where, refvalue, value))

#Insert a new value before or after a value in the list corresponding to name #Parameters: # name, redis name # where, BEFORE or AFTER(lowercase also possible) # refvalue, benchmark value, i.e. insert data before and after it (if there are multiple benchmark values, the first one found shall prevail) # value, data to insert

5\r.lset(name, index, value)

#Reassign an index position in the list corresponding to name #Parameters: # name, redis name # index, index position of list # value, the value to be set

6、r.lrem(name, value, num)

#Delete the specified value from the list corresponding to name #Parameters: # name, redis name # value, value to delete # num, num=0, delete all specified values in the list; # num=2, delete 2 from front to back; # num=-2, from back to front, delete 2

7、lpop(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 operation from right to left

8、lindex(name, index)

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

9、lrange(name, start, end)

#Get data from the list fragment corresponding to name #Parameters: # name, redis name # start, the starting position of the index # end, index end position print(re.lrange ('aa ',0,re.llen ('aa'))10, ltrim(name, start, end)

#Remove values from the list corresponding to name that are not between the start-end index #Parameters: # name, redis name # start, the starting position of the index # end, index end position (greater than list length, means no removal)

11、rpoplpush(src, dst)

#Take the rightmost element from one list and add it to the leftmost element of another list #Parameters: # src, name of list to fetch data # dst, name of list to add data

11、blpop(keys, timeout)

#Arrange multiple lists and pop the elements of the corresponding list from left to right #Parameters: # keys, collection of redis names # timeout, when all the elements of the list are obtained, block the time (seconds) waiting for data in the list, 0 means forever block #more: # r.brpop(keys, timeout), Get data from right to left Crawler implementation simple distribution: multiple urls put in the list, keep putting URLs in, program loop value, but only one machine can run the value, you can put url in redis, multiple machines from redis value, crawl data, achieve simple distribution

12、brpoplpush(src, dst, timeout=0)

#Remove an element from the right side of one list and add it to the left side of another #Parameters: # src, name of the list of elements to be removed # dst, the name of the list of elements to insert # timeout, when src corresponds to the list of no data, block waiting for its data timeout time (seconds), 0 means forever block

13. Custom incremental iteration

#Since the redis class library does not provide incremental iterations of list elements, if you want to loop through all elements of the list corresponding to name, you need: # 1. Get all lists corresponding to name # 2, loop list #However, if the list is very large, then it is possible to burst the content of the program in the first step, so it is necessary to customize an incremental iteration function: import redisconn=redis.Redis (host='127.0.0.1',port=6379)# conn.lpush('test',*[1,2,3,4,45,5,6,7,7,8,43,5,6,768,89,9,65,4,23,54,6757,8,68])# conn.flushall()def scan_list(name,count=2): index=0 while True: data_list=conn.lrange(name,index,count+index-1) if not data_list: return index+=count for item in data_list: yield itemprint(conn.lrange('test',0,100))for item in scan_list('test',5): print('---') print(item) The above is all the contents of the case analysis of redis List operation. Thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report