In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article uses easy-to-understand examples to introduce the method of using redis to store list, the code is very detailed, interested friends can refer to it, I hope it can be helpful to you.
A problem encountered in the work is that because business requirements need to be accelerated by redis, how can the list list stored in redis be fully stored and taken out?
If you use redis's list storage method, take out the elements one by one in the list = [1Jing 2jue 3je 4] list, and then use the lpush method to store the elements one by one. In the end, you use the complete lrange ('name',0,-1) to take out, you will find that the removed list has become a str, for example, (list =' [1Jing 2Met 3Jet 4]'), which is very troublesome to deal with.
After continuous attempts, we found a relatively easy to use library pickle.
The advantage of pikle is that the serialization process converts text information into a binary data stream. In this way, the information can be easily stored on the hard disk, and when you need to read the file, read the data from the hard disk, and then deserialize it to get the original data.
Some strings, lists, dictionaries and other data are obtained during the operation of the Python program. If you want to save them for a long time, it is convenient to use them later, rather than simply put them in memory and turn off the computer and lose the data.
The Pickle module in the python module encyclopedia comes in handy, transforming objects into a format that can be transferred or stored. This ensures the integrity of the stored and retrieved data.
From redis import StrictRedis# selects a default redis db repository db= StrictRedis (host='localhost', port=6379, db=0,decode_responses=True) # imports the pickle package import pickle list = [1je 2je 3jue 4] # pickle encapsulates listnew_list = pickle.dumps (list) # redis to store data, and sets the expiration time db.setx ('list',3600,new_list) result = db.get (' list') get_list = pickle.loads (result) print (get_list) # get_list = [1Med 2Met 3JE4]
After dealing with pickle like this, you will find that the list removed is much better to use. You don't have to worry about data conversion anymore, and you can also deal with data types such as json.
So much for sharing the method of using redis to store list. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.