• Redis中hash表中的field的value自增可以用hincrby


    Redis HINCRBY命令用于增加存储在字段中存储由增量键哈希的数量。如果键不存在,新的key被哈希创建。如果字段不存在,值被设置为0之前进行操作。

    回复整数,字段的增值操作后的值。

    redis HINCRBY命令的基本语法如下所示:

    redis 127.0.0.1:6379> HINCRBY KEY_NAME FIELD_NAME INCR_BY_NUMBER 
    
    redis 127.0.0.1:6379> HSET myhash field1 20
    (integer) 1
    redis 127.0.0.1:6379> HINCRBY myhash field 1
    (integer) 21
    redis 127.0.0.1:6379> HINCRBY myhash field -1
    (integer) 20


    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"
  • 相关阅读:
    Javascript 组成:ECMAscript、Dom、Bom
    js字符串的操作
    函数(方法)基本内容
    for循环运用,三角形
    Java Script 数组
    控制语句—for循环、while循环
    JavaScript基础内容
    网站创建中遇到的问题
    数据库无法启动ORA-01034: ORACLE not available
    关于数据库管理员安全和权限
  • 原文地址:https://www.cnblogs.com/qiange/p/5110207.html
Copyright © 2020-2023  润新知