hash类型
设置值hset 获取值hget 127.0.0.1:6379> hset phone name apple (integer) 1 127.0.0.1:6379> hget phone name "apple"
同时设置多个字段和值(键相同)hset,同时获取多个字段的值(键相同)hget 127.0.0.1:6379> hmset phone price 12000 from America OK127.0.0.1:6379> hmget phone name price from 1) "apple" 2) "12000" 3) "America"
查看键中所有字段 127.0.0.1:6379> hkeys phone 1) "name" 2) "price" 3) "from"
查看键中所有值127.0.0.1:6379> hvals phone 1) "apple" 2) "12000" 3) "America"
查看键中所有的字段和值 127.0.0.1:6379> hgetall phone 1) "name" 2) "apple" 3) "price" 4) "12000" 5) "from" 6) "America"
查看键中字段的个数 127.0.0.1:6379> hlen phone (integer) 3
查看键中是否存在某个字段 127.0.0.1:6379> hexists phone price (integer) 1
键中某个字段增加一定值,只能是数字类型 127.0.0.1:6379> hincrby phone price 1000 (integer) 13000
127.0.0.1:6379> hget phone price "13000"
127.0.0.1:6379> hincrby phone name 100
(error) ERR hash value is not an integer
如果字段不存在则赋值,如果存在则不赋值
127.0.0.1:6379> hsetnx phone price 11000 (integer) 0 127.0.0.1:6379> hget phone price "13000" 127.0.0.1:6379> hsetnx phone generation 12 (integer) 1 127.0.0.1:6379> hget phone generation "12"
删除键中某个字段 127.0.0.1:6379> hdel phone generation (integer) 1 127.0.0.1:6379> hkeys phone 1) "name" 2) "price" 3) "from"
删除键 127.0.0.1:6379> del phone (integer) 1 127.0.0.1:6379> exists phone (integer) 0
总结:
增/改:hset hmset hsetnx hincrby
删:hdel
查:hget hmget hkeys hvals hgetall hlen hexists
原文:https://www.cnblogs.com/scorates/p/14096206.html