正在为你努力加载中........

点滴记录 分享
12
2015-12
首页 >> redis >> redis 集合常见方法

redis 集合常见方法

sets 类型及其操作
set集合相当于数学中的集合概念,利用他可以计算交集并集差集(注意此数据是没有顺序的哦,一定晓得)
--------------------------------------------------------
sadd 向名称为key的集合中添加元素
127.0.0.1:6379> SADD set1 aa
(integer) 1
---------------------------------------------------------
SMEMBERS 查看集合中元素
127.0.0.1:6379> SMEMBERS set1
1) "bb"
2) "aa"
---------------------------------------------------------
SREM 删除集合中的元素
127.0.0.1:6379> SADD set1 cc
(integer) 1
127.0.0.1:6379> SREM set1 cc
(integer) 1
127.0.0.1:6379> SMEMBERS set1
1) "bb"
2) "aa"
----------------------------------------------------------
sdiff 比较2个集合的差集
127.0.0.1:6379> SADD myset1 1 2 
(integer) 2
127.0.0.1:6379> SADD myset2 2 3 
(integer) 2
127.0.0.1:6379> SDIFF myset1 myset2
1) "1"
----------------------------------------------------------
SDIFFSTORE 返回2个集合的差集 并且存储到另外一个集合里面
127.0.0.1:6379> SDIFFSTORE myset3 myset2 myset1
(integer) 1
127.0.0.1:6379> SMEMBERS myset3
1) "3"
----------------------------------------------------------
SINTER 返回2个集合的交集
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
127.0.0.1:6379> SMEMBERS myset2
1) "2"
2) "3"
127.0.0.1:6379> SINTER myset1 myset2
1) "2"
----------------------------------------------------------------
SUNION 获取2个集合的并集
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
127.0.0.1:6379> SMEMBERS myset2
1) "2"
2) "3"
127.0.0.1:6379> SUNION myset1 myset2
1) "1"
2) "2"
3) "3"
----------------------------------------------------------------
SUNIONSTORE 返回2个集合的并集,并存储到另外一个集合里面
127.0.0.1:6379> SUNIONSTORE myset7 myset1 myset2
(integer) 3
127.0.0.1:6379> SMEMBERS myset7
1) "1"
2) "2"
3) "3"
-----------------------------------------------------------------
SCARD 返回集合的元素个数
127.0.0.1:6379> SCARD myset1
(integer) 2
------------------------------------------------------------------
SISMEMBER 判断集合的元素是否存在
127.0.0.1:6379> SMEMBERS myset1
1) "1"
2) "2"
127.0.0.1:6379> SISMEMBER myset1 3
(integer) 0
127.0.0.1:6379> SISMEMBER myset1 2
(integer) 1
-------------------------------------------------------------------
SRANDMEMBER 随机返回集合中的某个元素
127.0.0.1:6379> SRANDMEMBER myset1
"1"
-------------------------------------------------------------------
有序集合,是集合的一个升级版本,只是增加了一个顺序
ZADD 添加集合元素
127.0.0.1:6379> ZADD zset1 1 one
(integer) 1
127.0.0.1:6379> ZADD zset1 2 two
(integer) 1
127.0.0.1:6379> ZADD zset1 3 three
(integer) 1
-------------------------------------------------------------------
ZRANGE 获取集合元素
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
注意上面加了一个 withscores , 返回顺序号
-------------------------------------------------------------------
ZREM 删除集合中的元素
127.0.0.1:6379> ZREM zset1 two
(integer) 1
-------------------------------------------------------------------
ZRANK 返回集合的索引值
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "one"
2) "three"
127.0.0.1:6379> ZRANK zset1 one
(integer) 0
127.0.0.1:6379> ZRANK zset1 three
(integer) 1
--------------------------------------------------------------------
ZCARD 返回集合元素的个数
127.0.0.1:6379> ZCARD zset1
(integer) 2

作者:一根烟的寂寞 分类:redis
浏览:858 评论:0

发表评论: