Preface
Hash data structure is like C # dictionary, we should understand that the array is quickly positioned through the index to the specified element, whether it is to access the first element of the array or the last element, the time is the same, but the index in the array has no practical significance, He's just a place. When we look for an element, we usually use meaningful fields to index it, which results in dictionary. In fact, the implementation of dictionary is to let key with the subscript index has a certain relationship, the implementation of his search algorithm to the complexity of the constant O (1).
Pull up the egg above, I would like to say today Redis in the hash command, if you do not understand Redis, from the first look at the side of the knock, do not lazy, lazy people pretending you can not pretend, haha.
a command demonstration of a hash in Redis for a single key/value operation
Add command for hash in Redis Hset, if key does not exist, create key, exist, overwrite original value
Redis 127.0.0.1:6379>hset myhash name Jim----key to key MySet set key to name value for Jim
View command for hash in Redis Hget
Redis 127.0.0.1:6379>hget myhash name----output: "Jim", gets the value of key is MySet, key is name
Redis 127.0.0.1:6379>hset myhash name ZLH----Overwrite original value, change value to ZLH replace Jim
Redis 127.0.0.1:6379>hget myhash Name---output: "ZLH"
The hash in Redis gets the key containing the number of field commands Hlen
Redis 127.0.0.1:6379>hset Myhash Age---Set key to MySet key for age value=31
Redis 127.0.0.1:6379>hlen Myhash---The number of field outputs for 2,key to MySet is 2
A command hexists in Redis that specifies whether field is present in the specified key, there is a return of 1, there is no return 0
Redis 127.0.0.1:6379>hexists Myhash Name---Returns 1, indicating that there is
Redis 127.0.0.1:6379>hexists Myhash name1---returns 0, indicating that there is no
Redis Hash Delete command Hdel, delete one or more of the specified fields
Redis 127.0.0.1:6379>hset myhash Sex nan----add data
Redis 127.0.0.1:6379>hset Myhash issingle Yes----add data
Redis 127.0.0.1:6379>hset myhash Hobby Sports----Adding data
Redis 127.0.0.1:6379>hdel myhash Hobby----Delete individual data, filed to hobby data
Redis 127.0.0.1:6379>hdel myhash issingle Sex---Delete multiple data, filed two data for Issingle and sex
Redis Hash if key or field does not exist insert valid, otherwise do not take action command hsetnx
Redis 127.0.0.1:6379>hsetnx myhash Sex nan---Set the value of Myhash,field to sex as Nan, successfully returning 1 because there is no sex in this field
Redis 127.0.0.1:6379>hsetnx Myhash Sex NV---Set the value of Myhash,field to sex as NV and did not successfully return 0 because there was a field for sex and a value
Redis 127.0.0.1:6379>hget myhash Sex-Output "Nan"
A command that increases or decreases when value in a hash in Redis is a numeric value Hincrby
Redis 127.0.0.1:6379>del Myhash---Delete the key
Redis 127.0.0.1:6379>hset Myhash Age---Set key to Myhash with an age value of 31
Redis 127.0.0.1:6379>hincrby Myhash Age---Give key myhash, the value of age is added 10, the output is 41
Redis 127.0.0.1:6379>hincrby myhash age-10---Give key to Myhash, the value of age is minus 10, the output is 31
command demonstration of the hash batch operation Key/value in Redis
Add Key/value commands in bulk Hmset
Redis 127.0.0.1:6379>del Myhash--delete this key
Redis 127.0.0.1:6379>hmset myhash name ZLH age issingle No----add multiple key values Myhash to the hash of key Name=zlh,age=31,issingle=no
Command Hmget to get key/value in bulk
Redis 127.0.0.1:6379>hmget Myhash name Age issingle----Output: ZLH
Commands for all fields and value are obtained according to the Myhash key Hgetall
The Redis 127.0.0.1:6379>hgetall myhash----Output is: Name Age Issingle ZLH
Get all the field commands Hkeys
The Redis 127.0.0.1:6379>hkeys myhash---Output as: Name Age Issingle
command to get the values of all fields hvals
Redis 127.0.0.1:6379>hvals myhash----Output as: ZLH No
PostScript
If you have any questions after reading this article, please join the top left corner of the blog to Exchange learning.
My Redis Series blog: Double-click an address
REDIS data Structure detailed hash (iv)