Redis資料結構之雜湊hash

2022-02-07 19:28:44 字數 2498 閱讀 2463

雜湊型別(hash)用於儲存鍵值對結構的資料,值只能是字串

hset:新增資料,返回1或0

hset key field value
127.0.0.1:6379> hset user:1 name redis

(integer) 1

hmset:批量新增資料

hmset key field1 value1 field2 value2 ..
hget:獲取field對應的值,如果field存在,則返回value,如果field不存在,返回nil

hget key field
127.0.0.1:6379> hget user:1 name

""redis

hlen:獲取field的個數

hlen key
hmget:批量獲取field

hmget key field1 field2 ..
hexists:查詢某個field是否存在,返回1或0

hexists key field
hkeys:獲取所有的field

hkeys key
hvals:獲取所有的value

hvals key
hgetall:獲取所有的filed-value

如果元素個數較多,可能會造成redis阻塞,如果只需要獲取部分元素,可以使用hmget

hgetall key
hdel:刪除元素,返回1或0

hdel key field1 field2..
127.0.0.1:6379> hdel user:1 name

(integer) 1

127.0.0.1:6379> hdel user:1 name

(integer) 0

hincrby:自增

hincrby key field
hstrlen:查詢value的字串長度

hstrlen key fild
字串型別內部有3種編碼

可以使用object encoding key檢視當前編碼

field個數小於512個,且沒有大的value時,內部編碼ziplist

127.0.0.1:6379> hmset hashkey f1 v1 f2 v2

ok127.0.0.1:6379> object encoding hashkey

"ziplist"

127.0.0.1:6379> hset hashkey f3 "one string is bigger than 64 byte....."

ok127.0.0.1:6379> object encoding hashkey

"hashtable"

購物車這種資料如果存入資料庫,會頻繁讀寫,造成資料庫壓力大,而redis是基於記憶體操作的,頻繁讀寫不會造成資料庫壓力,並且可以持久化到磁碟,任何時間地點都可以訪問

redis資料結構之雜湊

請看官方文件 redis中文官方文件 雜湊表redis的hash實際是內部儲存的value為乙個hashmap,並提供了直接訪問這個map成員的介面,也就是說,key仍然是使用者id,value是乙個map,這個map的key是成員的屬性名,value是屬性值,這樣對資料的修改和訪問都可以直接通過其...

Python操作redis系列 雜湊 hash

hash儲存資料格式 name redis conn get redis connection redis資料庫名 1.hset 命令用於為雜湊表中的字段賦值 如果雜湊表不存在,乙個新的雜湊表被建立並進行 hset 操作。如果字段已經存在於雜湊表中,舊值將被覆蓋。redis conn.hset na...

資料結構之雜湊

題目1 實現乙個簡單的雜湊結構 使用youlookdeliciousc的做法,即用拉鍊表來實現。技巧是vector來做為容器 struct node int len 1000 class myhashmap void put int key,int value pre cur cur cur nex...