Redis 的數據類型 - Hash 對象類型

#Hash更容易存儲對象,好比在設置用戶姓名,年齡,郵箱等屬性時,用string須要分別來進行設置存儲,經過Hash就能夠把屬性放到對象中,而後再存儲對象,所以相對於string類型,Hash類型存儲對象能夠佔用更少的字節#spa

 

  在配置文件中能夠經過配置 code

  hash-max-ziplist-entries 512  #存儲值得最大字節512字節#對象

  hash-max-ziplist-value 64  #字段數目,默認64#blog

 

HSET:將 Hash 表 key 中域 field 設置成指定的 value
HGET:返回 Hash 表 key 中給定 field 的值ip

  語法:HSET key field valuerem

    HGET key field字符串

    HSET userInfo1 username 'tom' string

    HSET userInfo1 password '123456' hash

    HSET userInfo1 email 'tom@qq.com'class

    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
相關文章
相關標籤/搜索