Redis Data type: Hashes

Source: Internet
Author: User

Redis Hash is a string-type field and value mapping table.

It's add, delete operations are O (1) (average), hash is particularly suitable for storing objects

Storing an object in a hash type always consumes less memory and makes it easier to access

The entire object.

Hset method: Sets the hash field to the specified value, and if key does not exist, first create

127.0.0.1:6379> hset user:001 Name Xiaoming
(integer) 1
127.0.0.1:6379> hget user:001 Name
"Xiaoming"

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

Returns 0 if present.

127.0.0.1:6379> hsetnx user:001 name Xiaohong
(integer) 0

Hmset method: Batch Set hash field

127.0.01:6379> Get user:002 Name
(Error) ERR wrong number of arguments for ' get ' command
127.0.01:6379> hget user:002 Name
"Xiaoming"
127.0.01:6379> Hget user:002 Age
"10"
127.0.01:6379> Hget user:002 Gender
"1"

Hget method: Gets the value of the filed specified by the hash

Hmget method: Bulk Get hash specified filed value

127.0.01:6379> hmget user:002 Name Age gender
1) "Xiaoming"
2) "10"
3) "1"

Hincrby method: Specifies the hash field plus the specified value

127.0.01:6379> Hincrby user:002 age 8
(integer) 18
127.0.01:6379> hmget user:002 Name Age gender
1) "Xiaoming"
2) "18"
3) "1"

Hexists method: Test whether the specified field exists, existence returned 1

127.0.01:6379> hexists user:002 Name
(integer) 1

Hlen method: Returns the number of keys for the specified hash

127.0.01:6379> Hlen user:002
(integer) 3

Hdel method: Delete the filed of the specified hash

127.0.01:6379> Hdel user:002 Age
(integer) 1
127.0.01:6379> Hget user:002 Age
(nil)

Hkeys method: Returns all field of hash

127.0.01:6379> Hkeys user:002
1) "Name"
2) "Gender"

Hvals method: Returns all the value of a hash

127.0.01:6379> hvals user:002
1) "Xiaoming"
2) "1"

Hgetall method: Returns all fields and their corresponding values

127.0.01:6379> Hgetall user:002
1) "Name"
2) "Xiaoming"
3) "Gender"
4) "1"

Redis Data type: Hashes

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.