redis的简单学习2.1-redis的数据类型

4.set类型以及操作

set是集合,它是string类型的无序集合 
set是通过hash table实现的, 
添加、删除和查找的复杂度都是0(1) 
对集合我们可以取交集、并集、差集 
通过这些操作我们可以实现SNS中的好友推荐和blogtag功能 

sadd

向名称为keyset中添加元素 
redis 127.0.0.1:6379> sadd myset "hello" 
(integer) 1 
redis 127.0.0.1:6379> sadd myset "world" 
(integer) 1 
redis 127.0.0.1:6379> sadd myset "world" 
(integer) 0 
redis 127.0.0.1:6379> smembers myset 
1) "world" 
2) "hello" 

smembers

获取名称为keyset中所有元素 
redis 127.0.0.1:6379> smembers myset 
1) "hello" 
1) "world" 

srem

删除名称为keyset中的元素 
redis 127.0.0.1:6379> sadd myset2 "one" 
(integer) 1 
redis 127.0.0.1:6379> sadd myset2 "two" 
(integer) 1 
redis 127.0.0.1:6379> srem myset2 "one" 
(integer) 1 

spop

随机返回并删除名称为keyset中一个元素 
redis 127.0.0.1:6379> sadd myset3 "one" 
(integer) 1 
redis 127.0.0.1:6379> sadd myset3 "two" 
(integer) 1 
redis 127.0.0.1:6379> spop myset3 
"two" 

sdiff

返回所有给定key与第一个key差集 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> sdiff myset2 myset3 
1) "three" 

sdiffstore

返回所有给定key与第一个key差集,并将结果存为另一个key 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> sdiffstore myset4 myset2 myset3 
(integer) 1 
redis 127.0.0.1:6379> smembers myset4 
1) "three" 

sinter

返回所有给定key交集 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> sinter myset2 myset3 
1) "two" 

sinterstore

返回所有给定key的交集,并将结果存为另一个key 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> sinterstore myset4 myset2 myset3 
(integer) 1 
redis 127.0.0.1:6379> smembers myset4 
1) "two"

sunion

返回所有给定key并集 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> sunion myset2 myset3 
1) "three" 
2) "one" 
3) "two"  

sunionstore

返回所有给定key的并集 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> sunionstore myset 4 myset2 myset3 
(integer) 3 
redis 127.0.0.1:6379> smembers myset4 
1) "three" 
2) "one" 
3) "two" 

smove

从第一个key对应的set中移除member并添加到第二个对应的set 
redis 127.0.0.1:6379> smembers myset2 
1) "three" 
2) "two" 
redis 127.0.0.1:6379> smembers myset3 
1) "three" 
2) "one" 
redis 127.0.0.1:6379> smove myset2 myset7 three 
(integer) 1 
redis 127.0.0.1:6379> smembers myset2 
1) "two" 
redis 127.0.0.1:6379> smembers myset7 
1) "three" 

scard

返回名称为keyset的元素个数 
redis 127.0.0.1:6379> scard myset2 
(integer) 1 

sismember

测试member是否是名称为keyset的元素 
redis 127.0.0.1:6379> smembers myset2 
1) "two" 
redis 127.0.0.1:6379> sismember myset2 two 
(integer) 1 
redis 127.0.0.1:6379> sismember myset2 one 
(integer) 0 

srandmember

随机返回名称为keyset的一个元素,但不删除元素 
redis 127.0.0.1:6379> smembers myset3 
1) "two" 
2) "one" 
redis 127.0.0.1:6379> srandmember myset3 
"two" 
redis 127.0.0.1:6379> srandmember myset3 
"one" 

5.sorted sets类型以及操作

sorted set set的一个升级版本,它在set的基础上增加了一个顺序属性 
这一属性在添加修改元素的时候可以指定 
每次指定后,zset会自动重新按新的值调整顺序 
可以理解为有两列的MySQL表,一列存value,一列存顺序,操作中key理解为zset的名字 

zadd

向名称为keyzset中添加元素memberscore用于排序,如果该元素存在,则更新其顺序 
redis 127.0.0.1:6379> zadd myzset 1 "one" 
(integer) 1 
redis 127.0.0.1:6379> zadd myzset 2 "two" 
(integer) 1 
redis 127.0.0.1:6379> zadd myzset 3 "two" // 
添加未成功,但是顺序号更新 
(integer) 0 
redis 127.0.0.1:6379> zrange myzset 0 -1 withscores //
withscores 输出顺序号 
1) "one" 
2) "1" 
3) "two" 
4) "3" 

zrem

删除名称为keyzset中的元素member 
redis 127.0.0.1:6379> zrange myzset 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "3" 
redis 127.0.0.1:6379> zrem myzset two 
(integer) 1 
redis 127.0.0.1:6379> zrange myzset 0 -1 withscores 
1) "one" 
2) "1" 

zincrby

如果再名称为keyzset中已经存在元素member,则该元素的score增加increment 
否则向该集合中添加该元素,其score的值为increment 
redis 127.0.0.1:6379> zadd myzset2 1 "one" 
(integer) 1 
redis 127.0.0.1:6379> zadd myzset2 2 "two" 
(integer) 1 
redis 127.0.0.1:6379> zincrby myzset2 2 "one" 
"3" 
redis 127.0.0.1:6379> zrange myzset2 0 -1 withscores 
1) "two" 
2) "2" 
3) "one" 
4) "3" 

zrank

返回名称为keyzsetmember元素的排名(按score从小到大排序)即下标 

redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "2" 
5) "three" 
6) "3" 
7) "five" 
8) "5" 
redis 127.0.0.1:6379> zrank myzset3 two 
(integer) 1 

zrevrank

返回名称为keyzsetmember元素的排名(按score从大到小排序)即下标 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "2" 
5) "three" 
6) "3" 
7) "five" 
8) "5" 
redis 127.0.0.1:6379> zrevrank myzset3 two 
(integer) 2

zrevrange 
返回名称为keyzset(按score从大到小顺序)中的indexstartend的所有元素 
redis 127.0.0.1:6379> zrevrange myzset3 0 -1 withscores 
1) "five" 
2) "5" 
3) "three" 
4) "3" 
5) "two" 
6) "2" 
7) "one" 
8) "1"  

zrangebyscore

返回集合中score在给定区间的元素 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "2" 
5) "three" 
6) "3" 
redis 127.0.0.1:6379> zrangebyscore myzset3 2 3 withscores 
1) "two" 
2) "2" 
3) "three" 
4) "3" 

zcount

返回集合中score在给定区间的数量 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "2" 
5) "three" 
6) "3" 
7) "five" 
8) "5" 
redis 127.0.0.1:6379> zcount myzset3 2 3 
(integer) 2 

zcard

返回所有元素个数 
redis 127.0.0.1:6379> zcard myzset3 
(integer) 4 

zremrangebyrank

删除集合中排名在给定区间的元素 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "2" 
redis 127.0.0.1:6379> zremrangebyrank myzset3 1 1  
(integer) 1 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 

zremrangebyscore

删除集合中score在给定区间的元素 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "one" 
2) "1" 
3) "two" 
4) "2" 
5) "three" 
6) "3" 
redis 127.0.0.1:6379> zremrangebyscore myzset3 1 2 
(integer) 2 
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores 
1) "three" 
2) "3" 

 


你可能感兴趣的:(redis,redis数据类型)