Redis command details and examples of usage scenarios--hash

Source: Internet
Author: User
Tags hash mongodb redis
hdel key field [field ...]

Deletes one or more specified domains in the hash table key, and the nonexistent fields are ignored.
In versions below Redis2.4, Hdel can delete only a single domain at a time, and if you need to delete multiple domains in an atomic time, include the command in the Multi/exec block.
Available versions:
2.0.0+
Complexity of Time:
O (n), n is the number of fields to be deleted.
return value:
The number of domains successfully removed, excluding ignored domains.
Test data

redis> hgetall abbr
1) "a"
2) "Apple"
3) "B"
4) "Banana"
5) "C"
6) "Cat"
7) "D"
8) " Dog

Delete a single domain

Redis> Hdel abbr a
(integer) 1

Delete a nonexistent domain

redis> Hdel abbr not-exists-field
(integer) 0

Delete multiple domains

redis> Hdel abbr b C
(integer) 2
redis> hgetall abbr
1) "D"
2) "Dog"
hexists key Field

View the hash table key for the existence of a given domain field.
Available versions:
2.0.0+
Complexity of Time:
O (1)
return value:
Returns 1 if the hash table contains a given field.
Returns 0 if the hash table does not contain the given domain, or the key does not exist.

redis> hexists phone myphone
(integer) 0
redis> hset phone myphone nokia-1110
(integer) 1
redis > Hexists phone myphone
(integer) 1
hget key Field

Returns the value of the given field field in the hash table key.
Available versions:
2.0.0+
Complexity of Time:
O (1)
return value:
The value of the given field.
Nil is returned when the given domain does not exist or a given key does not exist.
Domain exists

redis> hset site Redis redis.com
(integer) 1
redis> hget site Redis
"redis.com"
 domain does not exist for
Redis > Hget site MySQL
(nil)
Hgetall Key

Returns all the fields and values in the hash table key.
In the return value, followed by each domain name (field name) is the value of the domain (value), so the length of the return value is twice times the size of the hash table.
Available versions:
2.0.0+
Complexity of Time:
O (n), n is the size of the hash table.
return value:
Returns the field and domain values of the Hashtable as a list.
If key does not exist, returns an empty list.

Redis> hset people Jack "Jack Sparrow"
(integer) 1
redis> hset people Gump "Forrest Gump"
(integer) 1
  redis> hgetall People
1) "Jack"          # domain
2) "Jack Sparrow"  # value
3) "Gump"
4) "Forrest Gump"
hincrby key field increment

Adds an increment increment to the value of the field field in the hash table key.
The increment can also be negative, which is equivalent to subtracting a given field.
If key does not exist, a new hash table is created and the Hincrby command is executed.
If the domain field does not exist, the value of the domain is initialized to 0 before the command is executed.
Executing the Hincrby command on a domain field that stores a string value will cause an error.
The value of this operation is limited to a 64-bit (bit) signed numeric representation.
Available versions:
2.0.0+
Complexity of Time:
O (1)
return value:
After the Hincrby command is executed, the value of the field field in the hash table key.
Increment is positive

Redis> hexists counter Page_view    # Set Airspace
(integer) 0
redis> Hincrby counter Page_view
Integer)
redis> hget counter page_view
"" "
increment for negative
redis> hget counter Page_view
"redis>"
Hincrby counter page_view-50
(integer) redis> hget counter
page_view
" 150 "

Attempt to execute the Hincrby command on a domain of string values

redis> hset Myhash string Hello,world       # Sets a String value
(integer) 1
redis> hget myhash string
"Hello , world "
redis> hincrby Myhash string 1              # command execution failed with error.
(Error) ERR hash value is not a integer
redis> hget myhash string                   # Original value unchanged
' Hello,world '
hincrbyfloat key field increment

Adds a floating-point increment increment to the field field in the hash table key.
If there is no domain field in the hash table, Hincrbyfloat will first set the value of the field field to 0 before performing the addition operation.
If the key does not exist, then hincrbyfloat creates a hash table, creates the domain field, and then executes the addition.
An error is returned when any one of the following conditions occurs:
The value of field field is not a string type (because both numbers and floating-point number in Redis are saved as strings, so they all belong to string types)
The current value of the field field or the given increment increment cannot be interpreted (parse) as a double-precision floating-point number (double precision floating
The detailed functionality of the Hincrbyfloat command is similar to the Incrbyfloat command, see the incrbyfloat command for more information.
Available versions:
2.6.0+
Complexity of Time:
O (1)
return value:
The value of the field field after the addition operation is performed.
Both the value and the increment are ordinary decimals

redis> hset mykey field 10.50
(integer) 1
redis> hincrbyfloat mykey field 0.1
"10.6"

Both the value and the increment are exponential symbols

redis> hset mykey field 5.0e3
(integer) 0
redis> hincrbyfloat mykey field 2.0e2
"5200"
to non-existent key execution Hincrbyfloat
redis> EXISTS price
(integers) 0
redis> hincrbyfloat price Milk 3.5
"3.5"
Redis > Hgetall Price
1) "Milk"
2) "3.5"
for non-existent fields hincrbyfloat
redis> hgetall price
1) "Milk" C14/>2) "3.5"
redis> hincrbyfloat Price Coffee 4.5   # added coffee domain
"4.5"
redis> hgetall price< c19/>1) "Milk"
2) "3.5"
3) "Coffee"
4) "4.5"
Hkeys Key

