Redis Learning (II) hash

I. Overview

We can Redis as has the type String Key Hash String Value and the map container. Therefore, this type is very suitable for storing the information value of the object. The Username, Password Age, and the like. If the Hash contains very little field, then this type of data will also only use very little disk space. Each Hash can store 4,294,967,295 pairs.

Second, the relevant command

1, the assignment

    Example: hset hash field val

    Usage: If the hash table does not exist, a new hash table is created and HSET operation.

                If the field already exists in the hash table, the old value will be overwritten.

    Returns: if the field is a hash table of a new field, and the value of the success, returned. If the hash table already exists in the domain field and the old values ​​have been overwritten with the new value, return 0.

 

    Example: hsetnx hash field val

    Usage: If the hash table does not exist, a new hash table is created and HSET operation.

               If the field already exists in the hash table, the operation is invalid.

               If the key does not exist, a new hash table is created and executed HSETNX command.

    Returns: set successfully, return 1. If the given field already exists and no operation is performed ( difference of hset hash field val ), returns 0.

 

    Example: hmset hash field val

    Usage: for simultaneously a plurality of field-value (field - value) provided to the hash table.

                This command overwrites fields in the hash table that already exists.

                If the hash table does not exist, it creates an empty hash table, and perform HMSET operation.

    Returns: If the command is successful, return OK.

 

    Example: hkeys hash

    Usage: used to get all the fields in the hash table (field).

    Returns: A hash table in all fields (field) list. When the key does not exist, it returns an empty list.

127.0.0.1:6379> hmset myhash field1 1 filed2 2 filed3 3
OK
127.0.0.1:6379> hset myhash filed4 4
(integer) 1
127.0.0.1:6379> hsetnx myhash field2 66
(integer) 1
127.0.0.1:6379> hsetnx myhash filed2 666
(integer) 0
127.0.0.1:6379> hkeys myhash
1) "name"
2) "field1"
3) "filed2"
4) "filed3"
5) "filed4"
6) "field2"
127.0.0.1:6379>

2, the value

    Example: hget hash field

    Usage: Returns the hash value of the field specified in the table.

    Returns: Returns the value of the given field. If the given field or key does not exist, returns nil.

 

    Example: hmget hash field1 ..fieldn 

    Usage: Returns the hash table, the one or more values ​​for a given field. If the specified field does not exist in the hash table, then returns a nil value.

    Returns: containing a plurality of sequence tables given request field associated values, and the order table values ​​as specified field.

 

    Example: hvals key

    Usage: Returns a hash table values ​​for all fields (field) of.

    Returns: a list (field) values ​​in the hash table contains all fields. When the key does not exist, an empty table.

    

    Example: hexists key

    Usage: Specify the field for viewing the hash table exists.

    Returns: true if the hash table containing a given field, a return. If the hash table does not contain a given field, or key does not exist, returns 0.

 

    Example: hgetall key

    Usage: used to return a hash table, all the fields and values. In return value, the immediately following each field name (field name) is the value of the field (value), so the return value is twice the length of the hash table size.

    Returns: Fields and field values ​​hash table in a list. If the key does not exist, it returns an empty list.

 

    Example: hlen hash

    Usage: To view the number of fields in the hash table.

    Returns: Returns the number of fields in the hash table, if the key does not exist, returns 0.

 

    Example: hlen hash

    Usage: To view the number of fields in the hash table.

    Returns: Returns the number of fields in the hash table, if the key does not exist, returns 0.

  

127.0.0.1:6379> hkeys myhash                    #得到的是标签值
1) "name"
2) "field1"
3) "filed2"
4) "filed3"
5) "filed4"
6) "field2"
127.0.0.1:6379> HVALS myhash                    得到标签对应的值
1) "jack"
2) "1"
3) "2"
4) "3"
5) "4"
6) "66"
127.0.0.1:6379> hget myhash name                获取哈希表对应标签的值
"jack"
127.0.0.1:6379> hmget myhash name field1        获取多个标签的值
1) "jack"
2) "1"
127.0.0.1:6379> HEXISTS myhash name
(integer) 1
127.0.0.1:6379> hgetall myhash                 得到标签值+对应的值
 1) "name"
 2) "jack"
 3) "field1"
 4) "1"
 5) "filed2"
 6) "2"
 7) "filed3"
 8) "3"
 9) "filed4"
10) "4"
11) "field2"
12) "66"
127.0.0.1:6379> hlen myhash                  统计标签个数
(integer) 6
127.0.0.1:6379>

3, operation

    Example: hincrby hash field num

    Usage: used as field values in the hash table plus the specified increment value. Increment may be negative , corresponding to subtraction operation on the specified field. If the key hash table does not exist, a new hash table is created and executed HINCRBY command. If the specified field does not exist, then before executing the command, the value field is initialized to zero. A field for the string value stored execution HINCRBY command will cause an error . The value of this operation is limited to 64-bit (bit) within the signed digital representation.

    Return: incr after the value of the field.

 

    Example: hincrbyfloat hash field num

    Usage: used as field values in the hash table plus the specified increment value. Increment may be negative , corresponding to subtraction operation on the specified field. If the key hash table does not exist, a new hash table is created and executed HINCRBY command. If the specified field does not exist, then before executing the command, the value field is initialized to zero. A field for the string value stored execution HINCRBY command will cause an error . The value of this operation is limited to 64-bit (bit) within the signed digital representation.

    Return: incr after the value of the field.

127.0.0.1:6379> hincrby myhash field2 -6
(integer) 60
127.0.0.1:6379> hincrby myhash field2 6
(integer) 66
127.0.0.1:6379> hincrbyfloat myhash field2 6.6
"72.599999999999994"
127.0.0.1:6379>

 

Published 22 original articles · won praise 9 · views 8818

Guess you like

Origin blog.csdn.net/ljm_c_bok/article/details/104841393