HASH type of Redis Data Type

Source: Internet
Author: User

HASH type of Redis Data Type

 

HASH type-Features

Redis hash is a ing table between fields and values of the string type. Its Addition and deletion operations are O (1) (average ).
Hash is particularly suitable for Object Storage. Compared to saving each field of an object to a single string type. Storing an object in the hash type consumes less memory and makes it easier to access the entire object. Memory saving is because zipmap (also known as small hash) is used to store a new hash object. This zipmap is not actually a hash table, but zipmap can save a lot of metadata storage overhead for hash compared to normal hash implementation. Although zipmap's addition, deletion, and search operations are all O (n), the number of fields of general objects is not large. Therefore, the use of zipmap is also very fast, that is to say, the average addition and deletion is still O (1 ). If the size of the field or value exceeds the limit, Redis will automatically replace zipmap with a normal hash internally. this restriction can be specified in the configuration file hash-max-zipmap-entries 64 # The configuration field can contain up to 64 hash-max-zipmap-value 512 # The maximum value configured is 512 bytes.

Common HASH commands

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[15]> HSET website google "www.g.cn"(integer) 1127.0.0.1:6379[15]>  HSET website google "www.google.com"(integer) 0

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[15]> HSETNX nosql key-value-store redis(integer) 1127.0.0.1:6379[15]> HSETNX nosql key-value-store Memcached(integer) 0127.0.0.1:6379[15]> HGET nosql key-value-store"redis"

HMSET
HMSET key field value [field value…]
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 HMSET is executed.
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.

127.0.0.1:6379[15]> HMSET website google www.google.com yahoo www.yahoo.comOK127.0.0.1:6379[15]> HGET website google"www.google.com"127.0.0.1:6379[15]> HGET website yahoo"www.yahoo.com"

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[15]> HSET site redis redis.com(integer) 1127.0.0.1:6379[15]> HGET site redis"redis.com"127.0.0.1:6379[15]> HGET site mysql(nil)

Hmet
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 key that does not exist is processed as an empty hash table, performing the hmet operation on a non-existent key will return a table with only nil values.
Time Complexity: O (N), N is the number of given domains.
Return Value: a table that contains the associated values of multiple given domains. The table values are arranged in the same order as the request order for the given domain parameters.

127.0.0.1:6379[15]> HMSET pet dog "doudou" cat "nounou"OK127.0.0.1:6379[15]> HMGET pet dog cat fake_pet1) "doudou"2) "nounou"3) (nil)

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[15]> HEXISTS phone myphone(integer) 0127.0.0.1:6379[15]> HSET phone myphone nokia-1110(integer) 1127.0.0.1:6379[15]> HEXISTS phone myphone(integer) 1

HDEL
HDEL key field [field…]
Delete one or more specified fields in the hash table key. nonexistent fields are ignored.
Time Complexity: O (N), N is the number of domains to be deleted.
Returned value: the number of successfully removed fields, excluding the ignored fields.

127.0.0.1:6379[15]> HMSET key f1 "v1" f2 "v2" f3 "v3" f4 "v4"OK127.0.0.1:6379[15]> HGETALL key1) "f1"2) "v1"3) "f2"4) "v2"5) "f3"6) "v3"7) "f4"8) "v4"127.0.0.1:6379[15]> HDEL key f1(integer) 1127.0.0.1:6379[15]> HDEL key not-field(integer) 0127.0.0.1:6379[15]> HDEL key f2 f3(integer) 2127.0.0.1:6379[15]> HDEL key f4 f1(integer) 1

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.

127.0.0.1:6379[15]> HMSET website google www.google.com yahoo www.yahoo.comOK127.0.0.1:6379[15]> HKEYS website1) "google"2) "yahoo"127.0.0.1:6379[15]> EXISTS fake_key(integer) 0127.0.0.1:6379[15]> HKEYS fake_key(empty list or set)

HVALS
HVALS key
Returns the values of 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 values in the hash table.
If the key does not exist, an empty table is returned.

127.0.0.1:6379[15]> HMSET website google www.google.com yahoo www.yahoo.comOK127.0.0.1:6379[15]> HVALS website1) "www.google.com"2) "www.yahoo.com"127.0.0.1:6379[15]> EXISTS not_exists(integer) 0127.0.0.1:6379[15]> HVALS not_exists(empty list or set)

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[15]> HSET people jack "Jack Sparrow"(integer) 1127.0.0.1:6379[15]> HSET people gump "Forrest Gump"(integer) 1127.0.0.1:6379[15]> HGETALL people1) "jack"2) "Jack Sparrow"3) "gump"4) "Forrest Gump"

HINCRBY
HINCRBY key field increment
Add incremental increment to the field value in the key of the hash table.
An increment can also be a negative number, which is equivalent to performing a subtraction operation on a given domain.
If the key does not exist, a new hash table is created and run the HINCRBY command.
If the field does not exist, the field value is initialized to 0 before the command is executed.
Executing the HINCRBY command on a field that stores string values may cause an error.
The value of this operation is limited to 64-bit Signed numbers.
Time Complexity: O (1)
Returned value: the field value in the key of the hash table after the HINCRBY command is executed.

127.0.0.1:6379[15]> HEXISTS counter page_view(integer) 0127.0.0.1:6379[15]> HINCRBY counter page_view 200(integer) 200127.0.0.1:6379[15]> HGET counter page_view"200"127.0.0.1:6379[15]> HINCRBY counter page_view -50(integer) 150127.0.0.1:6379[15]> HGET counter page_view"150"

HINCRBYFLOAT
HINCRBYFLOAT key field increment
Add the floating point incremental increment to the field in the key of the hash table.
If there is no field in the hash table, HINCRBYFLOAT sets the field value to 0 and then performs addition.
If the key does not exist, HINCRBYFLOAT will first create a hash table, then create a field, and then perform the addition operation.
If any of the following conditions occurs, an error is returned:
Field Values in the field are not of the string type (because the numbers and floating-point numbers in redis are saved as strings, they all belong to the string type)
The current value of the field or the given incremental increment cannot be interpreted as a double precision floating point number)
Returned value: the value of the field after the addition operation is executed.

127.0.0.1:6379[15]> HSET mykey field 10.50(integer) 0127.0.0.1:6379[15]> HINCRBYFLOAT mykey field 0.1"10.6"127.0.0.1:6379[15]> HSET mykey field 5.0e3(integer) 0127.0.0.1:6379[15]> HINCRBYFLOAT mykey field 2.0e2"5200"127.0.0.1:6379[15]> EXISTS price(integer) 0127.0.0.1:6379[15]> HINCRBYFLOAT price milk 3.5"3.5"127.0.0.1:6379[15]> HGETALL price1) "milk"2) "3.5"

HLEN
HLEN key
Returns the number of fields in the hash table key.
Return Value:
Number of domains in the hash table.
If the key does not exist, 0 is returned.

127.0.0.1:6379[15]> HSET db redis redis.com(integer) 1127.0.0.1:6379[15]> HSET db mysql mysql.com(integer) 1127.0.0.1:6379[15]> HLEN db(integer) 2127.0.0.1:6379[15]> HSET db mongodb mongodb.org(integer) 1127.0.0.1:6379[15]> HLEN db(integer) 3

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.