01-redis安裝:how2j.cn/k/redis/red…html
02-redis學習:<Redis開發與運維>(獲取本書PDF見文章末)java
它是用來保存多個字符串的元素,元素不能夠重複,而且元素是無序的,不能經過索引下標獲取元素。Redis除了支持元素的增刪改查以外,還支持集合之間的交集、並集和補集。redis
以下圖:集合user:1:follow包含着it、music、his和sports一共4個元素。一個集合最多能夠存儲2的32次方減1個元素。運維
經常使用的命令和時間複雜度學習
添加的元素不能重複,不然會返回0。能夠看出元素仍是無序的。編碼
127.0.0.1:6379> sadd myset a b c (integer) 3 127.0.0.1:6379> sadd myset a b (integer) 0 127.0.0.1:6379> smembers myset 1) "c" 2) "a" 3) "b" 複製代碼
127.0.0.1:6379> smembers myset
1) "c"
2) "a"
3) "b"
127.0.0.1:6379> srem myset a b (integer) 2 127.0.0.1:6379> smembers myset 1) "c" 複製代碼
127.0.0.1:6379> smembers myset
1) "c"
127.0.0.1:6379> scard myset (integer) 1 複製代碼
元素存在返回1,不存在返回0spa
127.0.0.1:6379> smembers myset
1) "c"
127.0.0.1:6379> sismember myset c (integer) 1 127.0.0.1:6379> sismember myset a (integer) 0 複製代碼
127.0.0.1:6379> sadd myset a b d (integer) 3 127.0.0.1:6379> srandmember myset "d" 127.0.0.1:6379> srandmember myset "a" 127.0.0.1:6379> smembers myset 1) "a" 2) "c" 3) "d" 4) "b" 複製代碼
127.0.0.1:6379> spop myset
"d"
127.0.0.1:6379> spop myset
"b"
127.0.0.1:6379> smembers myset
1) "a"
2) "c"
複製代碼
srandmember和spop都是隨機從集合選出元素,二者不一樣的是spop命令執行後,元素會從集合中刪除,而srandmember不會。3d
127.0.0.1:6379> smembers myset
1) "a"
2) "c"
複製代碼
添加2個集合,分別是user:1:follow和user:2:followcode
127.0.0.1:6379> sadd user:1:follow a b c d (integer) 4 127.0.0.1:6379> sadd user:2:follow a e f d (integer) 4 複製代碼
127.0.0.1:6379> sadd user:1:follow a b c d (integer) 4 127.0.0.1:6379> sadd user:2:follow a e f d (integer) 4 127.0.0.1:6379> sinter user:1:follow user:2:follow 1) "a" 2) "d" 複製代碼
127.0.0.1:6379> sadd user:1:follow a b c d (integer) 4 127.0.0.1:6379> sadd user:2:follow a e f d (integer) 4 127.0.0.1:6379> sunion user:1:follow user:2:follow 1) "c" 2) "a" 3) "e" 4) "d" 5) "b" 6) "f" 複製代碼
127.0.0.1:6379> sadd user:1:follow a b c d (integer) 4 127.0.0.1:6379> sadd user:2:follow a e f d (integer) 4 127.0.0.1:6379> sdiff user:1:follow user:2:follow 1) "c" 2) "b" 複製代碼
集合之間的運算在元素比較多的時候會比較耗時,因此將它們保存起來。cdn
交集
127.0.0.1:6379> sinterstore user:1_2:inter user:1:follow user:2:follow
(integer) 2
127.0.0.1:6379> type user:1_2:inter
set
127.0.0.1:6379> smembers user:1_2:inter
1) "a"
2) "d"
複製代碼
並集
127.0.0.1:6379> sunionstore user:1_2:sunion user:1:follow user:2:follow
(integer) 6
127.0.0.1:6379> type user:1_2:sunion
set
127.0.0.1:6379> smembers user:1_2:sunion
1) "c"
2) "a"
3) "e"
4) "d"
5) "b"
6) "f"
複製代碼
補集
127.0.0.1:6379> sdiffstore user:1_2:sdiff user:1:follow user:2:follow
(integer) 2
127.0.0.1:6379> type user:1_2:sdiff
set
127.0.0.1:6379> smembers user:1_2:sdiff
1) "c"
2) "b"
複製代碼
集合中的元素都是整數,而且元素的個數都小於512個時,redis會採用intset,減小內存的使用。
超過512個元素,redis會選擇hashtable。
示例
127.0.0.1:6379> sadd mekey 1 2 3 4 5
(integer) 5
127.0.0.1:6379> object encoding mekey
"intset"
複製代碼
127.0.0.1:6379> sadd mekey 1 2 3 4 5 6 ... 512 513
(integer) 509
127.0.0.1:6379> scard setkey (integer) 513 127.0.0.1:6379> object encoding listkey "hashtable" 複製代碼
127.0.0.1:6379> sadd mekey 1 a b d 4 5
(integer) 3
127.0.0.1:6379> object encoding mekey
"hashtable"
複製代碼
標籤
一個用戶對娛樂、新聞感興趣,另外一個用戶對歷史、科技感興趣,有了這些數據就能夠拿到喜歡同一個類型的用戶了,就能夠給對應的用戶推薦他們感興趣的內容。
127.0.0.1:6379> sadd user:1:tags one two three (integer) 3 127.0.0.1:6379> sadd user:2:tags one four five (integer) 3 複製代碼
127.0.0.1:6379> sadd one:users user:1 user:3
(integer) 2
127.0.0.1:6379> sadd two:users user:1
(integer) 1
複製代碼
用戶和標籤的關係應該放在一個事務中執行,防止事務不一致,致使命令執行失敗。
127.0.0.1:6379> smembers user:1:tags
1) "two"
2) "one"
3) "three"
127.0.0.1:6379> srem user:1:tags one (integer) 1 127.0.0.1:6379> smembers user:1:tags 1) "two" 2) "three" 複製代碼
127.0.0.1:6379> smembers one:users
1) "user:1"
2) "user:3"
127.0.0.1:6379> srem one:users user:1
(integer) 1
127.0.0.1:6379> smembers one:users
1) "user:3"
複製代碼
127.0.0.1:6379> smembers user:1:tags
1) "two"
2) "three"
3) "five"
127.0.0.1:6379> smembers user:2:tags
1) "one"
2) "five"
3) "four"
127.0.0.1:6379> sinter user:1:tags user:2:tags
1) "five"
複製代碼
連接:關注公衆號Elevenkeep,回覆redis便可得到連接。