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 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值