Redis命令学习—Hash(哈希表)操作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Redis命令学习—Hash(哈希表)操作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4604字,纯文字阅读大概需要7分钟。
内容图文
![Redis命令学习—Hash(哈希表)操作](/upload/InfoBanner/zyjiaocheng/1322/9983ae66d21441998ca3fc8aa101aba7.jpg)
HDEL
# 测试数据
redis > HGETALL abbr
1 ) "a"
2 ) "apple"
3 ) "b"
4 ) "banana"
5 ) "c"
6 ) "cat"
7 ) "d"
8 ) "dog"
# 删除单个域
redis > HDEL abbr a
( integer ) 1
# 删除不存在的域
redis > HDEL abbr not - exists - field
( integer ) 0
# 删除多个域
redis > HDEL abbr b c
( integer ) 2
redis > HGETALL abbr
1 ) "d"
2 ) "dog"
HEXISTS
redis > HEXISTS phone myphone
( integer ) 0
redis > HSET phone myphone nokia - 1110
( integer ) 1
redis > HEXISTS phone myphone
( integer ) 1
HGET
# 域存在
redis > HSET site redis redis . com
( integer ) 1
redis > HGET site redis
"redis.com"
# 域不存在
redis > HGET site mysql
( nil )
HGETALL
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"
HINCRBY
# increment 为正数
redis > HEXISTS counter page_view # 对空域进行设置
( integer ) 0
redis > HINCRBY counter page_view 200
( integer ) 200
redis > HGET counter page_view
"200"
# increment 为负数
redis > HGET counter page_view
"200"
redis > HINCRBY counter page_view - 50
( integer ) 150
redis > HGET counter page_view
"150"
# 尝试对字符串值的域执行HINCRBY命令
redis > HSET myhash string hello , world # 设定一个字符串值
( integer ) 1
redis > HGET myhash string
"hello,world"
redis > HINCRBY myhash string 1 # 命令执行失败,错误。
( error ) ERR hash value is not an integer
redis > HGET myhash string # 原值不变
"hello,world"
HINCRBYFLOAT
# 值和增量都是普通小数
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"
# 对不存在的键执行 HINCRBYFLOAT
redis > EXISTS price
( integer ) 0
redis > HINCRBYFLOAT price milk 3.5
"3.5"
redis > HGETALL price
1 ) "milk"
2 ) "3.5"
# 对不存在的域进行 HINCRBYFLOAT
redis > HGETALL price
1 ) "milk"
2 ) "3.5"
redis > HINCRBYFLOAT price coffee 4.5 # 新增 coffee 域
"4.5"
redis > HGETALL price
1 ) "milk"
2 ) "3.5"
3 ) "coffee"
4 ) "4.5"
HKEYS
# 哈希表非空
redis > HMSET website google www . google . com yahoo www . yahoo . com
OK
redis > HKEYS website
1 ) "google"
2 ) "yahoo"
# 空哈希表/key不存在
redis > EXISTS fake_key
( integer ) 0
redis > HKEYS fake_key
( empty list or set )
HLEN
redis > HSET db redis redis . com
( integer ) 1
redis > HSET db mysql mysql . com
( integer ) 1
redis > HLEN db
( integer ) 2
redis > HSET db mongodb mongodb . org
( integer ) 1
redis > HLEN db
( integer ) 3
HMGET
redis > HMSET pet dog "doudou" cat "nounou" # 一次设置多个域
OK
redis > HMGET pet dog cat fake_pet # 返回值的顺序和传入参数的顺序一样
1 ) "doudou"
2 ) "nounou"
3 ) ( nil ) # 不存在的域返回nil值
HMSET
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"
HSET
redis > HSET website google "www.g.cn" # 设置一个新域
( integer ) 1
redis > HSET website google "www.google.com" # 覆盖一个旧域
( integer ) 0
HSETNX
redis > HSETNX nosql key - value - store redis
( integer ) 1
redis > HSETNX nosql key - value - store redis # 操作无效,域 key-value-store 已存在
( integer ) 0
HVALS
# 非空哈希表
redis > HMSET website google www . google . com yahoo www . yahoo . com
OK
redis > HVALS website
1 ) "www.google.com"
2 ) "www.yahoo.com"
# 空哈希表/不存在的key
redis > EXISTS not_exists
( integer ) 0
redis > HVALS not_exists
( empty list or set )
原文:http://blog.csdn.net/mergades/article/details/46467551
内容总结
以上是互联网集市为您收集整理的Redis命令学习—Hash(哈希表)操作全部内容,希望文章能够帮你解决Redis命令学习—Hash(哈希表)操作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。