1、Hash概念
(1)Redis hash 是一个键值对集合。
(2)Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象。
(3)类似Java里面的Map<String,Object>
2、Hash 基本命令
(1)给<key>集合中的 <field>键赋值<value>
hset <key> <field> <value>
127.0.0.1:6379> hset customer:1001 name zhangsan
(integer) 1
127.0.0.1:6379> hset customer:1001 sex nv
(integer) 1
(2)从<key1>集合<field> 取出 value
hget <key1> <field>
127.0.0.1:6379> hget customer:1001 sex
"nv"
127.0.0.1:6379> hget customer:1001 name
"zhangsan"
(3)批量设置hash的值
hmset <key1> <field1> <value1> <field2> <value2>...
127.0.0.1:6379> hmset customer:1002 name zhangting sex nv
OK
127.0.0.1:6379> hget customer:1002 name
"zhangting"
(4)查看哈希表 key 中,给定域 field 是否存在
hexists key <field>
127.0.0.1:6379> hexists customer:1001 name
(integer) 1
127.0.0.1:6379> hexists customer:1001 id
(integer) 0
127.0.0.1:6379> hexists customer:1001 sex
(integer) 1
(5)列出该hash集合的所有field
hkeys <key>
127.0.0.1:6379> hkeys customer:1001
1) "name"
2) "sex"
127.0.0.1:6379> hkeys customer:1002
1) "name"
2) "sex"
3) "aihao"
(6)列出该hash集合的所有value
hvals <key>
127.0.0.1:6379> hvals customer:1001
1) "zhangsan"
2) "nv"
127.0.0.1:6379> hvals customer:1002
1) "zhangting"
2) "nv"
3) "lanqiu"
(7)为哈希表 key 中的域 field 的值加上增量 increment
hincrby <key> <field> <increment>
127.0.0.1:6379> hkeys customer:1002
1) "name"
2) "sex"
3) "aihao"
4) "id"
127.0.0.1:6379> hvals customer:1002
1) "zhangting"
2) "nv"
3) "lanqiu"
4) "4"
127.0.0.1:6379> hincrby customer:1002 id 3
(integer) 7
127.0.0.1:6379> hvals customer:1002
1) "zhangting"
2) "nv"
3) "lanqiu"
4) "7"
(8)将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在
hsetnx <key> <field> <value>
127.0.0.1:6379> hsetnx customer:1002 money 1000
(integer) 1
127.0.0.1:6379> hsetnx customer:1002 money 1000
(integer) 0