Redis tutorial (4): Hashes data type

黄舟
Release: 2016-12-28 14:34:34
Original
1111 people have browsed it

1. Overview:

We can think of the Hashes type in Redis as a map container with String Key and String Value. So this type is very suitable for storing information about value objects. Such as Username, Password and Age, etc. If the Hash contains few fields, this type of data will also occupy very little disk space. Each Hash can store 4294967295 key-value pairs.

2. Related command list:

Command prototype Time complexity Command description Return value
HSET key field value O(1) Set the Field/Value pair for the specified Key. If the Key does not exist, this command will create a new Key with the Field/Value in the parameter. Yes, if the Field in the parameter already exists in the Key, the original value will be overwritten with the new value. 1 means that the new Field has been set with a new value, 0 means that the Field already exists, and will be overwritten with the new value. Original value
HGET key field O(1) Returns the associated value of the specified Field in the specified Key. Return the associated value of the Field in the parameter. If the Key or Field in the parameter does not exist, return nil.
HEXISTSkey field O(1) Determine whether the specified Field in the specified Key exists. 1 means it exists, 0 means the Field or Key in the parameter does not exist.
HLEN key O(1) Get the number of Fields contained in the Key. Returns the number of Fields contained in Key. If Key does not exist, returns 0.
HDEL key field [field ...] O(N) N in time complexity represents the field to be deleted in the parameter quantity. Delete multiple fields specified in the parameters from the Hashes Value of the specified Key. If the fields do not exist, they will be ignored. If the Key does not exist, it is treated as empty Hashes and returns 0. The number of Fields actually deleted.
HSETNXkey field value O(1) Only when the Key or Field in the parameter does not exist, set the specified Key Define the Field/Value pair, otherwise the command will not perform any operation. 1 means that the new Field has been set with a new value, 0 means that the Key or Field already exists, and this command does not perform any operation.
HINCRBYkey field increment O(1) Increase the value of the Value associated with the specified Field in the specified Key. If the Key or Field does not exist, this command will create a new Key or Field, initialize its associated Value to 0, and then specify the operation of increasing the number. The numbers supported by this command are 64-bit signed integers, that is, the increment can be negative. Return the value after the operation
HGETALLkey O(N) The N in the time complexity indicates that the Key contains Field quantity. Get all Field/Value contained in this key. The return format is a Field, a Value, and so on. List of Field/Value
HKEYSkey O(N) The N in the time complexity indicates that the Key contains Field quantity. Returns all Fields names for the specified Key. List of Fields.
HVALSkey O(N) The N in the time complexity represents the number of Fields contained in the Key. Returns all Values ​​names of the specified Key. List of Values.
MGETkey field [field ...] O(N) The N in the time complexity represents the number of fields requested. Gets a set of Values ​​associated with the Fields specified in the parameter. If the requested Field does not exist, its value returns nil. If the Key does not exist, the command treats it as an empty hash and therefore returns a set of nil. Returns a set of Values ​​associated with the requested Fields. The return order is equal to the request order of the Fields.
HMSET key field value [field value ...] O(N) N in time complexity represents the Field being set quantity. Set the Field/Value pairs given in the parameters pair by pair. If one of the Fields already exists, the original value will be overwritten with the new value. If the Key does not exist, create a new Key and set the Field/Value in the parameters.

3. Command examples:

1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX:

 #在Shell命令行启动Redis客户端程序
    /> redis-cli
    #给键值为myhash的键设置字段为field1,值为stephen。
    redis 127.0.0.1:6379> hset myhash field1 "stephen"
    (integer) 1
    #获取键值为myhash,字段为field1的值。
    redis 127.0.0.1:6379> hget myhash field1
    "stephen"
    #myhash键中不存在field2字段,因此返回nil。
    redis 127.0.0.1:6379> hget myhash field2
    (nil)
    #给myhash关联的Hashes值添加一个新的字段field2,其值为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的字段,由于存在,返回值为1。
    redis 127.0.0.1:6379> hexists myhash field1
    (integer) 1
    #删除myhash键中字段名为field1的字段,删除成功返回1。
    redis 127.0.0.1:6379> hdel myhash field1
    (integer) 1
    #再次删除myhash键中字段名为field1的字段,由于上一条命令已经将其删除,因为没有删除,返回0。
    redis 127.0.0.1:6379> hdel myhash field1
    (integer) 0
    #判断myhash键中是否存在field1字段,由于上一条命令已经将其删除,因为返回0。
    redis 127.0.0.1:6379> hexists myhash field1
    (integer) 0
    #通过hsetnx命令给myhash添加新字段field1,其值为stephen,因为该字段已经被删除,所以该命令添加成功并返回1。
    redis 127.0.0.1:6379> hsetnx myhash field1 stephen
    (integer) 1
    #由于myhash的field1字段已经通过上一条命令添加成功,因为本条命令不做任何操作后返回0。
    redis 127.0.0.1:6379> hsetnx myhash field1 stephen
    (integer) 0
Copy after login

2. HINCRBY:

 #删除该键,便于后面示例的测试。
    redis 127.0.0.1:6379> del myhash
    (integer) 1
    #准备测试数据,该myhash的field字段设定值1。
    redis 127.0.0.1:6379> hset myhash field 5
    (integer) 1
    #给myhash的field字段的值加1,返回加后的结果。
    redis 127.0.0.1:6379> hincrby myhash field 1
    (integer) 6
    #给myhash的field字段的值加-1,返回加后的结果。
    redis 127.0.0.1:6379> hincrby myhash field -1
    (integer) 5
    #给myhash的field字段的值加-10,返回加后的结果。
    redis 127.0.0.1:6379> hincrby myhash field -10
    (integer) -5
Copy after login

3. HGETALL/HKEYS/HVALS/HMGET/HMSET:

   #删除该键,便于后面示例测试。
    redis 127.0.0.1:6379> del myhash
    (integer) 1
    #为该键myhash,一次性设置多个字段,分别是field1 = "hello", field2 = "world"。
    redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
    OK
    #获取myhash键的多个字段,其中field3并不存在,因为在返回结果中与该字段对应的值为nil。
    redis 127.0.0.1:6379> hmget myhash field1 field2 field3
    1) "hello"
    2) "world"
    3) (nil)
    #返回myhash键的所有字段及其值,从结果中可以看出,他们是逐对列出的。
    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"
    #仅获取myhash键中所有字段的值。
    redis 127.0.0.1:6379> hvals myhash
    1) "hello"
    2) "world"
Copy after login

The above is the Redis tutorial (4): Hashes data type For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!