Redis Learning Lesson Two: Redis hash types and operations

Source: Internet
Author: User

Redis Hash is a string-type field and value mapping table. Its addition, delete operations are O (1) (average). Hash is particularly useful for storing objects. there is a single string type compared to each field of an object. Storing an object in a hash type takes up less memory and makes it easier to access the entire object.

Hset: Sets the hash field to the specified value, and if key does not exist, it is created first.

Hget: Gets the specified hash field.

127.0.0.1:6379>hset user:001 name Tom

(integer) 1

127.0.0.1:6379>hset user:001 Age 28

(integer) 1

127.0.0.1:6379>hget user:001 Name

"Tom"

HSETNX: Sets the hash field to the specified value, and if key does not exist, it is created first. Returns 0 if it exists.

127.0.0.1:6379>hset user:001 name Jack

(integer) 0

Hmset: Set multiple fields of hash at the same time.

Hmget: Gets all the specified hash field.

127.0.0.1:6379> hmset user:002 ID 1 name Mark sex male Age 29

Ok

127.0.0.1:6379> Hget user:002

(Error) ERR wrong number of arguments for ' hget ' command

127.0.0.1:6379> hmget user:002 ID name male age

1) "1"

2) "Mark"

3) (nil)

4) "29"

Hincrby: Specifies the hash field plus the given value.

127.0.0.1:6379> Hincrby user:002 age 6

(integer) 35

127.0.0.1:6379> Hincrby user:002 age-2

(integer) 33

Hexists: test Specifies whether the field exists in the hash.

127.0.0.1:6379> hexists user:002 Sex

(integer) 1

127.0.0.1:6379> hexists user:001 Sex

(integer) 0

Hlen: Returns the number of field for the specified hash.

127.0.0.1:6379> Hlen user:001

(integer) 2

127.0.0.1:6379> Hlen user:002

(integer) 4

Hdel: Deletes the field of the specified hash.

127.0.0.1:6379> Hdel user:002 Sex

(integer) 1

127.0.0.1:6379> hget user:002 Sex

(nil)

Hkeys: Returns all field of the hash.

127.0.0.1:6379> Hkeys user:002

1) "id"

2) "Name"

3) "Age"

127.0.0.1:6379> Hkeys user:001

1) "Name"

2) "Age"

Hvals: Returns all value of the hash.

127.0.0.1:6379> hvals user:002

1) "1"

2) "Mark"

3) "33"

Hgetall: Gets all the field and value in a hash.

127.0.0.1:6379> Hgetall user:002

1) "id"

2) "1"

3) "Name"

4) "Mark"

5) "Age"

6) "33"

Redis Learning Lesson Two: Redis hash types and operations

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.