Beckham _ redis hash type learning, Beckham _ redishash type

Source: Internet
Author: User

Beckham _ redis hash type learning, Beckham _ redishash type

Redis Hash type

1. View hash commands

Ii. Detailed description of operating hash commands

 

1. View hash commands

1. Enter help @ hash

127.0.0.1: 6379> help @ hash

HDEL key field [field...]

Summary: Delete one or more hash fields

Since: 2.0.0

 

HEXISTS key field

Summary: Determine if a hash field exists

Since: 2.0.0

 

HGET key field

Summary: Get the value of a hash field

Since: 2.0.0

 

HGETALL key

Summary: Get all the fields and values in ahash

Since: 2.0.0

 

HINCRBY key field increment

Summary: Increment the integer value of ahash field by the given number

Since: 2.0.0

Note: because the length is too large, it is omitted.

 

 

Ii. Detailed description of operating hash commands

1. hset

Hset key field value

Set the field value in the key of the hash table to value.

If the key does not exist, a new hash table is created and hset is performed. If the field already exists in the hash table, the old value will be overwritten.

Time Complexity:

O (1)

Return Value:

If the field is a new field in the hash table and the value is set successfully, 1 is returned.

If the field in the hash table already exists and the old value is overwritten by the new value, 0 is returned.

127.0.0.1: 6379> hset website google "www.g.cn" # A New Domain

(Integer) 1

127.0.0.1: 6379> hset website google "www.google.com" # overwrite an old domain

(Integer) 0

 

2. hsetnx

Hsetnx key field value

Set the field value in the key of the hash table to value, if and only if the field does not exist.

This operation is invalid if the field already exists.

If the key does not exist, a new hash table is created and the hsetnx command is executed.

Time Complexity:

O (1)

Return Value:

Set successfully. 1 is returned.

If the specified domain already exists and no operation is executed, 0 is returned.

127.0.0.1: 6379> hsetnx nosql key-value-store redis

(Integer) 1

127.0.0.1: 6379> hsetnx nosql key-value-store redis # The operation is invalid. The domain key-value-store already exists.

(Integer) 0

 

3. hmset

Hmset key field value [fieldvalue...]

Set multiple field-value pairs to the hash table key.

This command overwrites the existing fields in the hash table. If the key does not exist, an empty hash table is created and executed.HmsetOperation.

Time Complexity:

O (N), N is the number of field-value pairs.

Return Value:

If the command is successfully executed, OK is returned.

If the key type is not hash, an error is returned.

# Case 1: hash table

127.0.0.1: 6379> hmset website google www.google.com yahoowww.yahoo.com

OK

127.0.0.1: 6379> hget website google

"Www.google.com"

127.0.0.1: 6379> hget website yahoo

"Www.yahoo.com"

# Case 2: Type Error

127.0.0.1: 6379> set G 10 # error

OK

127.0.0.1: 6379> hmset G name huangz age 20

(Error) ERR Operation against a key holding the wrongkind of value

 

4. hget

Hget key field

Returns the value of the specified field in the hash table key.

Time Complexity:

O (1)

Return Value:

The value of the given domain.

If the specified domain does not exist or the specified key does not exist, nil is returned.

127.0.0.1: 6379> hset huangz blog huangz.iteye.com

(Integer) 1

 

127.0.0.1: 6379> hget huangz blog

"Huangz.iteye.com"

 

5. hmet

Hget key field [field...]

Returns the values of one or more given fields in the hash table key.

If the given domain does not exist in the hash table, an nil value is returned.

Because a non-existing key is processed as an empty hash tableHmetThe operation returns a table with only nil values.

Time Complexity:

O (N), N is the number of given domains.

Return Value:

A table contains the associated values of multiple given fields. The table values are arranged in the same order as the request order of the given domain parameters.

127.0.0.1: 6379> hmset pet dog "doudou" cat "nounou" # Save multiple values at a time

OK

127.0.0.1: 6379> hmet pet dog cat fake_pet # The returned values are in the same order as those of input parameters.

1) "doudou"

2) "nounou"

3) (nil) # nil value returned for a nonexistent domain

6. hgetall

Hgetall key

Returns all fields and values in the hash table key.

In the return value, each domain name (field name) is followed by the value of the domain, so the return value is twice the size of the hash table.

Time Complexity:

