In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to master Redis scene design". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to master Redis scene design.
I. Common instructions
Next, let's take a look at the commonly used instructions for each data structure, which are clearly shown in a table:
Second, scene parsing 1.1string storage
1.2String type usage scenario
Scenario 1: inventory of goods
In terms of business, commodity inventory data is hot data, and transaction behavior will directly affect inventory. Redis's own String type provides:
Set goods_id 10; the initial inventory value of items with id set to good_id is 10
Decr goods_id; when the item is purchased, the inventory data is minus 1.
Similar scenarios in turn: the number of views of the product, the number of likes of questions or replies, and so on. You can consider using Redis to implement this kind of counting scenario.
Scenario 2: storage of aging information
Redis's data storage has the ability of automatic failure. That is, the stored key-value can set the expiration time: set (key, value, expireTime).
For example, a user who logs in to an App needs to obtain a login verification code, which is valid for 30 seconds. Then we can use the String type to store the CAPTCHA and set the expiration time of 30 seconds.
2.1hash stores data
2.2Hash type usage scenario
Redis needs to serialize and then store objects when storing objects (for example, user information).
Another form is to convert object data into JSON structured data, and then store JSON strings to Redis.
For some object types, there is another convenient type, which is stored according to the Hash type of Redis.
For example, we store some basic information about website users, and we can use:
In this way, a basic user information is stored, which includes: {name: Xiao Ming, phone: "123456", sex: "male"}
Of course, there are many similar scenarios, such as storing order data, product data, basic merchant information and so on. Give priority to Taobao shopping cart
2.3 advantages and disadvantages of implementing information storage
1. Native:
Set user: 1:name james
Set user:1:age 23
Set user:1:sex boy
Advantages: simple and intuitive, each key corresponds to a value
Disadvantages: too many keys, too much memory, too scattered user information, not used in production environment
two。 Serialize the object into the
Redis set user:1 serial ize (userInfo)
Advantages: simple programming, high memory usage if serialization is reasonable.
Disadvantages: serialization and deserialization have some overhead. When updating attributes, you need to deserialize all the userInfo, and then serialize to redis after updating.
3.hash storage:
Hmset user:1 name james age 23 sex boy
Advantages: simple and intuitive, reasonable use can reduce memory space consumption
Cons: to control both ziplist and hashtable transcoding, Mhashtable consumes more memory.
3.1List type usage scenario
List is a linked list of strings sorted in the order in which they are inserted. New elements can be inserted at the head and tail (bidirectional linked list implementation, with O (1) time complexity for adding elements at both ends).
Scenario 1: message queuing implementation
At present, there are many professional message queue components such as Kafka, RabbitMQ and so on. Here we are simply using the characteristics of list to implement the requirements of message queues. In the process of actual technology selection, we can think carefully.
List storage is the storage form of a queue:
Lpush key value; adds a string element to the header of the corresponding list of key
Rpop key; removes the last element of the list and returns the removed element.
Scene 2: the latest goods on the shelves
There is often a new product recommendation module on the home page of the trading website, which stores the latest top 100 on the shelves.
At this time, Redis's list data structure is used to store TOP 100s new products on the shelves.
The Redis ltrim directive trim a list so that the list contains only the specified elements of the specified range.
Both start and stop are counted with 0, where 0 is the first element (header) in the list and 1 is the second element.
The following pseudo-code is demonstrated:
4.1set type usage scenario
Set also stores a collection list function. Unlike list, set has deduplication function. It is more appropriate to use set when you need to store a list of information and require that the elements in the list cannot be duplicated. At the same time, set also provides intersection, union and subtraction.
For example, on a trading website, we store information about goods that users are interested in, and when analyzing similar users, we can provide some basis by calculating the number of goods of interest between two different users.
Get the similar products of two users, and then determine the category of similar products for user analysis.
Similar application scenarios include the support of social scenarios such as paying attention to friends and tag with similar interests.
4.2Set collection special operation commands
SetA= {A,B,C} setB= {B,C}
1) the intersection between sets and sets
Sinter setA setB-- > get the set {BMagazine C}
Union between a set and a set
Sunion setA setB-- > get the set {Ameng Bjorn C}
3) the difference between sets and sets
Sdiff setA setB-- > get the collection {A}
4.3Set collection special operation command application scenario
How to realize the micro-relationship design of Weibo?
5.1 Zset ordered set
It is often used for ranking, for example, a video website needs to rank the videos uploaded by users, or the number of likes is related to the collection, and there can be no duplicate members.
5.2Zset type usage scenario
Thank you for your reading, the above is the content of "how to master Redis scene design". After the study of this article, I believe you have a deeper understanding of how to master Redis scene design, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.