I have been in touch with redis for a while. I know all five types, but it seems confusing when to use which type ~ Could you give me some advice ~ I have been in touch with redis for a while. I know all five types, but it seems confusing when to use which type ~ Could you give me some advice ~
Reply content:
I have been in touch with redis for a while. I know all five types, but it seems confusing when to use which type ~ Could you give me some advice ~
Answer this question based on my understanding of redis.
What is redis?
Redis is a storage key-value Pair warehouse, how to use redis lies in how to understand the E-R model of the system you need to design, and then rational planning redis database structure
Scenario
Here is an example of a simple messaging system. Business Requirements: the server sends messages to users.
E-R model:
1. User (uid, nickname, phone, mood)
2. Message (mid, title, content, ts)
The relationship between a user and a message is n: n. a message can be sent to multiple users, and each user can have multiple messages.
Redis Database Design
The key to redis database design is key design.Fixed prefix + unique suffixFor example:
1. the message entity is a two-layer structure. You can use redis's hash data structure for storage (ps: It's okay to use a basic string). The key is msg _ [$ mid], mid is similar to the primary key of a mysql table. It must be globally unique in the current redis database and can be implemented using the redis incr atomic operation. The value can be array ('title' => 'title ', 'content' => 'content', 'ts' => 'publication timestamp '). in this way, each time the server generates a message, construct such a key-value pair to represent the content of the message.
2. user entity. Because user information requires more stable persistent storage, it is recommended to store it directly in mysql without migrating to redis.
3. User-message relationship. You can consider the redis sets data structure. The key is a set of unread_mids _ [$ uid] And read_mids _ [$ uid], and the value is mid. Each time a message is sent to a specified user, you only need to put the message in the corresponding set.
In this way, the E-R model corresponding to the message system is displayed.
Summary
How to Use redis database, the key is how you design and understand how you want to do the system E-R model, understand the relationship between entities, design the corresponding key-value pairs in redis. As for those data structures, it is not very helpful for you to design a redis database, but it is more convenient. You can do this using memcache.
For redis to provide several data structures, I suggest not floating on the surface is just to call an api, a little technical content are not, it is recommended to look at the source code implementation, recommended link: http://www.redisbook.com/en/latest/