zoukankan      html  css  js  c++  java
  • Redis 的数据类型

    #Hash更容易存储对象,比如在设置用户姓名,年龄,邮箱等属性时,用string需要分别来进行设置存储,通过Hash就可以把属性放到对象中,然后再存储对象,因此相对于string类型,Hash类型存储对象可以占用更少的字节#

      在配置文件中可以通过配置 

      hash-max-ziplist-entries 512  #存储值得最大字节512字节#

      hash-max-ziplist-value 64  #字段数目,默认64#

    HSET:将 Hash 表 key 中域 field 设置成指定的 value
    HGET:返回 Hash 表 key 中给定 field 的值

      语法:HSET key field value

        HGET key field

        HSET userInfo1 username 'tom' 

        HSET userInfo1 password '123456' 

        HSET userInfo1 email 'tom@qq.com'

        HGET userInfo1 username

        HSET userInfo1 username 'jerry'   #这时 userInfo1 的 username 已经重赋值为 'jerry' #

        HGET userInfo2 username  #如果 Hash 表 key 中 field 不存在,则返回 nil 如果 key 中 field 不存在,返回的是 nil #

    127.0.0.1:6379> HSET userInfo1 username 'tom'
    (integer) 1
    
    127.0.0.1:6379> HSET userInfo1 password '123456'
    (integer) 1
    
    127.0.0.1:6379> HSET userInfo1 email 'tom@qq.com'
    (integer) 1
    
    127.0.0.1:6379> HGET userInfo1 username
    "tom"
    
    127.0.0.1:6379> HSET userInfo1 username 'jerry'
    (integer) 0
    
    127.0.0.1:6379> HGET userInfo1 username
    "jerry"

    HSETNX:将 Hash 表中的 field 设置成指定的值,只要 field 不存在的时候才可以成功;如果 field 存在,操作无效

      语法:HSETNX key field value

        HSETNX testHash1 test 'tom' #返回 0 #

    HMSET:同时将多个 field-value 设置到 Hash 表 key 中
    HMGET:一次获得 Hash 表 key 中多个 field 的值

      语法:HMSET key field value field value ...
        HMGET key field field

        HMSET userInfo2 username 'tom' password '123456' email 'tom@qq.com' 

        HGET userInfo2 username

        HMSET userInfo2 username 'jerry' addr 'nanjing'  #存在覆盖,不存在添加,返回 ok #

        HMGET userInfo2 username password email addr phone  #这里phone这个field并不存在,返回nil#

    127.0.0.1:6379> HMSET userInfo2 username 'tom' password '123456' email 'tom@qq.com'
    OK
    
    127.0.0.1:6379> HGET userInfo2 username
    "tom"
    
    127.0.0.1:6379> HMSET userInfo2 username 'jerry' addr 'nanjing'
    OK
    
    127.0.0.1:6379> HMGET userInfo2 username password email addr phone
    1) "jerry"
    2) "123456"
    3) "tom@qq.com"
    4) "nanjing"
    5) (nil)

    HGETALL:返回 Hash 表 key 中所有的 field 和 value

      语法:HGETALL key

        HGETALL userInfo2   #如果key不存在,则返回(empty list or set)#

    127.0.0.1:6379> HGETALL userInfo2
    1) "username"
    2) "jerry"
    3) "password"
    4) "123456"
    5) "email"
    6) "tom@qq.com"
    7) "addr"
    8) "nanjing"

    HKEYS:返回 Hash 中 key 的所有的 field

      语法:HKEYS key

        HKEYS userInfo2

    127.0.0.1:6379> HKEYS userInfo2
    1) "username"
    2) "password"
    3) "email"
    4) "addr"

    HVALS:返回 Hash 中 key 中 field 所有的对应的值

      语法:HVALS key

        HVALS userInfo2

    127.0.0.1:6379> HVALS userInfo2
    1) "jerry"
    2) "123456"
    3) "tom@qq.com"
    4) "nanjing"

    HEXISTS:检测 Hash 中 key 的 field 是否存在

      语法:HEXISTS key field

        HEXISTS userInfo2 username   #存在,返回 1 #

        HEXISTS userInfo2 sex   #不存在,返回 0 #

    127.0.0.1:6379> HEXISTS userInfo2 username
    (integer) 1
    
    127.0.0.1:6379> HEXISTS userInfo2 sex
    (integer) 0

    HLEN:返回 Hash 表 key 中 field 的数量

      语法:HLEN key

        HLEN userInfo2   #如果key不存在,返回 0 #

    127.0.0.1:6379> HLEN userInfo2
    (integer) 4

    HINCRBY:给 Hash 中 key 的 field 做增量操作

      语法:HINCRBY key field increment

        HSET userInfo3 age 12

        HINCRBY userInfo3 age 10   #返回22#

        HSET userInfo3 username 'tom'

        HINCRBY userInfo3 username 10   #对字符串进行设置就会报错#

    127.0.0.1:6379> HSET userInfo3 age 12
    (integer) 1
    
    127.0.0.1:6379> HINCRBY userInfo3 age 10
    (integer) 22
    
    127.0.0.1:6379> HSET userInfo3 username 'tom'
    (integer) 1
    
    127.0.0.1:6379> HINCRBY userInfo3 username 10
    (error) ERR hash value is not an integer

    HINCRBYFLOAT:给 Hash 中 key 的 field 做增量 float 操作

      语法:HINCRBYFLOAT key field increment

      HSET userInfo3 salary '12345'

      HINCRBYFLOAT userInfo3 salary 0.888  #如果设置的数字为 int 型,而添加的数为 float 型,则返回值会自动补全 12 位小数#

    127.0.0.1:6379> HSET userInfo3 salary '12345'
    (integer) 1
    
    127.0.0.1:6379> HGET userInfo3 salary
    "12345"
    
    127.0.0.1:6379> HINCRBYFLOAT userInfo3 salary 0.888
    "12345.888000000001"

    HDEL:删除 Hash 中 key 的指定域,可以删除一个也可以删除多个

      语法:HDEL key field field

        HGETALL userInfo1

        HDEL userInfo1 username password email  #返回删除的条数,如果没有删除返回 0 #

    127.0.0.1:6379> HDEL userInfo1 username password email
    (integer) 3
  • 相关阅读:
    MySQL 的连接时长控制--interactive_timeout和wait_timeout
    查看MySQL 连接信息--连接空闲时间及正在执行的SQL
    mysql timestamp为0值时,python读取后的对象为None
    MySQL基础普及《MySQL管理之道:性能调优、高可用与监控》
    读《大秦帝国》第三部
    golang mysql 如何设置最大连接数和最大空闲连接数
    如何查看MySQL connection id连接id
    JAVA配置环境变量
    PB常见功能实现代码
    PB中数据窗口自动换行
  • 原文地址:https://www.cnblogs.com/shuo-128/p/7073528.html
Copyright © 2011-2022 走看看