那些你不得不知的Redis基礎(chǔ)類型常用操作、命令
概念:Redis是用C語言開發(fā)的一個開源的高性能鍵值對數(shù)據(jù)庫。
特征:
- 數(shù)據(jù)間沒有必然的聯(lián)系
- 內(nèi)部采用單線程機制進行工作
- 高性能
- 多數(shù)據(jù)類型支持字符串類型 String列表類型 List散列類型 Map集合類型 Set有序集合類型 SortedSet
- 持久化支持
應用場景
- 為熱點數(shù)據(jù)加速查詢;如:熱點商品、熱點新聞、熱點資訊等高訪問量信息。
- 任務隊列;如:秒殺、搶購、購票等。
- 即時信息查詢;如:排行榜等。
- 時效性信息控制;如:驗證碼、投票控制等。
- 分布式數(shù)據(jù)共享;如:分布式架構(gòu)中的session等。
- 消息隊列
- 分布式鎖
基礎(chǔ)數(shù)據(jù)類型之: String
單個string類型的存儲空間為512MB
string基本操作
- 添加或修改數(shù)據(jù)
- set key value
- 獲取數(shù)據(jù)
- get key
- 刪除數(shù)據(jù)
- del key
- 設置過期時間(默認單位為秒)
- expire key second
- 追加信息到初始value后邊
- append key value
- 添加/修改多個數(shù)據(jù)
- mset key1 value1 key2 value2...
- 獲取多個數(shù)據(jù)
- mget key1 key2 ...
- 獲取字符串長度
- strlen key
- 設置key的生命周期 控制生命周期
- setex key seconds value (秒)
- psetex key millisexxonds value (毫秒)
- key的設置約定與數(shù)據(jù)庫中的表-主鍵-字段一一對應表名主鍵名主鍵值字段名eg1orderid443523454nameeg2equireid435432543typeeg3newsid45435454title
基礎(chǔ)數(shù)據(jù)類型之:Hash
- 存儲需求: 對一系列存儲的數(shù)據(jù)進行編排,方便管理,典型應用存儲對象信息
- 存儲結(jié)構(gòu):一個存儲空間存儲多個鍵值對數(shù)據(jù)
- hash類型:底層使用哈希表結(jié)構(gòu)實現(xiàn)數(shù)據(jù)存儲

hash存儲結(jié)構(gòu)優(yōu)化
- 如果field數(shù)量較少,存儲結(jié)構(gòu)優(yōu)化為類數(shù)組結(jié)構(gòu)
- 如果field數(shù)量較多,存儲結(jié)構(gòu)使用HashMap結(jié)構(gòu)
hash類型基本操作
- 添加/修改數(shù)據(jù)
- hset key field value
- 獲取數(shù)據(jù)
- hget key field hgetall key
- 刪除數(shù)據(jù)
- hdel key field [field2 ...]
- 添加或刪除多個數(shù)據(jù)
- hmset key field1 value1 field2 value2 ...
- 獲取多個數(shù)據(jù)
- hmget key field1 field2 ...
- 獲取hash表中的字段的數(shù)量
- hlen key
- 獲取hash表中是否存在指定的字段
- hexists key field
- 獲取哈希表中所用的字段名或字段值
- hkeys keyhvalues key
- 設置指定字段的數(shù)值數(shù)據(jù)增加指定范圍的值
- hincrby key field increment
- hincrbyfloat key field increment
- 如果key值下的field存在則不做操作 不存在則添加進去
- hsetnx key field value
hash類型數(shù)據(jù)操作的注意事項
- hash類型下的value只能存儲字符串,不允許存儲其他數(shù)據(jù)類型,不存在嵌套現(xiàn)象,如果數(shù)據(jù)未獲取到,對應的值為(nil)
- 每個hash存儲的鍵值對上限為$$2^{32}-1$$個鍵值對
- hash類型十分貼近對象的存儲形式,并且可以靈活刪除對象屬性。但hash設計初衷并不是為了存儲大量對象而設計的,切記不可濫用,更不可將hash作為對象列表使用
- hgetall操作可以獲取全部屬性,如果內(nèi)部field過多,遍歷整體數(shù)據(jù)效率會很低,有可能會成為數(shù)據(jù)訪問瓶頸
應用場景
- 電商網(wǎng)站購物車設計實現(xiàn)
基礎(chǔ)數(shù)據(jù)類型之:list
- 數(shù)據(jù)存儲需求:存儲多個數(shù)據(jù),并對數(shù)據(jù)進入存儲Jon關(guān)鍵的順序進行區(qū)分
- 需要的存儲結(jié)構(gòu):一個存儲空間存儲多個數(shù)據(jù),并且數(shù)據(jù)可以體現(xiàn)進入順序
- list類型:保存多個數(shù)據(jù),底層使用雙向鏈表存儲結(jié)構(gòu)實現(xiàn)