O (N), N is the size of the hash table.

Return Value:

Return the values of the fields and fields in the hash table in the form of a list. If the key does not exist, an empty list is returned.

127.0.0.1: 6379> hset hash_name jack "Jack Sparrow"

(Integer) 1

127.0.0.1: 6379> hset hash_name gump "Forrest Gump"

(Integer) 1

127.0.0.1: 6379> hgetall hash_name

1) "jack" # domain

2) "Jack Sparrow" # Value

3) "gump"

4) "Forrest Gump"

7. hdel

Hdel key field [field...]

Deletes one or more specified fields in the hash table key.

The Nonexistent domain is ignored.

Time Complexity:

O (N) and N are the number of domains to be deleted.

Return Value:

If the domain exists and is successfully deleted, 1 is returned.

If the key does not exist or the domain does not exist, 0 is returned.

Annotation

In versions earlier than Redis2.2 and 2.2, hdel can only delete a single domain at a time. If you need to delete multiple domains within an atomic time, please include the command in the MULT/EXEC block.

127.0.0.1: 6379> hset hash_name jack "Jack Sparrow"

(Integer) 1

127.0.0.1: 6379> hget hash_name jack

"Jack Sparrow"

127.0.0.1: 6379> hdel hash_name jack

(Integer) 1

127.0.0.1: 6379> hget hash_name jack

(Nil)

8. hlen

Hlen key

Returns the number of fields in the hash table key.

Time Complexity:

O (1)

Return Value:

Number of domains in the hash table.

If the key does not exist, 0 is returned.

127.0.0.1: 6379> hset hash_name jack "Jack Sparrow"

(Integer) 1

127.0.0.1: 6379> hset hash_name gump "Forrest Gump"

(Integer) 1

127.0.0.1: 6379> hlen hash_name

(Integer) 2

9. hexists

Hexists key field

Check whether the specified field exists in the key of the hash table.

Time Complexity:

O (1)

Return Value:

If the hash table contains a given field, 1 is returned.

If the hash table does not contain a specified field or the key does not exist, 0 is returned.

127.0.0.1: 6379> hexists phone myphone

(Integer) 0

 

127.0.0.1: 6379> hset phone myphone nokia-1110

(Integer) 1

 

127.0.0.1: 6379> hexists phone myphone

(Integer) 1

10. hincrby

Hincrby key field increment

Add incremental increment to the field value in the key of the hash table.

If the key does not exist, a new hash table is created and run the hincrby command.

If the field does not exist, or the field's existing string value cannot be expressed as a number, the field value is set to 0 before the command is executed.

The value of this operation is limited to 64-bit Signed numbers.

Time Complexity:

O (1)

Return Value:

RunHincrbyAfter the command, hash the field value in the key of the table.

127.0.0.1: 6379> hexists hash_count page_views

(Integer) 0

 

127.0.0.1: 6379> hincrby hash_count page_views 200

(Integer) 200

 

127.0.0.1: 6379> hincrby hash_count page_views 10

(Integer) 210

11. hkeys

Hkeys key

Returns all fields in the hash table key.

Time Complexity:

O (N), N is the size of the hash table.

Return Value:

A table that contains all the fields in the hash table.

If the key does not exist, an empty table is returned.

# Case 1: the hash table is not empty.

127.0.0.1: 6379> hmset website google www.google.com yahoowww.yahoo.com

OK

127.0.0.1: 6379> hkeys website

1) "google"

2) "yahoo"

# Case 2: The empty hash table/key does not exist

127.0.0.1: 6379> exists fake_key

(Integer) 0

127.0.0.1: 6379> hkeys fake_key

(Empty list or set)

12. hvals

Hvals key

Returns all values of the hash table key.

Time Complexity:

O (N), N is the size of the hash table.

Return Value:

A table that contains all values in the hash table.

If the key does not exist, an empty table is returned.

# Case 1: non-empty hash table

127.0.0.1: 6379> hmset website google www.google.com yahoowww.yahoo.com

OK

 

127.0.0.1: 6379> hvals website

1) "www.google.com"

2) "www.yahoo.com"

# Case 2: empty hash table/nonexistent key

127.0.0.1: 6379> exists not_exists

(Integer) 0

127.0.0.1: 6379> hvals not_exists

(Empty list or set)

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.