set是string类型的无序集合。通过hash table实现。
sadd:向名称为key的set中添加元素,不能添加重复的元素。
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset world
(integer) 1
127.0.0.1:6379> sadd myset world
(integer) 0
127.0.0.1:6379> smembers myset
1) "hello"
2) "world"
srem:删除名称为key的set中的元素。
127.0.0.1:6379> sadd myset2 one
(integer) 1
127.0.0.1:6379> sadd myset2 two
(integer) 1
127.0.0.1:6379> srem myset2 one
(integer) 1
127.0.0.1:6379> smembers myset2
1) "two"
spop:随机返回并删除名称为key的set中一个元素。
127.0.0.1:6379> sadd myset3 one
(integer) 1
127.0.0.1:6379> sadd myset3 two
(integer) 1
127.0.0.1:6379> spop myset3
"two"
127.0.0.1:6379> smembers myset3
1) "one"
sdiff:返回所有给定key与第一个key的差集
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sdiff myset2 myset3
1) "three"
sdiffstore:返回所有给定key与第一个key的差集,并将结果存为另一个key。
127.0.0.1:6379> sdiffstore myset4 myset2 myset3
(integer) 1
127.0.0.1:6379> smembers myset4
1) "three"
sinter:返回所有给定key的交集。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sinter myset2 myset3
1) "two"
sinterstore:返回所有给定key的交集,并将结果存为另一个key。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sinterstore myset6 myset2 myset3
(integer) 1
127.0.0.1:6379> smembers myset6
1) "two"
sunion:返回所有给定key的并集。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sunion myset2 myset3
1) "two"
2) "three"
3) "one"
sunionstore:返回所有给定key的并集,并将结果存为另一个key。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sunionstore myset6 myset2 myset3
(integer) 3
127.0.0.1:6379> smembers myset6
1) "two"
2) "three"
3) "one"
smove:从第一个key对应的set中移除member并添加到第二个对应的set中。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smove myset2 myset7 three
(integer) 1
127.0.0.1:6379> smembers myset2
1) "two"
127.0.0.1:6379> smembers myset7
1) "three"
scard:返回名称为key的set的元素个数。
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> scard myset3
(integer) 2
sismember:测试member是否是名称为key的set的元素
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sismember myset3 two
(integer) 1
127.0.0.1:6379> sismember myset3 three
(integer) 0
srandmember:随机返回名称为key的set的一个元素,但不删除元素。
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> srandmember myset3
"one"
127.0.0.1:6379> srandmember myset3
"two"
sadd:向名称为key的set中添加元素,不能添加重复的元素。
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset world
(integer) 1
127.0.0.1:6379> sadd myset world
(integer) 0
127.0.0.1:6379> smembers myset
1) "hello"
2) "world"
srem:删除名称为key的set中的元素。
127.0.0.1:6379> sadd myset2 one
(integer) 1
127.0.0.1:6379> sadd myset2 two
(integer) 1
127.0.0.1:6379> srem myset2 one
(integer) 1
127.0.0.1:6379> smembers myset2
1) "two"
spop:随机返回并删除名称为key的set中一个元素。
127.0.0.1:6379> sadd myset3 one
(integer) 1
127.0.0.1:6379> sadd myset3 two
(integer) 1
127.0.0.1:6379> spop myset3
"two"
127.0.0.1:6379> smembers myset3
1) "one"
sdiff:返回所有给定key与第一个key的差集
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sdiff myset2 myset3
1) "three"
sdiffstore:返回所有给定key与第一个key的差集,并将结果存为另一个key。
127.0.0.1:6379> sdiffstore myset4 myset2 myset3
(integer) 1
127.0.0.1:6379> smembers myset4
1) "three"
sinter:返回所有给定key的交集。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sinter myset2 myset3
1) "two"
sinterstore:返回所有给定key的交集,并将结果存为另一个key。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sinterstore myset6 myset2 myset3
(integer) 1
127.0.0.1:6379> smembers myset6
1) "two"
sunion:返回所有给定key的并集。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sunion myset2 myset3
1) "two"
2) "three"
3) "one"
sunionstore:返回所有给定key的并集,并将结果存为另一个key。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sunionstore myset6 myset2 myset3
(integer) 3
127.0.0.1:6379> smembers myset6
1) "two"
2) "three"
3) "one"
smove:从第一个key对应的set中移除member并添加到第二个对应的set中。
127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
127.0.0.1:6379> smove myset2 myset7 three
(integer) 1
127.0.0.1:6379> smembers myset2
1) "two"
127.0.0.1:6379> smembers myset7
1) "three"
scard:返回名称为key的set的元素个数。
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> scard myset3
(integer) 2
sismember:测试member是否是名称为key的set的元素
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> sismember myset3 two
(integer) 1
127.0.0.1:6379> sismember myset3 three
(integer) 0
srandmember:随机返回名称为key的set的一个元素,但不删除元素。
127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
127.0.0.1:6379> srandmember myset3
"one"
127.0.0.1:6379> srandmember myset3
"two"
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28536251/viewspace-2143378/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28536251/viewspace-2143378/