Redis Tutorial (iv): Hashes data types

Source: Internet
Author: User
Tags redis tutorial

Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/132.html?1455807574

First, overview:

We can consider the hashes type in Redis as a map container with string key and string value. Therefore, this type is ideal for storing information about value objects. such as username, password, and age. If a hash contains very few fields, the data of that type will take up only a small amount of disk space. Each hash can store 4,294,967,295 key-value pairs.

Ii. List of related commands:

Command prototypes Complexity of Time Command description return value
hset key field value O (1) Sets the field/value pair for the specified key, and if key does not exist, the command creates a new key with the field/value pair in the argument, overwriting its original value with the new value if the field in the argument already exists in the key. 1 indicates that the new field is set with a new value, 0 indicates that field already exists, and overwrites the original value with the new value.
hget key field O (1) Returns the associated value for the specified field in the specified key. Returns the associated value of field in the parameter, or nil if the key or field in the argument does not exist.
hexistskey field O (1) Determines whether the specified field in the specified key exists. 1 is present, and 0 indicates that the field or key in the parameter does not exist.
Hlen Key O (1) Gets the number of field that the key contains. Returns the number of field keys contained in the key, or 0 if key does not exist.
hdel key field [field ...] O (N) N In time complexity indicates the number of fields to be deleted in the parameter. Removes the multiple fields specified in the parameter from hashes value of the specified key if the nonexistent field is ignored. If the key does not exist, it is treated as an empty hashes and returns 0. The number of field actually deleted.
hsetnxkey field value O (1) A Field/value pair is set for the specified key only if the key or field in the argument does not exist, otherwise the command will not do anything. 1 indicates that the new field is set to a new value, and 0 indicates that the key or field already exists and that the command does nothing.
hincrbykey field increment O (1) Increments the value of value that is associated with the specified field in the specified key. If the key or field does not exist, the command creates a new key or a new field, initializes its associated value to 0, and then specifies the operation to increment the number. The number supported by this command is a 64-bit signed integer, which means that increment can be negative. Returns the value after the operation.
HgetallKey O (N) N In time complexity indicates the number of field numbers that key contains. Gets all the field/value that the key contains. Its return format is a field, a value, and so on. A list of field/value.
HkeysKey O (N) N In time complexity indicates the number of field numbers that key contains. Returns all fields names for the specified key. A list of field.
hvalsKey O (N) N In time complexity indicates the number of field numbers that key contains. Returns all the values for the specified key. The list of value.
hmgetkey field [field ...] O (N) N In time complexity indicates the number of field requests. Gets a set of values that is associated with the fields specified in the parameter. If the requested field does not exist, its value returns nil. If key does not exist, the command treats it as an empty hash and returns a set of nil. Returns and requests a set of values that are associated with a fields, whose return order is equivalent to the order of fields in the request.
hmset key field value [field value ...] O (N) N In time complexity indicates the number of field sets that are set. Set the Field/value pairs given in the parameters on a per-pair basis. If one of the field already exists, the original value is overwritten with the new value. If key does not exist, create a new key and set the Field/value in the parameter.

#p # pagination Title #e#

Examples of commands:

1. Hset/hget/hdel/hexists/hlen/hsetnx:

The code is as follows:


#在Shell命令行启动Redis客户端程序
/> REDIS-CLI
#给键值为myhash的键设置字段为field1, the value is Stephen.
Redis 127.0.0.1:6379> hset myhash field1 "Stephen"
(integer) 1
#获取键值为myhash, field is the value of field1.
Redis 127.0.0.1:6379> hget Myhash field1
"Stephen"
#myhash键中不存在field2字段, so return nil.
Redis 127.0.0.1:6379> hget Myhash field2
(nil)
#给myhash关联的Hashes值添加一个新的字段field2, the value is Liu.
Redis 127.0.0.1:6379> hset myhash field2 "Liu"
(integer) 1
#获取myhash键的字段数量.
Redis 127.0.0.1:6379> Hlen Myhash
(integer) 2
#判断myhash键中是否存在字段名为field1的字段, the return value is 1 because it exists.
Redis 127.0.0.1:6379> hexists Myhash field1
(integer) 1
#删除myhash键中字段名为field1的字段, delete successfully returns 1.
Redis 127.0.0.1:6379> Hdel Myhash field1
(integer) 1
#再次删除myhash键中字段名为field1的字段, because the previous command has deleted it, because it is not deleted, 0 is returned.
Redis 127.0.0.1:6379> Hdel Myhash field1
(integer) 0
#判断myhash键中是否存在field1字段, because the previous command has deleted it because it returns 0.
Redis 127.0.0.1:6379> hexists Myhash field1
(integer) 0
#通过hsetnx命令给myhash添加新字段field1, the value is Stephen, because the field has been deleted, so the command was added successfully and returned 1.
Redis 127.0.0.1:6379> hsetnx Myhash field1 Stephen
(integer) 1
#由于myhash的field1字段已经通过上一条命令添加成功, because this command returns 0 without any action.
Redis 127.0.0.1:6379> hsetnx Myhash field1 Stephen
(integer) 0


2. Hincrby: #p # page Title #e#

The code is as follows:


#删除该键 to facilitate testing of the following example.
Redis 127.0.0.1:6379> del Myhash
(integer) 1
#准备测试数据, the Field field of the Myhash is set to a value of 1.
Redis 127.0.0.1:6379> hset myhash Field 5
(integer) 1
#给myhash的field字段的值加1, returns the result of the addition.
Redis 127.0.0.1:6379> hincrby myhash Field 1
(integer) 6
#给myhash的field字段的值加-1, returns the result of the addition.
Redis 127.0.0.1:6379> Hincrby Myhash field-1
(integer) 5
#给myhash的field字段的值加-10, returns the result of the addition.
Redis 127.0.0.1:6379> Hincrby Myhash field-10
(integer)-5


3. Hgetall/hkeys/hvals/hmget/hmset:

The code is as follows:


#删除该键 to facilitate the subsequent sample testing.
Redis 127.0.0.1:6379> del Myhash
(integer) 1
#为该键myhash, set multiple fields at once, field1 = "Hello", field2 = "World".
Redis 127.0.0.1:6379> hmset myhash field1 "Hello" field2 "World"
Ok
#获取myhash键的多个字段, where field3 does not exist because the value that corresponds to the field in the returned result is nil.
Redis 127.0.0.1:6379> hmget myhash field1 field2 field3
1) "Hello"
2) "World"
3) (nil)
#返回myhash键的所有字段及其值, as can be seen from the results, they are listed on a per-pair basis.
Redis 127.0.0.1:6379> Hgetall Myhash
1) "Field1"
2) "Hello"
3) "Field2"
4) "World"
#仅获取myhash键中所有字段的名字.
Redis 127.0.0.1:6379> Hkeys Myhash
1) "Field1"
2) "Field2" #p # page Title #e#
#仅获取myhash键中所有字段的值.
Redis 127.0.0.1:6379> hvals Myhash
1) "Hello"
2) "World"

Redis Tutorial (iv): Hashes data types

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.