list是可以有重复出现的,且list是有序的,list有序,这个序描述的是什么顺序,没错,是存入的插入的弹出的顺序对 他并不会去做排序的,注意list没有跟你去做排序,所以这时候把list弄清楚之后,然 后再来看set,set是一种去重的一种集合,而且他里边不维护排序,也不维护插入和弹出元 素的顺序,它完全是乱序的。set去重集合,不维护存入顺序,无序的 常用命令如下:
127.0.0.1:6379> help @set
SADD key member [member ...]
summary: Add one or more members to a set
since: 1.0.0
SCARD key
summary: Get the number of members in a set
since: 1.0.0
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0
SDIFFSTORE destination key [key ...]
summary: Subtract multiple sets and store the resulting set in a key
since: 1.0.0
SINTER key [key ...]
summary: Intersect multiple sets
since: 1.0.0
SINTERSTORE destination key [key ...]
summary: Intersect multiple sets and store the resulting set in a key
since: 1.0.0
SISMEMBER key member
summary: Determine if a given value is a member of a set
since: 1.0.0
SMEMBERS key
summary: Get all the members in a set
since: 1.0.0
SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0
SPOP key [count]
summary: Remove and return one or multiple random members from a set
since: 1.0.0
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0
SREM key member [member ...]
summary: Remove one or more members from a set
since: 1.0.0
SSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate Set elements
since: 2.8.0
SUNION key [key ...]
summary: Add multiple sets
since: 1.0.0
SUNIONSTORE destination key [key ...]
summary: Add multiple sets and store the resulting set in a key
since: 1.0.0
实例
127.0.0.1:6379> sadd k1 "a" "b" "c" "d" 给k赋值
(integer) 4
127.0.0.1:6379> sadd k2 "a" "b" "f" "g"
(integer) 4
127.0.0.1:6379> SCARD k1 得到数量
(integer) 4
127.0.0.1:6379> SDIFF k1 k2 取差集 k1在前 取k1独有的
1) "c"
2) "d"
127.0.0.1:6379> SDIFF k2 k1 取差集 k2在前 取k2独有的
1) "f"
2) "g"
127.0.0.1:6379> SDIFFSTORE k3 k1 k2 差集存在目标k中减少IO
(integer) 2
127.0.0.1:6379> SCARD k3
(integer) 2
127.0.0.1:6379> SINTER k1 k2 交集
1) "a"
2) "b"
127.0.0.1:6379> SINTER k4 k1 k2
(empty list or set)
127.0.0.1:6379> SINTERSTORE k4 k1 k2 交集存入目标k
(integer) 2
127.0.0.1:6379> SISMEMBER k4 "a" 看v是否存在k中
(integer) 1
127.0.0.1:6379> SISMEMBER k4 "g"
(integer) 0
127.0.0.1:6379> SMEMBERS k1 查k全部数据
1) "c"
2) "a"
3) "b"
4) "d"
127.0.0.1:6379> SMEMBERS k2
1) "a"
2) "b"
3) "g"
4) "f"
127.0.0.1:6379> SMEMBERS k3
1) "c"
2) "d"
127.0.0.1:6379> SMEMBERS k4
1) "a"
2) "b"
127.0.0.1:6379> SMOVE k3 k4 "c" 把数据从k3移动到k4
(integer) 1
127.0.0.1:6379> SMEMBERS k4
1) "c"
2) "a"
3) "b"
127.0.0.1:6379> SRANDMEMBER k4 6 随机事件
1) "c"
2) "a"
3) "b"
127.0.0.1:6379> SRANDMEMBER k4 2
1) "c"
2) "b"
127.0.0.1:6379> SRANDMEMBER k4 -6
1) "c"
2) "b"
3) "a"
4) "b"
5) "c"
6) "a"
127.0.0.1:6379> SUNION k1 k2 并集
1) "d"
2) "f"
3) "g"
4) "c"
5) "a"
6) "b"
127.0.0.1:6379> SUNIONSTORE k5 k1 k2
(integer) 6
127.0.0.1:6379> SMEMBERS k5
1) "d"
2) "f"
3) "g"
4) "c"
5) "a"
6) "b"
127.0.0.1:6379> SREM k5 "a" 移除命令
(integer) 1
127.0.0.1:6379> SMEMBERS k5
1) "d"
2) "f"
3) "g"
4) "c"
5) "b"
127.0.0.1:6379> SREM k5 "b" "c"
(integer) 2
127.0.0.1:6379> SMEMBERS k5
1) "d"
2) "f"
3) "g"
比如先对我们的k2和k3回车,sinter k2 k3,然后他就直接把结果给你 了,就返回给你的客户端了,4、5就是他的交集。明白了吧?但是这时候如果你接了一个 store,然后给出一个目标k,dest,回车,然后你的客户端是拿不到,然后这时候你可以单 独的去 smembers dest,回车可以拿里面交集的元素,好吧,那么这两点其实也是一个作者 的比较细腻的一点,因为带不带store它可以不开发,它完全直接开发单一命令,但是如果 你想让redis里边既有k2、k3那种全量集,又有它的一个交集,那么你客户端必须要先执行 sinter把数据取回,然后把数据邮走到客户端,然后再把这个数据store到你一个新的目标k 里,等于数据会有一个来回的交互的过程,这叫移动数据。如果有命令的话,你客户端触 发命令只需要给出两个数作为参数给这个目标的,然后所有的做交集和生成新的k的过程是 在服务端一步完成的数据是没有进入I/O对不对
随机事件:
SRANDMEMBER k number 抽奖问题 number 为正数 取出来一个去重结果集不能超过已有的 最多出来k的全部数据 会小于等于num (狼多肉少) number 为0 不取数据 number 为负数 取出一个带重复的数量为num的结果集 数量可以大于k的全部v (肉多狼少)
如果是一次一个的抽取 可以用spop
|