1、將哈希表 key 中的字段 field 的值設爲 valuespa
命令:hsetcode
格式:hset key field valueblog
127.0.0.1:6379> hset hash_key key1 v1 (integer) 1 127.0.0.1:6379> hset hash_key key2 v2 (integer) 1
2、獲取存儲在哈希表中指定字段的值v8
命令:hgetrem
格式:hget key fieldget
127.0.0.1:6379> hget hash_key key1 "v1" 127.0.0.1:6379> hget hash_key key2 "v2"
3、刪除一個或多個哈希表字段hash
命令:hdelclass
格式:hdel key fieldfloat
127.0.0.1:6379> hget hash_key key1 "v1" 127.0.0.1:6379> hget hash_key key2 "v2" 127.0.0.1:6379> hdel hash_key key2 (integer) 1 127.0.0.1:6379> hget hash_key key2 (nil) 127.0.0.1:6379> hget hash_key key1 "v1"
4、查看哈希表 key 中,指定的字段是否存在di
命令:hexists
格式:hexists key field
127.0.0.1:6379> hget hash_key key2 (nil) 127.0.0.1:6379> hget hash_key key1 "v1" 127.0.0.1:6379> hexists hash_key key1 (integer) 1 127.0.0.1:6379> hexists hash_key key2 (integer) 0
5、獲取在哈希表中指定 key 的全部字段和值
命令:hgetall
格式:hgetall key
127.0.0.1:6379> hset hash_key key1 v1 (integer) 1 127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1" 127.0.0.1:6379> hset hash_key key2 v2 (integer) 1 127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1" 3) "key2" 4) "v2" 127.0.0.1:6379> hdel hash_key key2 (integer) 1 127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1"
6、獲取全部哈希表中的字段
命令:hkeys
格式:kheys key
127.0.0.1:6379> hset hash_key key1 v1 (integer) 1 127.0.0.1:6379> hkeys hash_key 1) "key1" 127.0.0.1:6379> hset hash_key key2 v2 (integer) 1 127.0.0.1:6379> hkeys hash_key 1) "key1" 2) "key2"
7、獲取哈希表中字段的數量
命令:hlen
格式:hlen key
127.0.0.1:6379> hset hash_key key1 v1 (integer) 1 127.0.0.1:6379> hlen hash_key (integer) 1 127.0.0.1:6379> hset hash_key key2 v2 (integer) 1 127.0.0.1:6379> hlen hash_key (integer) 2
8、同時將多個 field-value (域-值)對設置到哈希表 key 中
命令:hmset
格式:hmset key field1 value1 [field2 value2] ...
127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1" 3) "key2" 4) "v2" 127.0.0.1:6379> hkeys hash_key 1) "key1" 2) "key2" 127.0.0.1:6379> hmset hash_key key3 v3 key4 v4 key5 v5 OK 127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1" 3) "key2" 4) "v2" 5) "key3" 6) "v3" 7) "key4" 8) "v4" 9) "key5" 10) "v5" 127.0.0.1:6379> hkeys hash_key 1) "key1" 2) "key2" 3) "key3" 4) "key4" 5) "key5"
9、獲取全部給定字段的值
命令:hmget
格式:hmget key field1 [field2] ...
127.0.0.1:6379> hmget hash_key key1 key3 key5 1) "v1" 2) "v3" 3) "v5"
10、獲取哈希表中全部值
命令:hvals
格式:hvals key
127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1" 3) "key2" 4) "v2" 5) "key3" 6) "v3" 7) "key4" 8) "v4" 9) "key5" 10) "v5" 127.0.0.1:6379> hvals hash_key 1) "v1" 2) "v2" 3) "v3" 4) "v4" 5) "v5"
11、只有在字段 field 不存在時,設置哈希表字段的值
命令:hsetnx
格式:hsetnx key field value
127.0.0.1:6379> hset hash_key key1 value1 (integer) 1 127.0.0.1:6379> hget hash_key key1 "value1" 127.0.0.1:6379> hset hash_key key1 value100 (integer) 0 127.0.0.1:6379> hget hash_key key1 "value100" 127.0.0.1:6379> hsetnx hash_key key1 value10000 (integer) 0 127.0.0.1:6379> hget hash_key key1 "value100" 127.0.0.1:6379> hsetnx hash_key key10 value10000 (integer) 1 127.0.0.1:6379> hget hash_key key10 "value10000"
12、爲哈希表 key 中的指定字段的整數值加上增量 increment
命令:hincrby
格式:hincrby key field increment
127.0.0.1:6379> hset hash_key num 100 (integer) 1 127.0.0.1:6379> hget hash_key num "100" 127.0.0.1:6379> hincrby hash_key num 5 (integer) 105 127.0.0.1:6379> hget hash_key num "105" 127.0.0.1:6379> hincrby hash_key num -15 (integer) 90 127.0.0.1:6379> hget hash_key num "90"
十3、爲哈希表 key 中的指定字段的浮點數值加上增量 increment
命令:hincrbyfloat
格式:hincrbyfloat key field increment
127.0.0.1:6379> hset hash_key float 100 (integer) 1 127.0.0.1:6379> hget hash_key float "100" 127.0.0.1:6379> hincrbyfloat hash_key float 0.5 "100.5" 127.0.0.1:6379> hget hash_key float "100.5" 127.0.0.1:6379> hincrbyfloat hash_key float -1.5 "99" 127.0.0.1:6379> hget hash_key float "99"
十4、迭代哈希表中的鍵值對
命令:hscan
格式:hscan key cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> hmset hash_key k1 v1 k2 v2 k3 v3 k4 v4 k5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k10 v10 k11 v11 k12 v12 OK 127.0.0.1:6379> hgetall hash_key 1) "k1" 2) "v1" 3) "k2" 4) "v2" 5) "k3" 6) "v3" 7) "k4" 8) "v4" 9) "k5" 10) "v5" 11) "k6" 12) "v6" 13) "k7" 14) "v7" 15) "k8" 16) "v8" 17) "k9" 18) "v9" 19) "k10" 20) "v10" 21) "k11" 22) "v11" 23) "k12" 24) "v12" 127.0.0.1:6379> del hash_key (integer) 1 127.0.0.1:6379> hmset hash_key key1 v1 key2 v2 key3 v3 key4 v4 key5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k10 v10 OK 127.0.0.1:6379> hgetall hash_key 1) "key1" 2) "v1" 3) "key2" 4) "v2" 5) "key3" 6) "v3" 7) "key4" 8) "v4" 9) "key5" 10) "v5" 11) "k6" 12) "v6" 13) "k7" 14) "v7" 15) "k8" 16) "v8" 17) "k9" 18) "v9" 19) "k10" 20) "v10" 127.0.0.1:6379> hscan hash_key 0 match k* 1) "0" 2) 1) "key1" 2) "v1" 3) "key2" 4) "v2" 5) "key3" 6) "v3" 7) "key4" 8) "v4" 9) "key5" 10) "v5" 11) "k6" 12) "v6" 13) "k7" 14) "v7" 15) "k8" 16) "v8" 17) "k9" 18) "v9" 19) "k10" 20) "v10" 127.0.0.1:6379> hscan hash_key 0 match key* 1) "0" 2) 1) "key1" 2) "v1" 3) "key2" 4) "v2" 5) "key3" 6) "v3" 7) "key4" 8) "v4" 9) "key5" 10) "v5"