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......