In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Redis data types and applicable scenarios, the article is very detailed, has a certain reference value, interested friends must read it!
1. Interview questions
What are the data types of Redis? Which scenarios do they apply to?
2. Psychological analysis of the interviewer
Unless the interviewer looks at your resume and feels that you are a relatively junior classmate who has been working for less than 3 years and may not have a deep study of technology, the interviewer will ask such questions.
In fact, there are two main reasons for asking this question:
First, see if you have a comprehensive understanding of the functions of redis and how to use it. Have you ever seriously thought about what scenario is more suitable for which data type to use? I'm afraid you only know the simplest kv operation, and you haven't thought about how to design it.
Second, see how you use Redis in actual projects, whether you have thought about these data types of Redis, and whether you are a learner.
If you don't give a good answer, don't name a few data types, and don't say anything about the scene, you are doomed. The interviewer must have a bad impression of you, thinking that you are usually just doing a simple set and get.
3. Analysis of the interview questions
Let's see how to answer this interview question.
(1) string
This is the most basic type of Redis, and the most commonly used, there is nothing to say, just ordinary set and get, do a simple kv cache.
(2) hash
This is a structure similar to map. Generally, you can cache structured data, such as an object (provided that the object does not nest other objects) in redis, and then manipulate a field in hash instead of taking out the whole object every time you read or write to the cache. This saves IO operations and is more efficient.
Use the HSET key field value command to store an object, such as we have a user
Key=user:id:1
Value= {
"id": 1
"name": "walking"
"age": 24
}
The data structure of the hash class is mainly used to store some objects and cache some simple objects. In subsequent operations, you can directly modify only the value of a field in this object.
Value= {
"id": 1
"name": "walking"
"age": 18
}
HGET user:id:1 age gets an age value with a user ID of 1.
(3) list
Ordered list, this can play a lot of tricks.
For example, in Weibo, if you have a big v fan, you can cache it in Redis in list format.
Key= some big v
Value= [zhangsan, lisi, wangwu]
For example, you can use list to store some column phenotypic data structures, such as fan lists, comment lists of articles, and so on.
You can also use the lrange command to read how many elements from an element, and you can also implement paging queries based on list, which is a great feature. Based on redis to achieve simple high-performance paging, you can do things like Weibo drop-down paging, high performance, a page to go.
For example, it can also be used to create a simple message queue, stuffing it into the list header and getting it out of the list tail.
(4) set
Set unordered collection, which can be deduplicated automatically.
Throw the data that needs to be deduplicated in the system directly based on set, and it will be deduplicated automatically. If you need to quickly remove the weight of some data globally, you can also do it based on the HashSet in jvm memory. But what if one of your systems is deployed on multiple machines? You have to do global set de-duplication based on Redis.
Of course, you can also play the operation of intersection, union and difference based on set, such as intersection bar, and you can intersect the fan list of two people to see who their common friends are. Right.
Put the fans of the two big v in the two set, do the intersection of the two set, and see the people of common concern.
(5) sorted set
Sorted set, de-duplicated but can be sorted, when written in, give a score, automatically sort according to the score, this can play a lot of tricks. The biggest feature of this data type is the concept of scores, which can be customized.
For example, if you want to sort the data by time, you can write it in with a certain time as a score, and people will automatically sort you by time.
In addition, this data type is suitable for functions such as the top rankings.
Ranking: write each user and its corresponding score into it
Command zadd board score username
Such as
Zadd board 85 Jobs
Zadd board 72 Jerry
Zadd board 96 Walking
Zadd board 62 Tom
...
Then, using the command zrevrange board start stop, you can get users ranked from start to stop.
Using the command zrank board username, you can see the ranking of users in the rankings.
Such as:
Zrevrange board 0 3 gets the top 3 users
96 Walking
85 Jobs
72 Jerry
View ranking zrank board Tom
Return to 4
The above is all the content of the article "what are the Redis data types and applicable scenarios". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.
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.