一、將哈希表 key 中的欄位 field 的值設為 value 命令:hset 格式:hset key field value 127.0.0.1:6379> hset hash_key key1 v1 (integer) 1 127.0.0.1:6379> hset hash_key key2 ...
一、將哈希表 key 中的欄位 field 的值設為 value
命令:hset
格式:hset key field value
127.0.0.1:6379> hset hash_key key1 v1 (integer) 1 127.0.0.1:6379> hset hash_key key2 v2 (integer) 1
二、獲取存儲在哈希表中指定欄位的值
命令:hget
格式:hget key field
127.0.0.1:6379> hget hash_key key1 "v1" 127.0.0.1:6379> hget hash_key key2 "v2"
三、刪除一個或多個哈希表欄位
命令:hdel
格式:hdel key field
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"
四、查看哈希表 key 中,指定的欄位是否存在
命令: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
五、獲取在哈希表中指定 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"
六、獲取所有哈希表中的欄位
命令: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"
七、獲取哈希表中欄位的數量
命令: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
八、同時將多個 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"
九、獲取所有給定欄位的值
命令:hmget
格式:hmget key field1 [field2] ...
127.0.0.1:6379> hmget hash_key key1 key3 key5 1) "v1" 2) "v3" 3) "v5"
十、獲取哈希表中所有值
命令: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"
十一、只有在欄位 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"
十二、為哈希表 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"
十三、為哈希表 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"
十四、迭代哈希表中的鍵值對
命令: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"