Redis key value design question?

Source: Internet
Author: User
As a Tom, I recently learned about Redis Cache and had a question: if there is a user table in mysql that is used to save user information, the main fields of the table are: id, username, password, email, nick, born, sex, status, etc. now you want to include... as a Tom, I recently learned about Redis Cache and have a question:
Assume that a user table in mysql is used to store user information. The main fields of the table include id, username, password, email, nick, born, sex, status, and so on, I want to cache all the data in the user data table to the redis database server. how can I set the redis key value?
Is set to be similar:

user:[id]:username username_valueuser:[id]:password password_valueuser:[id]:email email_valueuser:[id]:nick nick_value...

Set it to the following format:

User: [id] json_string // json_string is a two-dimensional array of user information converted into a json string.

Which of the two forms is better? Which one saves more memory?
I hope you can answer my questions. thank you very much =. =

Reply content:

As a Tom, I recently learned about Redis Cache and have a question:
Assume that a user table in mysql is used to store user information. The main fields of the table include id, username, password, email, nick, born, sex, status, and so on, I want to cache all the data in the user data table to the redis database server. how can I set the redis key value?
Is set to be similar:

user:[id]:username username_valueuser:[id]:password password_valueuser:[id]:email email_valueuser:[id]:nick nick_value...

Set it to the following format:

User: [id] json_string // json_string is a two-dimensional array of user information converted into a json string.

Which of the two forms is better? Which one saves more memory?
I hope you can answer my questions. thank you very much =. =

Add a prefix to the user ID and set it to key. for example, user: 888
Common User attributes are stored in the hash set.
In addition, it is not recommended that all users have no brains to store cache... this is a bit upside down .. mysql will be overwhelmed (how can I use dynamic _ tables )... you can try to create a simple heat statistics hot user memory cache

If this is not the case, you can set a timeout value for hash. after the first login, each user information will be stored for a period of time...

I have not studied the relationship between different data types and memory.
From the business logic point of view, the specific design of storage depends on requirements.
For example, the cache is only used for query convenience and information display. select the second one. mysql or mongodb has Related operations before updating the corresponding cache.
If you need to grant logon and data CURD logic, hash is recommended on the same building, for example, HMSET, rather than String type.

The first one is better. you can find one without coding.

I personally think that the 1st categories are better, which facilitates individual acquisition and modification. if it is the latter, each time you get or modify a single attribute, you need to get all of them, in addition, parsing json to obtain other serialization and deserialization forms will cause additional consumption.
In most cases, you do not need to query all the fields in the query. if you modify it, you only need to modify one field.

I have also encountered the problem of data table caching. my solution is to use multiple data types to complete the process.

First, each data record is saved into a dictionary, and the dictionary name is encrypted with the table name + ID and then md5.
Then, use an ordered set to store the dictionary name and sorting weight of each record (I want to sort the records here, so use an ordered set. Otherwise, use a set or list)

PS: redis cannot perform operations such as where query, so if necessary, you can filter them at the code layer and store them in redis by category, for example, I want to filter the data and store several different sorted sets, and then maintain the names of these sorted sets to a list.

The latter.
It is easy to operate and can avoid bottomless holes.

Bottomless cache

Do not use json. it seems that the value is convenient, but it cannot be described when the cached data is updated in multiple places at the same time. It is not convenient to write a program. only what you write is unreliable.

There is no need to cache all the data, such as gender. Only frequently accessed data must be cached.

What should I say in json format in redis .. It's not impossible, but it's just a pleasure not to make full use of the value of redis.

Use the hash structure. The second type is recommended. The first significance of data cache is to speed up browsing. for some data that will be modified, I normally create one more cache to store the data to be modified, the actually displayed data will be normally cached + modified cached data.

The general practice in the industry is
The value is stored in binary format, rather than a string. this saves memory and is highly efficient. The disadvantage is that the plaintext cannot be viewed through redis-cli because it is already in binary format.
For example
Key = user: [id]
Value = the binary form of the user object.

The next time you read this value using java, it will be automatically converted to your object. this is simple and you don't need to set the attribute one by one.

However, you must note that once this is involved, you cannot search by other attributes of the user, provided that you understand your business, if you want to add another key value to search for other fields, data redundancy is common in nosql.

Nosql is like this. you must understand the business when writing a program. Otherwise, the data structure design will be poor. Although the use threshold of nosql is very low, there are still some design barriers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.