码迷,mamicode.com
首页 > 其他好文 > 详细

Redis命令学习—Hash(哈希表)操作

时间:2015-06-12 10:08:15      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

HDEL

    HDEL key field [field...]:删除Hash key中的一个或多个域, 不存在的域会被忽略。
    返回值:被成功删除的key的数量。
    1. # 测试数据
    2. redis> HGETALL abbr
    3. 1) "a"
    4. 2) "apple"
    5. 3) "b"
    6. 4) "banana"
    7. 5) "c"
    8. 6) "cat"
    9. 7) "d"
    10. 8) "dog"
    11. # 删除单个域
    12. redis> HDEL abbr a
    13. (integer) 1
    14. # 删除不存在的域
    15. redis> HDEL abbr not-exists-field
    16. (integer) 0
    17. # 删除多个域
    18. redis> HDEL abbr b c
    19. (integer) 2
    20. redis> HGETALL abbr
    21. 1) "d"
    22. 2) "dog"

HEXISTS 

    HEXISTS key field:查看哈希表key中,给定域field是否存在。
    返回值:如果含有给定域返回1,否则返回0
    1. redis> HEXISTS phone myphone
    2. (integer) 0
    3. redis> HSET phone myphone nokia-1110
    4. (integer) 1
    5. redis> HEXISTS phone myphone
    6. (integer) 1

HGET

    HGET key field:返回哈希表给定key中给定域的field的值。
    返回值:给定域的值。如果给定域或key不存在时,返回nil。
    1. # 域存在
    2. redis> HSET site redis redis.com
    3. (integer) 1
    4. redis> HGET site redis
    5. "redis.com"
    6. # 域不存在
    7. redis> HGET site mysql
    8. (nil)

HGETALL

        HGETALL key:返回Hash key中,所有的域和值。
        返回值:以列表形式返回Hash的域和值。若key不存在,返回空列表。
        
    1. redis> HSET people jack "Jack Sparrow"
    2. (integer) 1
    3. redis> HSET people gump "Forrest Gump"
    4. (integer) 1
    5. redis> HGETALL people
    6. 1) "jack" # 域
    7. 2) "Jack Sparrow" # 值
    8. 3) "gump"
    9. 4) "Forrest Gump"

HINCRBY

    HINCRBY key field increment:为哈希表的key中的域的值加上增量increment。
    如果key不存在,那么新的哈希表会被创建。如果field不存在,那么新建filed并初始化field值为0。
    field为字符串,那么将会抛出错误。
    返回值:执行后的field值。
    1. # increment 为正数
    2. redis> HEXISTS counter page_view # 对空域进行设置
    3. (integer) 0
    4. redis> HINCRBY counter page_view 200
    5. (integer) 200
    6. redis> HGET counter page_view
    7. "200"
    8. # increment 为负数
    9. redis> HGET counter page_view
    10. "200"
    11. redis> HINCRBY counter page_view -50
    12. (integer) 150
    13. redis> HGET counter page_view
    14. "150"
    15. # 尝试对字符串值的域执行HINCRBY命令
    16. redis> HSET myhash string hello,world # 设定一个字符串值
    17. (integer) 1
    18. redis> HGET myhash string
    19. "hello,world"
    20. redis> HINCRBY myhash string 1 # 命令执行失败,错误。
    21. (error) ERR hash value is not an integer
    22. redis> HGET myhash string # 原值不变
    23. "hello,world"

HINCRBYFLOAT

    HINCRBYFLOAT key field increment:为哈希表域的浮点数增加increment。与HINCRBY相似。
    返回值:操作后的field域的值。
    
    1. # 值和增量都是普通小数
    2. redis> HSET mykey field 10.50
    3. (integer) 1
    4. redis> HINCRBYFLOAT mykey field 0.1
    5. "10.6"
    6. # 值和增量都是指数符号
    7. redis> HSET mykey field 5.0e3
    8. (integer) 0
    9. redis> HINCRBYFLOAT mykey field 2.0e2
    10. "5200"
    11. # 对不存在的键执行 HINCRBYFLOAT
    12. redis> EXISTS price
    13. (integer) 0
    14. redis> HINCRBYFLOAT price milk 3.5
    15. "3.5"
    16. redis> HGETALL price
    17. 1) "milk"
    18. 2) "3.5"
    19. # 对不存在的域进行 HINCRBYFLOAT
    20. redis> HGETALL price
    21. 1) "milk"
    22. 2) "3.5"
    23. redis> HINCRBYFLOAT price coffee 4.5 # 新增 coffee 域
    24. "4.5"
    25. redis> HGETALL price
    26. 1) "milk"
    27. 2) "3.5"
    28. 3) "coffee"
    29. 4) "4.5"

