original works, reproduced please indicate: http://blog.csdn.net/Xiejingfa/article/details/50550416
In the previous article, "Redis Note (ii)" In the Redis data structure-string string, we learned about Redis's most basic data structure, string, and today our protagonist is the –hash (hash) type. description of hash type
Hash is a data type added after Redis2.0, like the map data structure in most programming languages, Redis is a collection of key-value pairs, meaning that it holds a mapping between a string and a string. Because of this feature, hashing is particularly useful for storing an object. Storing an object in a hash consumes less memory and makes it easy to access the entire object.
The main command of the hash can be queried here. related commands for hash 1. Hset command
The Hset command uses a hash to specify the value of the key, if the key does not exist, creates and sets the corresponding value, returns an integer of 1, and if the key already exists, the corresponding value is overwritten and the integer 0 is returned. The specific format is:
Hset hash_name Field value
Example 1:
127.0.0.1:6379> hset Myhash name Fred
(integer) 1
127.0.0.1:6379> hset myhash name fred2
(integer) 0
2. Hmset command
The Hmset command is similar to the Hset command, and can be used to set the hash key and value. The difference is that Hmset can set multiple key-value pairs at the same time. After the operation succeeds, the Hmset command returns a simple string "OK". The specific format is as follows:
Hmset hash_name field1 value1 field2 value2 ...
Example 2:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hget myhash name
"Fred"
127.0.0.1:6379> hget Myhash Age
"24"
3. Hsetnx command
The HSETNX command is also used to set the key value information if the specified key does not exist. If the key does not exist, Redis creates the key first, then sets the corresponding value, and returns the integer 1 after the operation succeeds. If the key already exists, the command does nothing and returns a value of 0. The specific format is as follows:
Hsetnx hash_name Field value
Example 3:
127.0.0.1:6379> hsetnx Myhsh address hangzhou
(integer) 1
127.0.0.1:6379> hsetnx myhsh address guangzhou< c10/> (integer) 0
127.0.0.1:6379> hget myhsh address
"Hangzhou"
4. Hget command
The Hget command is used to obtain the value of a hash specified key. If the key exists, return the corresponding value directly, otherwise nil is returned. The specific format is as follows:
Hget hash_name Field
Example 4:
127.0.0.1:6379> hset Myhash name Fred
(integer) 0
127.0.0.1:6379> hget myhash name
"Fred"
127.0.0.1:6379> hget myhash nonkey
(nil)
5. Hmget command
The Hmget command is similar to the Hget command, which returns a list of values for a hash of multiple keys, and a nil value for a nonexistent key. The specific format is as follows:
Hmget hash_name field1 field2 ...
Example 5:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hmget myhash name Age weight
1) "Fred"
2) "3"
(nil)
6. hexists command
The hexists command is used to determine if a hash specified key exists and returns 0 if there is a return integer of 1. The specific format is as follows:
hexists hash_name Field
Example 6:
127.0.0.1:6379> hset Myhash name Fred
(integer) 0
127.0.0.1:6379> hexists myhash name
(integer) 1
127.0.0.1:6379> hexists Myhash Home
(integer) 0
7. Hlen Command
The Hlen command is used to return the number of keys in a hash. The specific format is as follows:
Hlen Hash_name
Example 7:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hlen myhash
(integer) 2
127.0.0.1:6379> hlen nonhash
(integer) 0
8. Hdel Command
The Hdel command is used to delete the key specified by a hash. If the key does not exist, no action is made. The return value of the Hdel command is the number of keys that were successfully deleted (not including nonexistent keys). The specific format is:
Hdel hash_name Field:
Example 8:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hdel myhash name Age
(integer) 2
1 27.0.0.1:6379> hmget Myhash name Age
1) (nil)
2) (nil)
9. Hkeys command
The Hkeys command returns all keys for a hash and returns an empty list if no key exists for that hash. The specific format is as follows:
Hkeys Hash_name
Example 9:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hkeys myhash
1) "Name"
2) "Age "
127.0.0.1:6379> hdel myhash name Age
(integer) 2
127.0.0.1:6379> hkeys myhash
(Empty list or set)
10. Hvals Command
The hvals command returns a list of all values for a hash. The specific format is as follows:
Hvals Hash_name
Example 10:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hvals myhash
1) "Fred"
2) "24"
11. Hgetall Command
The Hgetall command returns a list that contains all the keys and values for a hash. In the return value, the first key, the next element is the corresponding value, so the Hgetall command returns the list length is twice times the hash size. The specific format is as follows:
Hgetall Hash_name
Example 11:
127.0.0.1:6379> hmset myhash name Fred Age
OK
127.0.0.1:6379> hgetall myhash
1) "Name"
2) " Fred "
3") "Age
" 4) "24"
12. Hincrby command and Hincrbyfloat command
Both commands are used to increment the specified key, but the Hincrby command adds an integer value each time, and the hincrbyfloat command adds a floating point value each time. Returns the final value after the operation succeeds after the increment operation. The specific format is as follows:
Hincrby hash_name field K
hincrbyfloat hash_name field K
Example 12:
127.0.0.1:6379> hset myhash Age (integer) 0 127.0.0.1:6379> Hincrby Myhash age 2 (intege R) 127.0.0.1:6379> Hincrby myhash age-2 (integer)