對redis來說,所有的key(鍵)都是字符串。
?文章來源:http://www.zghlxwxcb.cn/news/detail-469450.html
?
1.String 字符串類型
是redis中最基本的數(shù)據(jù)類型,一個key對應(yīng)一個value。
?
String類型是二進(jìn)制安全的,意思是 redis 的 string 可以包含任何數(shù)據(jù)。如數(shù)字,字符串,jpg圖片或者序列化的對象。
?
使用:get 、 set 、 del 、 incr、 decr 等
?
復(fù)制代碼
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379> get counter
"2"
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
"3"
127.0.0.1:6379> incrby counter 100
(integer) 103
127.0.0.1:6379> get counter
"103"
127.0.0.1:6379> decr counter
(integer) 102
127.0.0.1:6379> get counter
"102"
復(fù)制代碼
實(shí)戰(zhàn)場景:
?
1.緩存: 經(jīng)典使用場景,把常用信息,字符串,圖片或者視頻等信息放到redis中,redis作為緩存層,mysql做持久化層,降低mysql的讀寫壓力。
?
2.計數(shù)器:redis是單線程模型,一個命令執(zhí)行完才會執(zhí)行下一個,同時數(shù)據(jù)可以一步落地到其他的數(shù)據(jù)源。
?
3.session:常見方案spring session + redis實(shí)現(xiàn)session共享,
?
?
?
2.Hash (哈希)
是一個Mapmap,指值本身又是一種鍵值對結(jié)構(gòu),如 value={{field1,value1},......fieldN,valueN}}
?
?
?
?
?
使用:所有hash的命令都是 h 開頭的 hget 、hset 、 hdel 等
?
復(fù)制代碼
127.0.0.1:6379> hset user name1 hao
(integer) 1
127.0.0.1:6379> hset user email1 hao@163.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) "hao@163.com"
127.0.0.1:6379> hget user user
(nil)
127.0.0.1:6379> hget user name1
"hao"
127.0.0.1:6379> hset user name2 xiaohao
(integer) 1
127.0.0.1:6379> hset user email2 xiaohao@163.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) "hao@163.com"
5) "name2"
6) "xiaohao"
7) "email2"
8) "xiaohao@163.com"
復(fù)制代碼
實(shí)戰(zhàn)場景:
?
1.緩存: 能直觀,相比string更節(jié)省空間,的維護(hù)緩存信息,如用戶信息,視頻信息等。
?
?
?
?3.鏈表?
List 說白了就是鏈表(redis 使用雙端鏈表實(shí)現(xiàn)的 List),是有序的,value可以重復(fù),可以通過下標(biāo)取出對應(yīng)的value值,左右兩邊都能進(jìn)行插入和刪除數(shù)據(jù)。
?
?
?
使用列表的技巧
?
lpush+lpop=Stack(棧)
lpush+rpop=Queue(隊(duì)列)
lpush+ltrim=Capped Collection(有限集合)
lpush+brpop=Message Queue(消息隊(duì)列)
?
?
使用:
?
復(fù)制代碼
127.0.0.1:6379> lpush mylist 1 2 ll ls mem
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) "mem"
2) "ls"
3) "ll"
4) "2"
5) "1"
127.0.0.1:6379>
復(fù)制代碼
實(shí)戰(zhàn)場景:
?
1.timeline:例如微博的時間軸,有人發(fā)布微博,用lpush加入時間軸,展示新的列表信息。
?
?
?
4.Set 集合
集合類型也是用來保存多個字符串的元素,但和列表不同的是集合中 1. 不允許有重復(fù)的元素,2.集合中的元素是無序的,不能通過索引下標(biāo)獲取元素,3.支持集合間的操作,可以取多個集合取交集、并集、差集。
?
?
?
?
?
?
?
使用:命令都是以s開頭的 sset 、srem、scard、smembers、sismember
?
復(fù)制代碼
127.0.0.1:6379> sadd myset hao hao1 xiaohao hao
(integer) 3
127.0.0.1:6379> SMEMBERS myset
1) "xiaohao"
2) "hao1"
3) "hao"
127.0.0.1:6379> SISMEMBER myset hao
(integer) 1
復(fù)制代碼
?
?
實(shí)戰(zhàn)場景;
?
1.標(biāo)簽(tag),給用戶添加標(biāo)簽,或者用戶給消息添加標(biāo)簽,這樣有同一標(biāo)簽或者類似標(biāo)簽的可以給推薦關(guān)注的事或者關(guān)注的人。
?
2.點(diǎn)贊,或點(diǎn)踩,收藏等,可以放到set中實(shí)現(xiàn)
?
?
?
5.zset 有序集合
有序集合和集合有著必然的聯(lián)系,保留了集合不能有重復(fù)成員的特性,區(qū)別是,有序集合中的元素是可以排序的,它給每個元素設(shè)置一個分?jǐn)?shù),作為排序的依據(jù)。
?
(有序集合中的元素不可以重復(fù),但是score 分?jǐn)?shù) 可以重復(fù),就和一個班里的同學(xué)學(xué)號不能重復(fù),但考試成績可以相同)。
?
?
?
?
?
使用: 有序集合的命令都是 以 z 開頭 zadd 、 zrange、 zscore文章來源地址http://www.zghlxwxcb.cn/news/detail-469450.html
到了這里,關(guān)于Redis數(shù)據(jù)結(jié)構(gòu)簡介的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!