HKEYS

    HKEYS key:返回哈希表中key的所有域。
    返回值:一个包含哈希表中所有域的表。key不存在时,返回一个空表。
    1. # 哈希表非空
    2. redis> HMSET website google www.google.com yahoo www.yahoo.com
    3. OK
    4. redis> HKEYS website
    5. 1) "google"
    6. 2) "yahoo"
    7. # 空哈希表/key不存在
    8. redis> EXISTS fake_key
    9. (integer) 0
    10. redis> HKEYS fake_key
    11. (empty list or set)

HLEN

    HLEN key:返回哈希表key中域的数量。
    返回值:哈希表中域的数量,key不存在时,返回0。
    1. redis> HSET db redis redis.com
    2. (integer) 1
    3. redis> HSET db mysql mysql.com
    4. (integer) 1
    5. redis> HLEN db
    6. (integer) 2
    7. redis> HSET db mongodb mongodb.org
    8. (integer) 1
    9. redis> HLEN db
    10. (integer) 3

HMGET

    HMGET key field[field ... ]:返回哈希表中一个或多个给定域的值。如果域不存在则返回nil。
    返回值:值的列表。表值的排序和给定参数的顺序一致。
    
    1. redis> HMSET pet dog "doudou" cat "nounou" # 一次设置多个域
    2. OK
    3. redis> HMGET pet dog cat fake_pet # 返回值的顺序和传入参数的顺序一样
    4. 1) "doudou"
    5. 2) "nounou"
    6. 3) (nil) # 不存在的域返回nil值

HMSET

    HMSET key field value [field value ... ]:同时将多个k-v对设置到哈希表key中。此命令会覆盖已存在的key。
    返回值:如果执行成功返回ok。否则返回一个错误。
    1. redis> HMSET website google www.google.com yahoo www.yahoo.com
    2. OK
    3. redis> HGET website google
    4. "www.google.com"
    5. redis> HGET website yahoo
    6. "www.yahoo.com"

HSET

    HSET key field value:设置哈希表中key对应field的value。如果存在,旧值覆盖。否则新建哈希表。
    返回值:如果哈新表新建域,并成功,返回1.如果哈希表中field存在,且旧值被新值覆盖,返回0。
    
    1. redis> HSET website google "www.g.cn" # 设置一个新域
    2. (integer) 1
    3. redis> HSET website google "www.google.com" # 覆盖一个旧域
    4. (integer) 0

HSETNX

    HSETNX key field value:当哈希表中field不存在时,设置值。
    返回值:设置成功,返回1.否则返回0。
    1. redis> HSETNX nosql key-value-store redis
    2. (integer) 1
    3. redis> HSETNX nosql key-value-store redis # 操作无效,域 key-value-store 已存在
    4. (integer) 0

HVALS

    HVALS key:返回对应key的所有域的值 。
    返回值:一个包含所有值的表。当key不存在时返回一个空表。
    1. # 非空哈希表
    2. redis> HMSET website google www.google.com yahoo www.yahoo.com
    3. OK
    4. redis> HVALS website
    5. 1) "www.google.com"
    6. 2) "www.yahoo.com"
    7. # 空哈希表/不存在的key
    8. redis> EXISTS not_exists
    9. (integer) 0
    10. redis> HVALS not_exists
    11. (empty list or set)

HSCAN
    HSCAN key cursor [MATCH pattern] [COUNT count]:与SCAN命令相同。

Redis命令学习—Hash(哈希表)操作

标签:

原文地址:http://blog.csdn.net/mergades/article/details/46467551

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!