redis set
list和set的区别(后期可能在添加修改,目前我觉得这两个可能是明显的区别):
set中不允许出现重复的元素,list可以出现
set是无序的,list是有序的(这里面说的有序是指删除添加操作是顺序执行的)
1.set增操作
1.sadd key member member …… key存在会报错,正常返回set集合元素的数量
127.0.0.1:6379> sadd person lixue zhangsan wangwu (error) WRONGTYPE Operation against a key holding the wrong kind of value 127.0.0.1:6379> sadd name lixue zhangsan wangwu (integer) 3
2.set查操作
1.获取集合中的元素个数,scard key,这个命令只能用于set结构的数据,否则会报错,返回集合元素个数
127.0.0.1:6379> scard name (integer) 3 127.0.0.1:6379> scard person (error) WRONGTYPE Operation against a key holding the wrong kind of value 127.0.0.1:6379>
2.获取元素是否在集合中SISMEMBER key member 返回1或0(在或者不在)
127.0.0.1:6379> SISMEMBER name lixue (integer) 1 127.0.0.1:6379> SISMEMBER person lixue (error) WRONGTYPE Operation against a key holding the wrong kind of value 127.0.0.1:6379> SISMEMBER name zheng (integer) 0
3.获取集合中的所有元素SMEMBERS key
127.0.0.1:6379> SMEMBERS name 1) "zhangsan" 2) "wangwu" 3) "lixue"
4.随机获取集合中的元素,srandmember key count 这个是随机获取的也不会按照顺序来
127.0.0.1:6379> SRANDMEMBER name 4 1) "wangwu" 2) "zhenyi" 127.0.0.1:6379> SRANDMEMBER name 1 1) "wangwu" 127.0.0.1:6379> SRANDMEMBER name 1 1) "zhenyi" 127.0.0.1:6379>
3.删除操作
1.随机从集合中删除某个元素,spop key,这个不像list有序,所以只能随机删除
127.0.0.1:6379> spop name "zhangsan" 127.0.0.1:6379> smembers name 1) "wangwu" 2) "lixue"
2.删除集合中的指定元素,可以批量,返回删除数量 srem key member member……,返回删除数量,如果你批量中有不存在的元素,他只会删除存在的元素,返回相应的删除的数量
127.0.0.1:6379> srem name lixue wangwu (integer) 2 127.0.0.1:6379> smembers name (empty list or set) 127.0.0.1:6379> sadd name lixue (integer) 1 127.0.0.1:6379> smembers name 1) "lixue"
127.0.0.1:6379> smembers name
1) "zhenyi"
2) "wangwu"
3) "lixue"
127.0.0.1:6379> srem name lixue li
(integer) 1
127.0.0.1:6379> smembers name
1) "zhenyi"
2) "wangwu"
3.移动将现有的集合中的某个元素转移到其他的集合中,(smove key 来源集合 目的集合 值) 返回转移的数量,只能转移一个
127.0.0.1:6379> smove name name1 zhengyi (integer) 0 127.0.0.1:6379> smove name name1 zhenyi (integer) 1 127.0.0.1:6379> smembers name 1) "wangwu" 127.0.0.1:6379> smembers name1 1) "zhenyi"
127.0.0.1:6379> smove name name1 lixue wangwu
(error) ERR wrong number of arguments for 'smove' command
4.集合间的运算
1.集合间差集运算,sdiff key key…… 这里面返回值是前面的key 与后面所有key之间的差异
127.0.0.1:6379> smembers name 1) "wangwu" 2) "lixue" 127.0.0.1:6379> smembers name1 1) "zhenyi" 2) "lixue" 127.0.0.1:6379> sdiff name name1 1) "wangwu" 127.0.0.1:6379> sdiff name 1) "wangwu" 2) "lixue" 127.0.0.1:6379> sdiff name1 name 1) "zhenyi"
2.集合间差集运算并将结果存储,SDIFFSTORE 目标集合 集合1 集合2...,这里面需要注意的是结果会将原有集合的数据覆盖,如果这个目标集合不存在则会创建一个这样的集合将结果存进去,某一key不存在,返回值为0
127.0.0.1:6379> smembers name 1) "zhenyi" 2) "lixue" 127.0.0.1:6379> smembers name1 1) "zhenyi" 2) "11111" 3) "lixue" 127.0.0.1:6379> smembers source 1) "zheng" 127.0.0.1:6379> SDIFFSTORE source name1 name (integer) 1 127.0.0.1:6379> smembers source 1) "11111"
3.集合间的交集运算 sinter集合1 集合2...
127.0.0.1:6379> smembers name1 1) "zhenyi" 2) "11111" 3) "lixue" 127.0.0.1:6379> smembers name 1) "zhenyi" 2) "lixue" 127.0.0.1:6379> SINTER name 1) "zhenyi" 2) "lixue" 127.0.0.1:6379> SINTER name name1 1) "zhenyi" 2) "lixue"
4.计算集合间的交集,并将结果存储在集合中SINTERSTORe 目标集合 集合1集合2...注意点同上计算差集是相同的某一key不存在,返回值为其中一个其中的key中的元素
127.0.0.1:6379> SINTERSTORe source1 name name1 (integer) 2 127.0.0.1:6379> smembers source1 1) "zhenyi" 2) "lixue"
127.0.0.1:6379> sdiff name 0sou
1) "zhenyi"
2) "lixue"
5.计算交集并存储,SINTERSTORE 目标集合 集合1 集合2... 注意事项和上面的是一样的
127.0.0.1:6379> SINTERSTORE lixue name name1 (integer) 2 127.0.0.1:6379> smembers lixue 1) "zhenyi" 2) "lixue"
6.计算并集SUNION 集合1 集合2...
127.0.0.1:6379> SUNION name name1 1) "zhenyi" 2) "11111" 3) "lixue" 127.0.0.1:6379> SUNION name 1) "zhenyi" 2) "lixue"
7.将并集放入集合中注意事项和上面的是一样的
127.0.0.1:6379> SUNIONSTORE lixue name1 name (integer) 3 127.0.0.1:6379> smembers lixue 1) "zhenyi" 2) "lixue" 3) "11111"