主要从事Linux,云原生架构改造,服务网格,ELK,python,golang等相关技术。
分类: NOSQL
2015-06-04 17:35:18
Sets类型及操作
Set是集合,它是string类型的无序集合。Set是通过hash table实现的,添加、删除和查找的复杂度都是O(1).对集合我们可以取并集、交集、差集。通过这些操作我们可以实现sns中的好友推荐和blog的tag功能。
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
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> sadd myset3 "three"
(integer) 1
127.0.0.1:6379> spop myset3
"two"
Sdiff
返回所有给定key与第一个key的差集
127.0.0.1:6379> smembers myset2
1) "two"
127.0.0.1:6379> smembers myset3
1) "three"
2) "one"
127.0.0.1:6379> sdiff myset2 myset3 这里是以myset2为标准的
1) "two"
Sdiffstore
返回所有给定key与第一个key的差集,并将结果存为另一个key
127.0.0.1:6379> SMEMBERS myset3
1) "three"
2) "one"
127.0.0.1:6379> SDIFFSTORE myset4 myset2 myset3
(integer) 1
Sinter
返回所有给定key的交集
127.0.0.1:6379> SMEMBERS myset2
1) "one"
2) "two"
127.0.0.1:6379> SMEMBERS myset3
1) "three"
2) "one"
127.0.0.1:6379> sinter myset2 myset3
1) "one"
Sinterstore
返回所有给定key的交集,并将结果存为另一个key
127.0.0.1:6379> sinterstore myset6 myset2 myset3
(integer) 1
127.0.0.1:6379> SMEMBERS myset6
1) "one"
Sunion
返回所有给定key的并集
127.0.0.1:6379> SMEMBERS myset2
1) "one"
2) "two"
127.0.0.1:6379> SMEMBERS myset3
1) "three"
2) "one"
127.0.0.1:6379> SUNION myset2 myset3
1) "three"
2) "two"
3) "one"
Sunionstore
返回所有给定key的并集
127.0.0.1:6379> SMEMBERS myset2
1) "one"
2) "two"
127.0.0.1:6379> SMEMBERS myset3
1) "three"
2) "one"
127.0.0.1:6379> SUNIONSTORE myset7 myset2 myset3
(integer) 2
Smove
从第一个key对应的set中移除member并添加第二个对应的set中
127.0.0.1:6379> SMEMBERS myset3
1) "three"
2) "one"
127.0.0.1:6379> SMEMBERS myset2
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> smove myset2 myset7 three 将myset2中的three移动到myset7
(integer) 1
127.0.0.1:6379> SMEMBERS myset2
1) "two"
2) "one"
Scard
返回名称为key的set的元素个数
127.0.0.1:6379> scard myset2
(integer) 2
Sismember
测试member是否是名称为key的set的元素
127.0.0.1:6379> SMEMBERS myset2
1) "two"
2) "one"
127.0.0.1:6379> SISMEMBER myset2 two
(integer) 1
127.0.0.1:6379> SISMEMBER myset2 three
(integer) 0
Srandmember
随机返回名称为key的set的一个元素,但不删除元素
127.0.0.1:6379> SMEMBERS myset3
1) "three"
2) "one"
127.0.0.1:6379> SRANDMEMBER myset3
"three"
Sorted sets类型及操作
Sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序。可以理解为两列的mysql表,一列存value,一列存书序。操作中key理解为zset的名字。
Zadd
添加有序元素,指定已经有的元素后它的顺序会随着更新
127.0.0.1:6379> zadd myzset 2 "one"
(integer) 1
127.0.0.1:6379> zadd myzset 1 "one"
(integer) 0
127.0.0.1:6379> zadd myzset 1 "two"
(integer) 1
127.0.0.1:6379> zadd myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZRANGE myzset 0 -1 withscores 并输出顺序号
1) "one"
2) "1"
3) "two"
4) "1"
5) "three"
6) "3"
Zrem
删除名称为key的zset中的元素member
127.0.0.1:6379> ZRANGE myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "1"
5) "three"
6) "3"
127.0.0.1:6379> zrem myzset two
(integer) 1
127.0.0.1:6379> ZRANGE myzset 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
Zincrby
在有序集合添加成员的分数
127.0.0.1:6379> zadd myzset2 1 "one"
(integer) 1
127.0.0.1:6379> zadd myzset2 1 "two"
(integer) 1
127.0.0.1:6379> ZINCRBY myzset2 2 "one"
"3"
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "one"
4) "3"
127.0.0.1:6379> ZRANGE myzset 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
127.0.0.1:6379> ZINCRBY myzset 2 "one"
"3"
127.0.0.1:6379> ZRANGE myzset 0 -1 withscores
1) "one"
2) "3"
3) "three"
4) "3"
Zrank
返回名称为key的zset中member元素的排名(按score从小到大排序)即下标
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "one"
4) "3"
127.0.0.1:6379> zrank myzset2 two
(integer) 0
127.0.0.1:6379> zrank myzset2 one
(integer) 1
Zrevrank
返回名称为key的zset中member元素的排名(按score从大到小排序)即下标
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "one"
4) "3"
127.0.0.1:6379> zrevrank myzset2 two
(integer) 1
127.0.0.1:6379> zrevrank myzset2 one
(integer) 0
Zrevrange
返回名称为key的zset(按score从大到小顺序)中的index从start到end的所有元素
127.0.0.1:6379> ZrevRANGE myzset2 0 -1 withscores
1) "one"
2) "3"
3) "two"
4) "1"
Zrangebyscore
返回集合中score在给定空间的元素
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "three"
4) "2"
5) "one"
6) "3"
127.0.0.1:6379> ZRANGEBYSCORE myzset2 2 3 withscores
1) "three"
2) "2"
3) "one"
4) "3"
Zcount
返回集合中score在给定区间的数量
127.0.0.1:6379> ZCOUNT myzset2 2 3
(integer) 2
127.0.0.1:6379> ZCOUNT myzset2 1 3
(integer) 3
Zcard
返回集合中元素个数
127.0.0.1:6379> ZCARD myzset2
(integer) 3
Zremrangebyrank
删除集合中排名在给定区间的元素
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "three"
4) "2"
5) "one"
6) "3"
7) "four"
8) "4"
9) "five"
10) "5"
127.0.0.1:6379> ZREMRANGEBYRANK myzset2 2 3 删除索引为2和3的元素
(integer) 2
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "three"
4) "2"
5) "five"
6) "5"
Zremrangebyscore
删除集合中score在给定区间的元素
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"
3) "three"
4) "2"
5) "five"
6) "5"
127.0.0.1:6379> ZREMRANGEBYSCORE myzset2 2 5 按顺序删除2到5的元素
(integer) 2
127.0.0.1:6379> ZRANGE myzset2 0 -1 withscores
1) "two"
2) "1"