Redis learning Manual (hashes data type)

Source: Internet
Author: User

I. Overview:

We can regard the hashes type in redis as a map container with string key and string value. Therefore, this type is suitable for storing the information of value objects. Such as username, password, and age. If the hash contains very few fields, this type of data will only occupy a small amount of disk space. Each hash can store 4294967295 key-value pairs.

Ii. Related command list:

Command prototype Time Complexity Command description Return Value
HsetKey Field Value O (1) Set the field/value pair for the specified key. If the key does not exist, this command creates a new key to use the field/value pair in the parameter. If the field in the parameter already exists in the key, overwrite the original value with the new value. 1 indicates that the new field is set with a new value, 0 indicates that the field already exists, and the new value overwrites the original value.
HgetKey Field O (1) Returns the associated value of the specified field in the specified key. Return the value associated with the field in the parameter. If the key or field in the parameter is not stored, return nil.
HexistsKey Field O (1) Determines whether the specified field in the specified key exists. 1 indicates existence, 0 indicates that the field or key in the parameter does not exist.
HlenKey O (1) Obtain the number of fields contained in the key. Returns the number of fields contained in the key. If the key does not exist, 0 is returned.
HdelKey field [field...] O (N) N in time complexity indicates the number of fields to be deleted in the parameter. Delete multiple fields specified in the parameter from the hashes value of the specified key. If the field does not exist, it is ignored. If the key does not exist, it is treated as a null hashes and 0 is returned. The number of actually Deleted fields.
HsetnxKey Field Value O (1)
If the key or field in the parameter does not exist, set the field/value pair for the specified key. Otherwise, the command will not perform any operation. 1 indicates that the new field is set with a new value. 0 indicates that the key or field already exists. This command does not perform any operation.
HincrbyKey field Increment O (1)
Adds the value of the value associated with the specified field in the specified key. If the key or field does not exist, this command creates a new key or field, initializes its associated value to 0, and then specifies the operation for adding a number. The number supported by this command is a 64-bit signed integer, that is, the increment can be negative. Returns the value after the operation.
HgetallKey O (N) N in time complexity indicates the number of fields contained in the key. Obtain all fields/values contained in the key. The return format is a field, a value, and so on. Field/Value List.
HkeysKey O (N) N in time complexity indicates the number of fields contained in the key. Returns all fields names of the specified key. Field List.
HvalsKey O (N) N in time complexity indicates the number of fields contained in the key. Returns all values names of the specified key. Value List.
HmetKey field [field...] O (N) N in time complexity indicates the number of requested fields. Gets a set of values associated with the specified fields in the parameter. If the requested field does not exist, the returned value is nil. If the key does not exist, this command treats it as an empty hash, so a group of Nil is returned. Return a group of values associated with the request fields. The return order is the same as the request order of fields.
HmsetKey field value [field value...] O (N) N in time complexity indicates the number of fields to be set. Set the field/value pairs in the parameters one by one. If a field already exists, the original value is overwritten with the new value. If the key does not exist, a new key is created and field/value in the parameter is set.   

Iii. Command example:

1. hset/hget/hdel/hexists/hlen/hsetnx:
# Start the redis client on the shell command lineProgram
/> Redis-cli
# Set the field to field1 and the value to Stephen for the key whose key value is myhash.
Redis 127.0.0.1: 6379> Hset myhash field1 "Stephen"
(Integer) 1
# Obtain the value of field1, whose key value is myhash.
Redis 127.0.0.1: 6379>Hget myhash field1
"Stephen"
# The field2 field does not exist in the myhash key, so NIL is returned.
Redis 127.0.0.1: 6379> Hget myhash field2
(Nil)
# Add a new field field2 to the hashes value associated with myhash, whose value is Liu.
Redis 127.0.0.1: 6379> Hset myhash field2 "Liu"
(Integer) 1
# Obtain the number of myhash key fields.
Redis 127.0.0.1: 6379> Hlen myhash
(Integer) 2
# Determine whether a field with the field name field1 exists in the myhash key. The returned value is 1 because it exists.
Redis 127.0.0.1: 6379> Hexists myhash field1
(Integer) 1
# Delete the field field1 in the myhash key. If the field is deleted successfully, 1 is returned.
Redis 127.0.0.1: 6379> Hdel myhash field1
(Integer) 1
# Delete the field field1 in the myhash key again. Because the previous command has deleted the field, 0 is returned because it is not deleted.
Redis 127.0.0.1: 6379> Hdel myhash field1
(Integer) 0
# Check whether the field1 field exists in the myhash key. The previous command has deleted it because 0 is returned.
Redis 127.0.0.1: 6379> Hexists myhash field1
(Integer) 0
# Use the hsetnx command to add the new field field1 to myhash with the value of Stephen. Because the field has been deleted, the command is successfully added and 1 is returned.
Redis 127.0.0.1: 6379>Hsetnx myhash field1 Stephen
(Integer) 1
# Because the field1 field of myhash has been successfully added through the previous command, this command returns 0 without any operation.
Redis 127.0.0.1: 6379> Hsetnx myhash field1 Stephen
(Integer) 0

2. hincrby:
# Delete the key to facilitate subsequent tests.
Redis 127.0.0.1: 6379> Del myhash
(Integer) 1
# Prepare the test data. The field value of myhash is 1.
Redis 127.0.0.1: 6379> Hset myhash Field 5
(Integer) 1
# Add 1 to the field value of myhash and return the added result.
Redis 127.0.0.1: 6379> Hincrby myhash Field 1
(Integer) 6
# Add-1 to the field value of myhash to return the added result.
Redis 127.0.0.1: 6379> Hincrby myhash field-1
(Integer) 5
# Add-10 to the field value of myhash to return the added result.
Redis 127.0.0.1: 6379> Hincrby myhash field-10
(Integer)-5

3. hgetall/hkeys/hvals/hmet/hmset:
# Delete the key to facilitate subsequent testing.
Redis 127.0.0.1: 6379> Del myhash
(Integer) 1
# For this key myhash, set multiple fields at one time, namely field1 = "hello", field2 = "world ".
Redis 127.0.0.1: 6379> Hmset myhash field1 "hello" field2 "world"
OK
# Obtain multiple fields of the myhash key. field3 does not exist because the value corresponding to this field is nil in the returned result.
Redis 127.0.0.1: 6379> Hmet myhash field1 field2 field3
1) "Hello"
2) "world"
3) (nil)
# Return all fields of the myhash key and their values. From the results, we can see that they are listed on a per-pair basis.
Redis 127.0.0.1: 6379> Hgetall myhash
1) "field1"
2) "Hello"
3) "field2"
4) "world"
# Obtain only the names of all fields in the myhash key.
Redis 127.0.0.1: 6379> Hkeys myhash
1) "field1"
2) "field2"
# Obtain only the values of all fields in the myhash key.
Redis 127.0.0.1: 6379> Hvals myhash
1) "Hello"
2) "world"

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.