Follow the Redis Getting Started Guide to learn
Chapter III 5 Types of data----hash type
3.3 (hash type)
Redis uses several structures to store data in the form of a health-value pair, while the health value of a hash type (hash) is also a dictionary structure that stores mappings of fields and field values. However, field values can only be strings, and other data types are not supported. In other words, a hash type cannot nest other data types. A hash type Jian can contain up to 2^32-1 fields.
Tip: In addition to hash types, data type nesting is not supported for other REDIS data types. For example, each element of a collection type can be a string only, not another collection or hash list, and so on.
Hash types are suitable for storing objects: Use the object class and ID to form a health name, use the properties of the object represented by the field, and the field value to store the property value. For example, to store a car object with an ID of 2, you can use 3 fields, named color, name, and price, to store the car's colors, names, and prices, respectively.
In a relational database, if you want to store a car object, the data is stored as a two-dimensional table, which means that all records have the same properties, cannot add or subtract attributes for a single chance, and if you want to increase the production date attribute for a car with an ID of 1, you need to include a data property for the table change. Data is redundant for the previous information. It is conceivable that when different records require different attributes, the number of fields in the table becomes more and more difficult to maintain. And when using ORM to map an object entity in a relational database into an entity in a program, modifying the table's structure often means disrupting the service (restarting the site program). To prevent these problems, storing this semi-structured data in a relational database requires additional tables.
The hash type of Redis does not have this problem. Redis does not require that each jian be stored according to this structure, and we are completely free to place any health or increment fields without affecting other health.
3.3.2 Command
1. Assigning and taking values
Hset key field value
hget key fiels
hmset key field value [Field value ...]
Hmget key field [field ...]
Hgetall Key
The Hset command is used to assign a value to a field, while the Hget command gets the value of the field, using the following:
127.0.0.1:6379[1]> hset car price
(integers) 1
127.0.0.1:6379[1]> hset car Price 666
(integer) 0
127.0.0.1:6379[1]> hget car Price
"666"
127.0.0.1:6379[1]> hset car name BMW
(integer) 1
127.0.0.1:6379[1]> hget car name
"BMW"
The convenience of the Hset command is that it does not differentiate between insert and update operations, which means that you do not have to determine whether a field exists to perform an insert operation (insert) or update operation (update) when modifying data. When you perform an insert (that is, the previous field does not exist) the Hset command returns 1, and when you perform an update operation, the return is 0. The Hset command also automatically establishes a health care when it does not exist.
Tip: In Redis, each of the keys belongs to a specific data type, such as a hash type established by the Hset command, which is a string type created by the set command. Use one of the data type commands to manipulate the health of another data type to prompt for error: Err operation against a key holding the wrong kind of value.
127.0.0.1:6379[1]> hmget Car name Price
1) "BMW"
2) "666"
127.0.0.1:6379[1]> hmset car color blue data 2 016
OK
127.0.0.1:6379[1]> hmget car name price color data
1) "BMW"
2) "666"
3) "Blue"
4) " "
127.0.0.1:6379[1]> hgetall car
1)" Price "
2)" 666 "
3)" name "
4)" BMW "
5)" Color "
6)" Blue "
7" "Data"
8) "2016"
If you want to get all the fields and field values in your health and don't know what fields are in your health, you should use the Hgetall command, and the result is a list of fields and field values, not very intuitive, but the Redis client in many languages encapsulates the return result of Hgetall into an object in the programming language. It's very convenient to deal with. For example, in node. JS:
The return value of Redis.hgetall ("Car", function (error,car) {
//hgetall method) is encapsulated as Javasript object
console.log (car.price);
Console.log (Car.name);
});
2. Determine if a field exists
Hexists key Field
Returns 0 if there is a return of 1. (If the health does not exist, it will return 0).
127.0.0.1:6379[1]> hexists car Model
(integer) 0
127.0.0.1:6379[1]> hexists car name
(integer) 1
127.0.0.1:6379[1]> hexists nocar name
(integer) 0
3. Assignment when the field does not exist
Hsetnx key field value
The HSETNX command is similar to the Hset command, except that if the field already exists, the HSETNX command will not take any action. The HSETNX command is an atomic operation without worrying about race conditions.
4. Increase the number
Hincrby key field Increment
Hincrby is similar to the Incrby command, which can be a field value that increases the specified integer, the hash type has no HINCR command, but can be implemented by Hincrby key field.
127.0.0.1:6379[1]> Hincrby CA Price
(integer) $
127.0.0.1:6379[1]> hget car price
"666"
127.0.0.1:6379[1]> Hincrby car Price
(integer) 866
127.0.0.1:6379[1]> hget CA-Price
"200"
If the health does not exist, the Hincrby command automatically establishes the health and defaults to the Price field before executing the command with a value of 0. The return value of the command is the value of the field after the increment.
5. Delete a field
Hdel key field [field ...]
The Hdel command can delete one or more fields, and the return value is the number of deleted fields:
127.0.0.1:6379[1]> Hdel Car Price name
(integer) 2
127.0.0.1:6379[1]> hdel car color name
(integer) 1
3.3.4 Command Supplements
1. Get field names or field values only
Hkeys key
hvals key
127.0.0.1:6379[1]> Hkeys car
1) "Data"
2) "name"
3) "Color"
127.0.0.1:6379[1]> hvals car
1) "2"
BMW "
3") "Blue"
2. Get the number of fields
Hlen Key
127.0.0.1:6379[1]> hlen car
(integer) 3