redis命令(2)--散列类型

以下命令中 key指散列表名,field 指散列表的属性(key),value 指属性(key)对应的值

1.为散列表设置单个属性值、获取单个属性值

  hset key field value (如果属性存在,则属性值被新value覆盖)

  hget key field 

   hset 和hget 每次设置(或获取)散列表的一个属性

localhost:6379> hset car brand focus
(integer) 1
localhost:6379> hset car color black
(integer) 1
localhost:6379> hset car price 12000
(integer) 1
localhost:6379> hget car brand
"focus"
localhost:6379> hget car color
"black"
localhost:6379> hget car price
"12000"

 2.为散列表设置(或获取)多个属性值

  hmset key field1 value1 field2 value2 field3 value3..

  hmget key field1 field2 field3...

localhost:6379> hmset car brand focus color black price 12000
OK
localhost:6379> hmget car brand color price
1) "focus"
2) "black"
3) "12000"

 3.获取散列表的所有属性及值

 hgetall key 

扫描二维码关注公众号,回复: 576200 查看本文章
localhost:6379> hmset car brand focus color black price 12000
OK
localhost:6379> hgetall car
1) "brand"
2) "focus"
3) "color"
4) "black"
5) "price"
6) "12000"

 4.判断散列表是否存在指定属性名

  hexists key field 

  

localhost:6379>  hmset car brand focus color black price 12000
OK
localhost:6379> hexists car brand
(integer) 1
localhost:6379> hexists car vender
(integer) 0

 5.如果属性不存在,则赋值

   hsetnx key field value (nx = not exist)

localhost:6379>  hmset car brand focus color black price 12000
OK
localhost:6379> hsetnx car brand bmw
(integer) 0
localhost:6379> hsetnx car vender Ford
(integer) 1

 6.散列表数值属性增加数值

   hincrby key field num

localhost:6379>  hmset car brand focus color black price 12000
OK
localhost:6379> hincrby car price 10000
(integer) 22000

 7.删 除属性

   hdel key field 

  

localhost:6379> hmset car brand focus color black price 12000
OK
localhost:6379> hdel car brand
(integer) 1
localhost:6379> hdel car name
(integer) 0

 8.删除散列表

  del key

  

localhost:6379>  hmset car brand focus color black price 12000
OK
localhost:6379> del car
(integer) 1
localhost:6379> exists car
(integer) 0

 9.获取散列表的所有属性名

 hkeys key

localhost:6379> hmset car brand focus color black price 12000
OK
localhost:6379>
localhost:6379> hkeys car
1) "brand"
2) "color"
3) "price"

 10.获取散列表的所有值

hvals key

localhost:6379>  hmset car brand focus color black price 12000
OK
localhost:6379> hvals car
1) "focus"
2) "black"
3) "12000"

 11.获取散列表属性个数

localhost:6379>  hmset car brand focus color black price 12000
OK
localhost:6379> hlen car
(integer) 3

猜你喜欢

转载自java12345678.iteye.com/blog/2162418
今日推荐