Sunlight through the glass, sprinkle on the body, a cup of warm tea in the hand, said endless ease complacent, let me have a kind of want to write a blog impulse. On the main talk about string, here to talk about hash it!
Hash is a mapping table of some column key value (field value). It is often used to store some object instances. Storing as a hash consumes less memory than storing the individual fields of an object as a string. Why do you save more memory? Need to figure out the meaning of the two configurations (Hash-max-zipmap-entries and Hash-max-zipmap-value), the detailed description of the configuration, which I intend to put in the final configuration optimization session.
1) New
A) Hset
Syntax: Hset key field value
Explanation: Set the value of field in the hash table key. If the hash table does not exist, then create and execute the value of field set if the hash table exists, the value of field is overwritten or new
[Plain]View Plaincopy
- [Email protected] ~]# REDIS-CLI
- Redis 127.0.0.1:6379> hset user.1 name Zhangsan #设置key user.1 Name field value
- (integer) 1
- Redis 127.0.0.1:6379> Hset User.1 Age #设置age域
- (integer) 1
- Redis 127.0.0.1:6379> Hset User.1 Tech Lisi
- (integer) 1
b) Hmset
Syntax: Hash key field Value[key value]
Explanation: Bulk Set hash table key field
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> hmset user.2 name Niuer age #同时设置name and age domain
- Ok
c) hsetnx
Syntax: Hsetnx key field value
Explanation: Setting the value of the hash table field only when the field field does not exist
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> hsetnx user.1 name Lisi #由于name域已经设置过, so return 0
- (integer) 0
- Redis 127.0.0.1:6379> hsetnx User.1 Fri 5
- (integer) 1 #fri域没有设置过, so hset and return 1
2) query
A) Hget
Syntax: Hget key field
Explanation: Gets the field value of the hash table key
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> hget User.1 name #存在的hash表及域
- "Zhangsan"
- Redis 127.0.0.1:6379> hget user.3 name #不存在的hash表
- (nil)
- Redis 127.0.0.1:6379> hget user.1 BB #不存在的域
- (nil)
b) Hmget
Syntax: Hmget key Field[field]
Explanation: Obtaining filed of hash table in bulk
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> hmget user.1 name Age Fri Tech
- 1) "Zhangsan"
- 2) "45"
- 3) "5"
- 4) "Lisi"
- Redis 127.0.0.1:6379> hmget user.1 name age Fri Tech Nofield #存在hash表中包含不存在的域nofield
- 1) "Zhangsan"
- 2) "45"
- 3) "5"
- 4) "Lisi"
- 5) (nil)
- Redis 127.0.0.1:6379> hmget user.3 name age Fri #不存在的hash表
- 1) (nil)
- 2) (nil)
- 3) (nil)
c) Hgetall
Syntax: Hgetall key
Explanation: Get all the domain values of the hash table
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Hgetall user.2 #存在的hash表
- 1) "Name" #域
- 2) "Niuer" #域name的值
- 3) "Age" #域
- 4) "#域age的值"
- Redis 127.0.0.1:6379> Hgetall user.3 #不存在的hansh表
- (empty list or set)
D) hexists
Syntax: hexists key field
Explanation: Determine if a field exists in the hash table
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> hexists User.1 name #存在
- (integer) 1
- Redis 127.0.0.1:6379> hexists User.1 Nofield #不存在
- (integer) 0
- Redis 127.0.0.1:6379> hexists use1 Nofield #hash表不存在
- (integer) 0
e) Hkeys
Syntax: Hkeys key
Explanation: Get all the fields of the hash table
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Hkeys User.1 #存在的hash表
- 1) "Name"
- 2) "Age"
- 3) "Tech"
- 4) "Fri"
- Redis 127.0.0.1:6379> Hkeys user.4 #不存在的hash
- (empty list or set)
f) hvals
Syntax: Hvals key
Explanation: Get all the domain values of the hash table
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> hvals User.1 #存在hash
- 1) "Zhangsan"
- 2) "45"
- 3) "Lisi"
- 4) "5"
- Redis 127.0.0.1:6379> hvals user.4 #不存在
- (empty list or set)
3) Modify
Syntax: Hincrby key field increment
Explanation: The value of the field field of the hash table increases step increment, and if increment is negative, it is decremented. If the domain does not exist, the initial value is treated as 0
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Hincrby User.1 age 2 #增加2
- (integer) 47
- Redis 127.0.0.1:6379> Hincrby User.1 age-3 #减少3
- (integer) 44
- Redis 127.0.0.1:6379> Hincrby User.1 age2-3 #域不能存在, initial value is 0
- (integer)-3
4) Delete
Syntax: Hdel key Field[field]
Explanation: Deleting a hash field, if more than one field is specified, deletes multiple
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Hkeys User.1
- 1) "Name"
- 2) "Age"
- 3) "Tech"
- 4) "Fri"
- 5) "Age2"
- Redis 127.0.0.1:6379> Hdel User.1 age2 #删除一个域
- (integer) 1
- Redis 127.0.0.1:6379> Hkeys User.1
- 1) "Name"
- 2) "Age"
- 3) "Tech"
- 4) "Fri"
- Redis 127.0.0.1:6379> hdel User.1 Fri Tech #删除2个域
- (integer) 2
- Redis 127.0.0.1:6379> Hkeys User.1
- 1) "Name"
- 2) "Age"
- Redis 127.0.0.1:6379> Hdel user.1 BB #删除一个不存在的域
- (integer) 0 #返回0
5) Other
Syntax: Hlen key
Explanation: The number of fields to get a hash
[Plain]View Plaincopy
- Redis 127.0.0.1:6379> Hkeys User.1
- 1) "Name"
- 2) "Age"
- Redis 127.0.0.1:6379> Hlen User.1 #存在2个域
- (integer) 2
- Redis 127.0.0.1:6379> Hlen user.4 #不存在的hash
- (integer) 0
For more detailed usage of hash, see: Http://redis.io/commands#hash
Redis Series-Storage chapter hash main Operation function summary