Returns all the fields in the hash table key.
Available versions:
2.0.0+
Complexity of Time:
O (n), n is the size of the hash table.
return value:
A table that contains all the fields in the hash table.
Returns an empty table when key does not exist.
Hash table non-empty

redis> hmset website Google www.google.com Yahoo www.yahoo.com
OK
redis> hkeys website
1) "Google"
2) "Yahoo"
empty hash table/key does not exist
redis> EXISTS fake_key
(integer) 0
redis> hkeys fake_key
(empty List or set)
Hlen Key

Returns the number of fields in the hash table key.
Complexity of Time:
O (1)
return value:
The number of fields in the hash table.
Returns 0 when key does not exist.

redis> Hset db redis redis.com
(integer) 1
redis> hset db mysql mysql.com
(integer) 1
redis> HLE N db
(integer) 2
redis> hset db mongodb mongodb.org
(integer) 1
redis> Hlen db
(integer) 3
hmget key field [field ...]

Returns the value of one or more given fields in a hash table key.
Returns a nil value if the given domain does not exist in the hash table.
Because the nonexistent key is treated as an empty hash table, a hmget operation on a nonexistent key returns a table with a nil value.
Available versions:
2.0.0+
Complexity of Time:
O (n), n is the number of a given field.
return value:
A table that contains the associated values for a given field, and the table values are arranged in the same order as the request order for the given domain parameters.

redis> hmset pet dog "Doudou" Cat "Nounou"    # Set multiple domains
at once ok
redis> Hmget pet dog cat Fake_pet             # return value of Same order as incoming parameters
1) "Doudou"
2) "Nounou"
3) (nil)   
hmset key field value [Field value ...]

Set multiple Field-value (domain-value) pairs to the hash table key at the same time.
This command overwrites a domain that already exists in the hash table.
If the key does not exist, an empty hash table is created and the Hmset operation is performed.
Available versions:
2.0.0+
Complexity of Time:
O (n), n is the number of field-value pairs.
return value:
Returns OK if the command executes successfully.
When key is not a hash table (hash) type, an error is returned.

redis> hmset website Google www.google.com Yahoo www.yahoo.com
ok
redis> hget website Google
" www.google.com "
redis> hget website yahoo
" www.yahoo.com "   
hset key field value

Set the value of the domain field in the hash table key to value.
If the key does not exist, a new hash table is created and the Hset operation is performed.
If the field field already exists in the hash table, the old value will be overwritten.
Available versions:
2.0.0+
Complexity of Time:
O (1)
return value:
If field is a new domain in the hash table, and the value is set successfully, return 1.
Returns 0 if the field field in the hash table already exists and the old value has been overwritten by the new value.

redis> Hset website google "www.g.cn"       # Set a new domain
(integer) 1
redis> hset website google "www.google.com" # Overwrite an old domain
(integer) 0    
hsetnx key field value

Set the value of the field field in the hash table key to value if and only if the domain field does not exist.
If the field field already exists, the operation is not valid.
If key does not exist, a new hash table is created and the HSETNX command is executed.
Available versions:
2.0.0+
Complexity of Time:
O (1)
return value:
Set success, return 1.
Returns 0 if the given domain already exists and no operations are performed.

redis> hsetnx nosql key-value-store redis
(integer) 1
redis> hsetnx nosql key-value-store redis       # operation invalid , domain Key-value-store already exists
(integer) 0
hvals Key

Returns the value of all fields in the hash table key.
Available versions:
2.0.0+
Complexity of Time:
O (n), n is the size of the hash table.
return value:
A table that contains all the values in the hash table.
Returns an empty table when key does not exist.
Non-empty hash table

redis> hmset website Google www.google.com Yahoo www.yahoo.com
ok
redis> hvals website
1) " www.google.com "
2)" www.yahoo.com "
empty hash Table/nonexistent key
redis> EXISTS not_exists
(integer) 0
Redis> hvals not_exists
(empty list or set)
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.