list類型是雙向鏈表存儲的
list基本操作
- 添加/修改數(shù)據(jù)
- lpush key value1 value2 [value3] ... //從list鏈表左側(cè)添加
- rpush key value1 value2 [value3] ... //從list右側(cè)添加
- 獲取數(shù)據(jù)
- lrange key start stop //指定鏈表起始結(jié)束位置中的value
- //在獲取未知長的的list類型的時候,想查看所有的value可以使用 -1表示倒數(shù)第一個 lrange key start -1
- lindex key index //獲取鏈表中指定位置的值
- llen key //獲取鏈表的長度
- 獲取并移除數(shù)據(jù)
- lpop key
- rpop key
- 規(guī)定時間內(nèi)獲取并移除數(shù)據(jù)(阻塞式數(shù)據(jù)獲取)
- blpop key1 [key2] timeout //指定時間內(nèi)取出并移除key值對應的value,若timeout超時仍未取出則返回空值(nil) 若本來沒有 其他客戶端在等待的時候添加了這個key的value則做操作 任務隊列
- brpop key1 [key2] timeout
- 移除指定數(shù)據(jù)
- lrem key count value
list類型數(shù)據(jù)操作注意事項
- list中保存的數(shù)據(jù)都是string類型的,數(shù)據(jù)總?cè)萘坑邢拮疃?2^32-1個元素
- list具有索引概念,但操作數(shù)據(jù)時通常以隊列的形式進行入隊出隊操作,或以棧的形式進行入棧出棧操作
- 獲取全部操作結(jié)束數(shù)據(jù)的索引設置為-1
- list可以對數(shù)據(jù)進行分頁操作,通常第一頁的信息來自list,第二頁及更多的數(shù)據(jù)信息通過數(shù)據(jù)庫進行查詢加載
基礎(chǔ)數(shù)據(jù)類型之:set
- 存儲需求:存儲大量的數(shù)據(jù),在查詢方面提供更高的效率
- 存儲結(jié)構(gòu):能夠保存大量的數(shù)據(jù),高效的內(nèi)部存儲機制,便于查詢
- set類型:與hash存儲結(jié)構(gòu)完全相同,僅存儲鍵,不存儲值(nil),并且值不允許為空

set存儲結(jié)構(gòu)是string類型的無序集合,內(nèi)部存儲時hash存儲結(jié)構(gòu),因此添加、查找、刪除的復雜度都是O(1)
set基本操作
- 添加不重復的數(shù)據(jù)
- sadd key value
- 獲取存儲的所有數(shù)據(jù)
- smembers key
- 刪除數(shù)據(jù)
- strem key member1 [member2]
- 獲取集合數(shù)據(jù)總量
- scard key
- 判斷集合中是否包含指定數(shù)據(jù)
- sismember key member
- 隨機獲取集合中指定數(shù)量的數(shù)據(jù)
- srandmember key [count]
- 隨機獲取集合中某個數(shù)據(jù)并將該數(shù)據(jù)移出集合
- apop key
- 兩個集合的交、并、差集
- sinter key1 [key2]
- sunion key1 [key2]
- adiff key1 [key2]
- 求兩個集合的交、并、差集并存儲到指定集合中
- sinterstore destination key1 [key2]
- sunionstore destination key1 [key2]
- sdiffstore destination key1 [key2]
- 將指定數(shù)據(jù)從原始集合中移動到目標集合中
- smove source destination member
注意事項
- set類型不允許有重復數(shù)據(jù),如果添加的數(shù)據(jù)在set中已存在,將只保留一份
- set雖然與hash存儲結(jié)構(gòu)相同,但無法啟用hash中存儲值的空間

基礎(chǔ)數(shù)據(jù)類型:sortedSet
- 存儲需求:數(shù)據(jù)排序有利于數(shù)據(jù)的展示效果,需要提供一種可以根據(jù)自身特征進行排序的方式
- 存儲結(jié)構(gòu):可以保存排序的數(shù)據(jù)
- 存儲類型:在set的存儲結(jié)構(gòu)上添加可排序字段

基本操作
- 添加數(shù)據(jù)
- zadd key scorel member [score2 member2]
- 獲取全部數(shù)據(jù)
- zrange key start stop [witchscores]
- zrevrange key star stop [witchscores]
- 刪除數(shù)據(jù)
- zrem key member [member ...]
- 按條件查詢數(shù)據(jù)
- zrangebyscore key min max [withscores] [limit]
- zrevrangebyscore key max min [withscores]
- 條件刪除數(shù)據(jù)
- zremrangebyrank key start stop //start stop 表示索引的開始結(jié)束位置
- zremrangebyscore key min max //min max表示排序的最小到最大位置
- 獲取集合數(shù)據(jù)數(shù)量
- zcard key
- zcount key min max
- 集合交、并操作
- zinterstore destination numkeys key [key ...]
- zunionstore destination key [key ...]



























