unordered collection of unique strings.set值是唯一的字元串的無序集合, 把握住兩個特點: 唯一, 無序. 清空所有的數據, 並清理顯示界面: 保存一條 set 數據, 鍵是 myset1, 值是 1, 2, 3, 4 四個數字: 查看鍵myset1 的值: ...
unordered collection of unique strings.
set值是唯一的字元串的無序集合, 把握住兩個特點: 唯一, 無序.
清空所有的數據, 並清理顯示界面:
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> clear
保存一條 set 數據, 鍵是 myset1, 值是 1, 2, 3, 4 四個數字:
127.0.0.1:6379> sadd myset1 1 2 3 4
(integer) 4
查看鍵myset1 的值:
127.0.0.1:6379> smembers myset1
1) "1"
2) "2"
3) "3"
4) "4"
在鍵myset1里增加一個已有的數字3, 返回0, 表示添加不成功, 因為 set值是要求唯一的:
127.0.0.1:6379> sadd myset1 3
(integer) 0
127.0.0.1:6379> smembers myset1
1) "1"
2) "2"
3) "3"
4) "4"
在鍵myset1里增加一個新數字5, 返回1, 表示添加成功:
127.0.0.1:6379> sadd myset1 5
(integer) 1
127.0.0.1:6379> smembers myset1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
查看鍵myset1的值里一共有幾天數據:
127.0.0.1:6379> scard myset1
(integer) 5
新增一條 set 數據myset2:
127.0.0.1:6379> sadd myset2 5 8 7 6 4 9
(integer) 6
127.0.0.1:6379> smembers myset2
1) "4"
2) "5"
3) "6"
4) "7"
5) "8"
6) "9"
查看兩條 set 數據 myset1 和 myset2 值的區別, myset1 寫在前面, 表示查出所有 myset1 里有, 而 myset2 里沒有的值:
127.0.0.1:6379> smembers myset1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> sdiff myset1 myset2
1) "1"
2) "2"
3) "3"
查出 myset1 相對於 myset2 里值的差別, 並且保存成一個新 set --- myset3:
127.0.0.1:6379> sdiffstore myset3 myset1 myset2
(integer) 3
127.0.0.1:6379> smembers myset3
1) "1"
2) "2"
3) "3"
合併 myset1 和 myset2 里的所有值, 由於 set值唯一性的特點, 重覆的部分只保留一個:
127.0.0.1:6379> sunion myset1 myset2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
合併 myset1 和 myset2 里的所有值, 並且保存成一個新 set --- myset4:
127.0.0.1:6379> sunionstore myset4 myset1 myset2
(integer) 9
127.0.0.1:6379> smembers myset4
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
指定刪除 set值里的某一個數
127.0.0.1:6379> srem myset4 8
(integer) 1
127.0.0.1:6379> smembers myset4
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "9"
指定刪除 set值里的某幾個數
127.0.0.1:6379> srem myset4 6 7 9
(integer) 3
127.0.0.1:6379> smembers myset4
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
隨機刪除 set值里的某一個數
127.0.0.1:6379> spop myset4 1
1) "5"
127.0.0.1:6379> smembers myset4
1) "1"
2) "2"
3) "3"
4) "4"
隨機刪除 set值里的某幾個數
127.0.0.1:6379> spop myset4 3
1) "2"
2) "4"
3) "1"
取兩個 set值的並集:
127.0.0.1:6379> smembers myset1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
127.0.0.1:6379> smembers myset2
1) "4"
2) "5"
3) "6"
4) "7"
5) "8"
6) "9"
127.0.0.1:6379> sinter myset1 myset2
1) "4"
2) "5"
取兩個 set值的並集, 並保存:
127.0.0.1:6379> sinterstore myset5 myset1 myset2
(integer) 2
127.0.0.1:6379> smembers myset5
1) "4"
2) "5"
把 myset1 的值里的數字 1 挪到 myset2
127.0.0.1:6379> smove myset1 myset2 1
(integer) 1
127.0.0.1:6379> smembers myset2
1) "1"
2) "4"
3) "5"
4) "6"
5) "7"
6) "8"
7) "9"
127.0.0.1:6379> smembers myset1
1) "2"
2) "3"
3) "4"
4) "5"
以上, 就是關於 set值的一些常用命令, enjoy......