HASH type of Redis Data Type

Source: Internet
Author: User
Redishash is a string ing table between fields and values of the string type. The addition and deletion operations are O (1) (average ). Hash is particularly suitable for storing

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 storing

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> "" (integer) 1127> "" (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> (integer) 1127> (integer) 0127> "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> 127> "" 127> ""
  • 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> (integer) 1127> "redis.com" 127> (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.

    [. [15]> hmet pet dog cat fake_pet) (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> (integer) 0127> (integer) 1127> (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.

    : 6379 [15]> HMSET key f1 "v1" f2 "v2" f3 "v3" f4 "v4" OK :)): 6379 [15]> HDEL key f1 (: 6379 [15]> HDEL key not-field (: 6379 [15]> HDEL key f2 f3 (: 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> 127> HKEYS website1) "google" 2) "yahoo" 127> EXISTS fake_key (integer) 0127> HKEYS fake_key ()
  • 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> 127> HVALS website1) "" 2) "" 127> EXISTS not_exists (integer) 0127> HVALS not_exists ()
  • 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.

    : 6379 [15]> HSET people jack "Jack Sparrow" (: 6379 [15]> HSET people gump "Forrest Gump" (: 6379 [15]> HGETALL people) "gump" 4) "Forrest Gump"
  • 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.