zoukankan      html  css  js  c++  java
  • Redis教程9-哈希(Hash)常用命令使用参考1

    1.HDEL

    HDEL key field [field ...]

    删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。

    在Redis2.4以下的版本里, HDEL 每次只能删除单个域,如果你需要在一个原子时间内删除多个域,请将命令包含在 MULTI / EXEC 块内。

    可用版本:>= 2.0.0

    时间复杂度:O(N), N 为要删除的域的数量。

    返回值:被成功移除的域的数量,不包括被忽略的域。

    127.0.0.1:6379> hmset user name zhangsan age 20 tel 12111112203 address chinese
    OK
    127.0.0.1:6379> hgetall user    // 所有field和values
    1) "name"
    2) "zhangsan"
    3) "age"
    4) "20"
    5) "tel"
    6) "12111112203"
    7) "address"
    8) "chinese"
    127.0.0.1:6379> hdel user address tel    // 删除存在的field域
    (integer) 2
    127.0.0.1:6379> hgetall user
    1) "name"
    2) "zhangsan"
    3) "age"
    4) "20"
    127.0.0.1:6379> hdel user abcd    // 删除单个不存在的field域
    (integer) 0
    127.0.0.1:6379> hdel user abcd age    // 删除多个field域, adbc不存在, age存在
    (integer) 1
    127.0.0.1:6379> hgetall user
    1) "name"
    2) "zhangsan"

    2.HEXISTS

    HEXISTS key field

    查看哈希表 key 中,给定域 field 是否存在。

    可用版本:>= 2.0.0

    时间复杂度:O(1)

    返回值:

    如果哈希表含有给定域,返回 1 。
    如果哈希表不含有给定域,或 key 不存在,返回 0 。
    127.0.0.1:6379> hgetall user
    1) "name"
    2) "zhangsan"
    127.0.0.1:6379> hexists user name    // name存在
    (integer) 1
    127.0.0.1:6379> hexists user age    // age不存在
    (integer) 0
    127.0.0.1:6379> hexists person name    // person不存在
    (integer) 0

    3.HGET

    HGET key field

    返回哈希表 key 中给定域 field 的值。

    可用版本:>= 2.0.0

    时间复杂度:O(1)

    返回值:

    给定域的值。
    当给定域不存在或是给定 key 不存在时,返回 nil 。
    127.0.0.1:6379> hgetall user
    1) "name"
    2) "zhangsan"
    127.0.0.1:6379> hget user name    // field域存在
    "zhangsan"
    127.0.0.1:6379> hget user age    // field域不存在
    (nil)
    127.0.0.1:6379> hget person name    // key不存在
    (nil)

    4.HGETALL

    HGETALL key

    返回哈希表 key 中,所有的域和值。

    在返回值里,紧跟每个域名(field name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍。

    可用版本:>= 2.0.0

    时间复杂度:O(N), N 为哈希表的大小。

    返回值:

    以列表形式返回哈希表的域和域的值。
    若 key 不存在,返回空列表。
    127.0.0.1:6379> hgetall person    // key不存在
    (empty list or set)
    127.0.0.1:6379> hgetall user
    1) "name"    // field域
    2) "zhangsan"    // 值

    5.HINCRBY

    HINCRBY key field increment

    为哈希表 key 中的域 field 的值加上增量 increment 。

    增量也可以为负数,相当于对给定域进行减法操作。

    如果 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。

    如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。

    对一个储存字符串值的域 field 执行 HINCRBY 命令将造成一个错误。

    本操作的值被限制在 64 位(bit)有符号数字表示之内。

    可用版本:>= 2.0.0

    时间复杂度:O(1)

    返回值:执行 HINCRBY 命令之后,哈希表 key 中域 field 的值。

    # 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"

    6.HINCRBYFLOAT

    HINCRBYFLOAT key field increment

    为哈希表 key 中的域 field 加上浮点数增量 increment 。

    如果哈希表中没有域 field ,那么 HINCRBYFLOAT 会先将域 field 的值设为 0 ,然后再执行加法操作。

    如果键 key 不存在,那么 HINCRBYFLOAT 会先创建一个哈希表,再创建域 field ,最后再执行加法操作。

    当以下任意一个条件发生时,返回一个错误:

    • 域 field 的值不是字符串类型(因为 redis 中的数字和浮点数都以字符串的形式保存,所以它们都属于字符串类型)
    • 域 field 当前的值或给定的增量 increment 不能解释(parse)为双精度浮点数(double precision floating point number)

    HINCRBYFLOAT 命令的详细功能和 INCRBYFLOAT 命令类似,请查看 INCRBYFLOAT 命令获取更多相关信息。

    可用版本:>= 2.6.0

    时间复杂度:O(1)

    返回值:执行加法操作之后 field 域的值。

    # 值和增量都是普通小数
    
    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"
  • 相关阅读:
    C语言 sprintf 函数 C语言零基础入门教程
    C语言 printf 函数 C语言零基础入门教程
    C语言 文件读写 fgets 函数 C语言零基础入门教程
    C语言 文件读写 fputs 函数 C语言零基础入门教程
    C语言 fprintf 函数 C语言零基础入门教程
    C语言 文件读写 fgetc 函数 C语言零基础入门教程
    C语言 文件读写 fputc 函数 C语言零基础入门教程
    C语言 strlen 函数 C语言零基础入门教程
    Brad Abrams关于Naming Conventions的演讲中涉及到的生词集解
    适配器模式
  • 原文地址:https://www.cnblogs.com/no-celery/p/13705780.html
Copyright © 2011-2022 走看看