Redis基础 - 2.4五大数据类型 哈希表

1. hset
# 设置/更新一个域  HSET hash field value
redis> HSET website google "www.g.cn"
(integer) 1

redis> HGET website google
"www.g.cn"
2. hsetnx
# 设置不存在的域  HSETNX hash field value
redis> HSETNX database key-value-store Redis
(integer) 1

3. hget
# 获取域的值 HGET hash field
redis> HSET homepage redis redis.com
(integer) 1

redis> HGET homepage redis
"redis.com"
4. hmset
# HMSET key field value [field value …]
redis> HMSET website google www.google.com yahoo www.yahoo.com
OK

redis> HGET website google
"www.google.com"

redis> HGET website yahoo
"www.yahoo.com"
5. hmget
# HMGET key field [field …]
redis> HMSET pet dog "doudou" cat "nounou"    # 一次设置多个域
OK

redis> HMGET pet dog cat fake_pet             # 返回值的顺序和传入参数的顺序一样
1) "doudou"
2) "nounou"
3) (nil)   
6. hgetall
# 返回哈希表 key 中,所有的域和值  HGETALL key
redis> HSET people jack "Jack Sparrow"
(integer) 1

redis> HSET people gump "Forrest Gump"
(integer) 1

redis> HGETALL people
1) "jack"          # 域
2) "Jack Sparrow"  # 值
3) "gump"
4) "Forrest Gump"
7. hkeys
# 返回哈希表 key 中的所有域。 HKEYS key

redis> HMSET website google www.google.com yahoo www.yahoo.com
OK

redis> HKEYS website
1) "google"
2) "yahoo"
8. hvals
# 返回哈希表 key 中所有域的值 HVALS key
redis> HMSET website google www.google.com yahoo www.yahoo.com
OK

redis> HVALS website
1) "www.google.com"
2) "www.yahoo.com"
9. hexists
# 域存在返回1,不存在返回0  HEXISTS hash field
redis> HEXISTS phone myphone
(integer) 0
10. hdel
# 删除哈希表中一个或多个指定域  HDEL key field [field …]
# 删除单个域

redis> HDEL abbr a
(integer) 1


# 删除不存在的域

redis> HDEL abbr not-exists-field
(integer) 0


# 删除多个域

redis> HDEL abbr b c
(integer) 2
11. hlen
# 哈希表中域的数量 HLEN key
127.0.0.1:6379> HMSET redis a 11 b 12 c 13 d 14
OK
127.0.0.1:6379> HLEN redis
(integer) 4
12. hstrlen
# 返回哈希表 key 中,与给定域 field 相关联的值的字符串长度 HSTRLEN key field
redis> HMSET myhash f1 "HelloWorld" f2 "99" f3 "-256"
OK

redis> HSTRLEN myhash f1
(integer) 10
13. hincrby
# HINCRBY key field increment
redis> HEXISTS counter page_view    # 对空域进行设置
(integer) 0

redis> HINCRBY counter page_view -200
(integer) -200
14.hincrbyfloat
# HINCRBYFLOAT key field increment
redis> HSET mykey field 10.50
(integer) 1
redis> HINCRBYFLOAT mykey field 0.1
"10.6"

# 值和增量都是指数符号
redis> HSET mykey field 5.0e3
(integer) 0
redis> HINCRBYFLOAT mykey field 2.0e2
"5200"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值