Redis data type-Hash

Source: Internet
Author: User
-- Reprinted:

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 program on the shell command line/> redis-cli # Set the field to field1 for the key value of myhash, the value is Stephen. Redis 127.0.0.1: 6379> hset myhash field1 "Stephen" (integer) 1 # obtain the value of the key value myhash and field field1. 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. Its 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 # judge whether the field name in the myhash key is field1. Because of this, the return value is 1. Redis 127.0.0.1: 6379> hexists myhash field1 (integer) 1 # delete a field named 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 name 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 # determine whether the field1 field exists in the myhash key, because 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 a new field field1 to myhash, whose value is Stephen because the field has been deleted, therefore, this 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, because this command returns 0 without any operation. Redis 127.0.0.1: 6379> hsetnx myhash field1 Stephen (integer) 0 2. hincrby: # Delete this key to facilitate subsequent tests. Redis 127.0.0.1: 6379> Del myhash (integer) 1 # Prepare the test data. The field value of this myhash is 1. Redis 127.0.0.1: 6379> hset myhash Field 5 (integer) 1 # Add 1 to the value of the field in 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, and return the added result. Redis 127.0.0.1: 6379> hincrby myhash field-1 (integer) 5 # Add-10 to the field value of myhash, and return the added result. Redis 127.0.0.1: 6379> hincrby myhash field-10 (integer)-5 3. hgetall/hkeys/hvals/hmet/hmset: # Delete this key to facilitate subsequent testing. Redis 127.0.0.1: 6379> Del myhash (integer) 1 # for this key myhash, multiple fields are set 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 in the returned result is nil. 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" # only obtain the names of all fields in the myhash key. Redis 127.0.0.1: 6379> hkeys myhash 1) "field1" 2) "field2" # only obtain the values of all fields in the myhash key. Redis 127.0.0.1: 6379> hvals myhash 1) "Hello" 2) "world"

 

Redis data type-Hash

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.