Redis pioneered a new approach to data storage, and using Redis, we don't have to focus on the problem of how to put an elephant into a refrigerator in the face of a monotonous database, but instead use Redis's flexible data structure and data manipulation to build different refrigerators for different elephants.
There are five main types of data used in Redis:
String
Hash
List
Set
Sorted Set
1, String
1.1. Common commands: Set,get,decr,incr,mget, etc.
1.2. Application Scenario:
String is one of the most commonly used data types, and normal Key/value storage can be categorized as such, not explained here.
2, Hash
2.1. Common commands: Hget,hset,hgetall, etc.
2.2. Application Scenario:
Let's simply cite an example to describe the application scenario for a hash, such as storing a user information object data that contains the following information:
The user ID is the key to find, the stored value user object contains the name, age, birthday and other information, if the ordinary key/value structure to store, mainly has the following 2 kinds of storage methods:
The disadvantage of using the user ID as a lookup key to encapsulate other information as a serialized object is to increase the cost of serialization/deserialization and to retrieve the entire object when one of the information needs to be modified, and the modification operation requires concurrency protection. Introduce complex problems such as CAs.
The second method is how many members of this user information object will be saved into the number of key-value, with the user id+ the name of the corresponding property as a unique identifier to obtain the value of the corresponding property, although the cost of serialization and concurrency is omitted, but the user ID is repeated storage, if there is a large number of such data, The memory waste is still very considerable.
So the hash provided by Redis is a good solution to this problem, and the Redis hash is actually the internal stored value as a hashmap, and provides a direct access to the map member's interface, such as:
That is, the key is still the user ID, value is a map, the map key is a member of the property name, value is the property value, so that the data can be modified and accessed directly through its internal map key (Redis called internal map key field), This means that the corresponding attribute data can be manipulated by key (user ID) + field (attribute tag), without the need to store the data repeatedly and without the problem of serialization and concurrency modification control. A good solution to the problem.
It is also important to note that Redis provides an interface (Hgetall) that can fetch all of the property data directly, but if the internal map has a large number of members, it involves traversing the entire internal map, which can be time-consuming due to the Redis single-threaded model. The other client requests are not responding at all, which requires extra attention.
Xxx
Redis Application Scenario One