Redis Series-Storage chapter hash main Operation function summary

Source: Internet
Author: User

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
    1. [Email protected] ~]# REDIS-CLI
    2. Redis 127.0.0.1:6379> hset user.1 name Zhangsan #设置key user.1 Name field value
    3. (integer) 1
    4. Redis 127.0.0.1:6379> Hset User.1 Age #设置age域
    5. (integer) 1
    6. Redis 127.0.0.1:6379> Hset User.1 Tech Lisi
    7. (integer) 1

b) Hmset

Syntax: Hash key field Value[key value]

Explanation: Bulk Set hash table key field

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> hmset user.2 name Niuer age #同时设置name and age domain
    2. 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
    1. Redis 127.0.0.1:6379> hsetnx user.1 name Lisi #由于name域已经设置过, so return 0
    2. (integer) 0
    3. Redis 127.0.0.1:6379> hsetnx User.1 Fri 5
    4. (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
    1. Redis 127.0.0.1:6379> hget User.1 name #存在的hash表及域
    2. "Zhangsan"
    3. Redis 127.0.0.1:6379> hget user.3 name #不存在的hash表
    4. (nil)
    5. Redis 127.0.0.1:6379> hget user.1 BB #不存在的域
    6. (nil)

b) Hmget

Syntax: Hmget key Field[field]

Explanation: Obtaining filed of hash table in bulk

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> hmget user.1 name Age Fri Tech
    2. 1) "Zhangsan"
    3. 2) "45"
    4. 3) "5"
    5. 4) "Lisi"
    6. Redis 127.0.0.1:6379> hmget user.1 name age Fri Tech Nofield #存在hash表中包含不存在的域nofield
    7. 1) "Zhangsan"
    8. 2) "45"
    9. 3) "5"
    10. 4) "Lisi"
    11. 5) (nil)
    12. Redis 127.0.0.1:6379> hmget user.3 name age Fri #不存在的hash表
    13. 1) (nil)
    14. 2) (nil)
    15. 3) (nil)

c) Hgetall

Syntax: Hgetall key

Explanation: Get all the domain values of the hash table

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> Hgetall user.2 #存在的hash表
    2. 1) "Name" #域
    3. 2) "Niuer" #域name的值
    4. 3) "Age" #域
    5. 4) "#域age的值"
    6. Redis 127.0.0.1:6379> Hgetall user.3 #不存在的hansh表
    7. (empty list or set)

D) hexists

Syntax: hexists key field

Explanation: Determine if a field exists in the hash table

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> hexists User.1 name #存在
    2. (integer) 1
    3. Redis 127.0.0.1:6379> hexists User.1 Nofield #不存在
    4. (integer) 0
    5. Redis 127.0.0.1:6379> hexists use1 Nofield #hash表不存在
    6. (integer) 0

e) Hkeys

Syntax: Hkeys key

Explanation: Get all the fields of the hash table

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> Hkeys User.1 #存在的hash表
    2. 1) "Name"
    3. 2) "Age"
    4. 3) "Tech"
    5. 4) "Fri"
    6. Redis 127.0.0.1:6379> Hkeys user.4 #不存在的hash
    7. (empty list or set)

f) hvals

Syntax: Hvals key

Explanation: Get all the domain values of the hash table

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> hvals User.1 #存在hash
    2. 1) "Zhangsan"
    3. 2) "45"
    4. 3) "Lisi"
    5. 4) "5"
    6. Redis 127.0.0.1:6379> hvals user.4 #不存在
    7. (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
    1. Redis 127.0.0.1:6379> Hincrby User.1 age 2 #增加2
    2. (integer) 47
    3. Redis 127.0.0.1:6379> Hincrby User.1 age-3 #减少3
    4. (integer) 44
    5. Redis 127.0.0.1:6379> Hincrby User.1 age2-3 #域不能存在, initial value is 0
    6. (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
  1. Redis 127.0.0.1:6379> Hkeys User.1
  2. 1) "Name"
  3. 2) "Age"
  4. 3) "Tech"
  5. 4) "Fri"
  6. 5) "Age2"
  7. Redis 127.0.0.1:6379> Hdel User.1 age2 #删除一个域
  8. (integer) 1
  9. Redis 127.0.0.1:6379> Hkeys User.1
  10. 1) "Name"
  11. 2) "Age"
  12. 3) "Tech"
  13. 4) "Fri"
  14. Redis 127.0.0.1:6379> hdel User.1 Fri Tech #删除2个域
  15. (integer) 2
  16. Redis 127.0.0.1:6379> Hkeys User.1
  17. 1) "Name"
  18. 2) "Age"
  19. Redis 127.0.0.1:6379> Hdel user.1 BB #删除一个不存在的域
  20. (integer) 0 #返回0

5) Other

Syntax: Hlen key

Explanation: The number of fields to get a hash

[Plain]View Plaincopy
    1. Redis 127.0.0.1:6379> Hkeys User.1
    2. 1) "Name"
    3. 2) "Age"
    4. Redis 127.0.0.1:6379> Hlen User.1 #存在2个域
    5. (integer) 2
    6. Redis 127.0.0.1:6379> Hlen user.4 #不存在的hash
    7. (integer) 0

For more detailed usage of hash, see: Http://redis.io/commands#hash

Redis Series-Storage chapter hash main Operation function summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.