相关命令
1.SADD
SADD key-name item1 [item 2…]
将一个或多个成员元素加入到集合中
2.SREM
SMEMBERS key-name item1 [item 2…]
移除集合中的一个或多个成员元素,不存在的成员元素会被忽略
3.SMEMBERS
SMEMBERS key-name
返回集合中的所有的成员。 不存在的集合 key 被视为空集合
4.SCARD
SCARD key-name
返回集合中元素的数量
php示例
$redis = new redis(); $redis->connect('127.0.0.1', 6609); $redis->delete('set1'); $redis->delete('set2'); $redis -> sAdd('set1','a','b','c','d','e'); var_dump($redis -> sMembers('set1')); echo "<br/>"; //rem //结果 array(5) { [0]=> string(1) "d" [1]=> string(1) "a" [2]=> string(1) "c" [3]=> string(1) "b" [4]=> string(1) "e" } var_dump($redis -> sRem('set1','d','e')); echo "<br/>"; //结果 int(2) var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果 array(3) { [0]=> string(1) "b" [1]=> string(1) "c" [2]=> string(1) "a" } var_dump($redis -> sCard('set1')); echo "<br/>"; //结果 int(3)
5.SMOVE
SMOVE source-key dest-key item
将 item元素从 source-key集合移动到 dest-key 集合
成功移除返回1,否则返回0
php示例
var_dump($redis -> sMove('set1','set2','c')); echo "<br/>"; //结果 bool(true) var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果 array(2) { [0]=> string(1) "b" [1]=> string(1) "a" } var_dump($redis -> sMembers('set2')); echo "<br/>"; //结果 array(1) { [0]=> string(1) "c" }
6.SPOP
SPOP key-name
随机移除集合中的一个元素
php示例
//spop var_dump($redis -> sPop('set1')); echo "<br/>"; //结果 string(1) "a" var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果 array(1) { [0]=> string(1) "b" }
7.SISMEMBER
SMEMBERS key-name item
检测元素item是否存在与几何key-name中
php示例
var_dump($redis -> sMembers('set1')); //结果 array(2) { [0]=> string(1) "b" [1]=> string(1) "a" } var_dump($redis -> sIsMember('set1','a')); echo "<br/>"; //结果 bool(true)
8.SRANDMEMBER
SRANDMEMBER key-name [count]
从集合里面随机返回一个或多个元素
php示例
//srandmember $redis->delete('set1'); $redis -> sAdd('set1','a','b','c','d','e'); var_dump($redis -> sRandMember('set1')); echo "<br/>"; //结果 string(1) "b" var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果 array(5) { [0]=> string(1) "c" [1]=> string(1) "b" [2]=> string(1) "e" [3]=> string(1) "a" [4]=> string(1) "d" } var_dump($redis -> sRandMember('set1',3)); echo "<br/>"; //结果 array(3) { [0]=> string(1) "c" [1]=> string(1) "b" [2]=> string(1) "d" } var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果array(5) { [0]=> string(1) "c" [1]=> string(1) "b" [2]=> string(1) "e" [3]=> string(1) "a" [4]=> string(1) "d" }
9.SINTER
SINTER key-name [key-name1……]
返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集
10.SINTERSTORE
SINTERSTORE key-name [key-name1……]
将给定集合之间的交集存储在指定的集合中。如果指定的集合已经存在,则将其覆盖
php示例
$redis->delete('set1'); $redis->delete('set2'); $redis -> sAdd('set1','a','b','c','d','e'); $redis -> sAdd('set2','a','b','x','y','z'); var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果array(5) { [0]=> string(1) "d" [1]=> string(1) "a" [2]=> string(1) "c" [3]=> string(1) "b" [4]=> string(1) "e" } var_dump($redis -> sMembers('set2')); echo "<br/>"; //结果array(5) { [0]=> string(1) "y" [1]=> string(1) "x" [2]=> string(1) "a" [3]=> string(1) "b" [4]=> string(1) "z" } var_dump($redis -> sInter('set1','set2')); echo "<br/>"; //结果array(2) { [0]=> string(1) "a" [1]=> string(1) "b" } var_dump($redis -> sInterStore('set-inter','set1','set2')); echo "<br/>"; //int(2) var_dump($redis -> sMembers('set-inter')); echo "<br/>"; //结果array(2) { [0]=> string(1) "a" [1]=> string(1) "b" }
11.SUNION
SUNION key-name [key-name1……]
返回给定集合的并集。不存在的集合 key 被视为空集
12.SUNIONSTORE
SUNIONSTORE dest-key key-name [key-name1……]
将给定集合的并集存储在指定的集合 dest-key中
php示例
$redis->delete('set1'); $redis->delete('set2'); $redis -> sAdd('set1','a','b','c','d','e'); $redis -> sAdd('set2','a','b','x','y','z'); var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果array(5) { [0]=> string(1) "d" [1]=> string(1) "a" [2]=> string(1) "c" [3]=> string(1) "b" [4]=> string(1) "e" } var_dump($redis -> sMembers('set2')); echo "<br/>"; //结果array(5) { [0]=> string(1) "y" [1]=> string(1) "x" [2]=> string(1) "a" [3]=> string(1) "b" [4]=> string(1) "z" } var_dump($redis -> sUnion('set1','set2')); echo "<br/>"; //结果array(8) { [0]=> string(1) "y" [1]=> string(1) "z" [2]=> string(1) "c" [3]=> string(1) "b" [4]=> string(1) "e" [5]=> string(1) "x" [6]=> string(1) "d" [7]=> string(1) "a" } var_dump($redis -> sUnionStore('set-union','set1','set2')); echo "<br/>"; //int(8) var_dump($redis -> sMembers('set-union')); echo "<br/>"; //结果array(8) { [0]=> string(1) "y" [1]=> string(1) "z" [2]=> string(1) "c" [3]=> string(1) "b" [4]=> string(1) "e" [5]=> string(1) "x" [6]=> string(1) "d" [7]=> string(1) "a" }
13.SDIFF
SDIFF key-name [key-name1……]
返回给定集合之间的差集
14.SDIFFSTORE
SDIFF dest-key key-name [key-name1……]
将给定集合之间的差集存储在指定的集合dest-key中
php示例
$redis->delete('set1'); $redis->delete('set2'); $redis -> sAdd('set1','a','b','c','d','e'); $redis -> sAdd('set2','a','b','x','y','z'); var_dump($redis -> sMembers('set1')); echo "<br/>"; //结果array(5) { [0]=> string(1) "d" [1]=> string(1) "a" [2]=> string(1) "c" [3]=> string(1) "b" [4]=> string(1) "e" } var_dump($redis -> sMembers('set2')); echo "<br/>"; //结果array(5) { [0]=> string(1) "y" [1]=> string(1) "x" [2]=> string(1) "a" [3]=> string(1) "b" [4]=> string(1) "z" } var_dump($redis -> sDiff('set1','set2')); echo "<br/>"; //结果array(3) { [0]=> string(1) "d" [1]=> string(1) "c" [2]=> string(1) "e" } var_dump($redis -> sDiffStore('set-diff','set1','set2')); echo "<br/>"; //int(3) var_dump($redis -> sMembers('set-diff')); echo "<br/>"; //结果array(3) { [0]=> string(1) "d" [1]=> string(1) "c" [2]=> string(1) "e" }
sisMember
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20396.html