Redis无序集合数据类型操作命令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis无序集合数据类型操作命令相关的知识,希望对你有一定的参考价值。
Set是集合,它是string类型的无序集合。set是通过hash table实现的,添加删除和查找的负责度都是0(1)。对集合我们可以取并集,交集、差集。通过这些操作我们可以实现QQ和微博中的好友推荐和blog的tag功能。
sadd:向名称为key的set中添加元素
127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 0
smembers: 查看集合中的所有元素
127.0.0.1:6379> smembers myset1 1) "two" 2) "one"
srem:删除集合中的元素
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> smembers myset2 1) "two" 2) "three" 3) "one" 127.0.0.1:6379> spop myset2 "two" 127.0.0.1:6379> smembers myset2 1) "three" 2) "one"
sdiff:返回所有给定key与第一个key的差集
127.0.0.1:6379> smembers myset3 1) "two" 2) "four" 3) "three" 4) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "one" 127.0.0.1:6379> sdiff myset3 myset2 1) "two" 2) "four" 127.0.0.1:6379> sdiff myset2 myset3 (empty list or set)
注意sdiff myset2 myset3 与 sdiff myset3 myset2的区别
sdiffstore:返回所有给定key与第一个key的差集,并将结果存为另一个key
127.0.0.1:6379> sdiff myset3 myset2 1) "two" 2) "four" 127.0.0.1:6379> sdiffstore myset4 myset3 myset2 (integer) 2 127.0.0.1:6379> smembers myset4 1) "two" 2) "four"
sinter:返回所有给定key的交集
127.0.0.1:6379> smembers myset2 1) "three" 2) "one" 127.0.0.1:6379> smembers myset3 1) "two" 2) "four" 3) "three" 4) "one" 127.0.0.1:6379> sinter myset2 myset3 1) "three" 2) "one"
sinterstore:返回所有给定key的交集,并将结果存为另一个key
127.0.0.1:6379> smembers myset3 1) "two" 2) "four" 3) "three" 4) "one" 127.0.0.1:6379> sinter myset2 myset3 1) "three" 2) "one" 127.0.0.1:6379> sinterstore myset5 myset2 myset3 (integer) 2 127.0.0.1:6379> smembers myset5 1) "three" 2) "one"
sunion:返回所有给定key的并集
127.0.0.1:6379> smembers myset2 1) "three" 2) "one" 127.0.0.1:6379> smembers myset3 1) "two" 2) "four" 3) "three" 4) "one" 127.0.0.1:6379> sunion myset2 myset3 1) "four" 2) "three" 3) "one" 4) "two"
suionstore:返回所有给定key的并集,并将结果存为另一个key
127.0.0.1:6379> smembers myset2 1) "three" 2) "one" 127.0.0.1:6379> smembers myset3 1) "two" 2) "four" 3) "three" 4) "one" 127.0.0.1:6379> sunionstore myset6 myset2 myset3 (integer) 4 127.0.0.1:6379> smembers myset6 1) "four" 2) "three" 3) "one" 4) "two"
smove:从第一个key对应的set中移除member并添加到第二个对应的set中
127.0.0.1:6379> smembers myset3 1) "two" 2) "four" 3) "three" 4) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "one" 127.0.0.1:6379> smove myset3 myset2 two (integer) 1 127.0.0.1:6379> smembers myset2 1) "two" 2) "three" 3) "one" 127.0.0.1:6379> smembers myset3 1) "four" 2) "three" 3) "one"
scard:返回名称为key的集合的元素个数
127.0.0.1:6379> smembers myset3 1) "four" 2) "three" 3) "one" 127.0.0.1:6379> scard myset3 (integer) 3
sismember:测试member是否是名称为key的set的元素,如果是则返回1,否则返回0
127.0.0.1:6379> smembers myset3 1) "four" 2) "three" 3) "one" 127.0.0.1:6379> sismember myset3 one (integer) 1 127.0.0.1:6379> sismember myset3 two (integer) 0
srandmember:随机返回名称为key的set的一个元素,但不删除元素
127.0.0.1:6379> smembers myset3 1) "four" 2) "three" 3) "one" 127.0.0.1:6379> srandmember myset3 "one" 127.0.0.1:6379> srandmember myset3 "four" 127.0.0.1:6379> srandmember myset3 "three" 127.0.0.1:6379> srandmember myset3 "one" 127.0.0.1:6379> srandmember myset3 "three"
本文出自 “zengestudy” 博客,请务必保留此出处http://zengestudy.blog.51cto.com/1702365/1859705
以上是关于Redis无序集合数据类型操作命令的主要内容,如果未能解决你的问题,请参考以下文章
laravel-- 在laravel操作redis数据库的数据类型(string哈希无序集合list链表有序集合)