文章目录
1、查看redis是否启动 2、通过客户端连接redis 3、切换到2数据库 4、给key指定的set集合中存入数据,set会自动去重 5、返回可以指定的set集合中所有的元素 6、返回集合中元素的数量(set cardinality) 7、检查当前指定member是否是集合中的元素 8、从集合中删除元素 9、将指定的集合进行交集操作(Set Intersection) 10、将指定的集合执行并集操作 11、取交集后存入destination 12、取并集后存入destination 13、将指定的集合执行差集操作(Set Difference) 14、将指定的集合执行差集操作,并存储到destination 15、把member从source移动到destination 16、从集合中随机返回count个数量的元素,count不指定就返回1个(数据有可能重复出现) 17、从集合中随机弹出count个数量的元素,count不指定就弹出1个(保证不会有重复数据出现) 18、基于游标的遍历。cursor是游标值,第一次显示第一块内容时,游标取值为0;根据后续返回的新的游标值获取下一块数据。直到游标值变成0,说明数据遍历成功
1、查看redis是否启动
[ root@localhost ~]
root 4270 1 0 05:51 ? 00:00:21 /usr/local/redis/bin/redis-server *:6379
root 5458 5407 0 07:33 pts/1 00:00:00 /usr/local/redis/bin/redis-cli -p 6379
root 6914 6858 0 09:44 pts/0 00:00:00 grep --color = auto redis
[ root@localhost ~]
2、通过客户端连接redis
[ root@localhost ~]
127.0 .0.1:6379 > ping
PONG
127.0 .0.1:6379 >
3、切换到2数据库
127.0 .0.1:6379 > select 2
OK
127.0 .0.1:6379[ 2 ] > keys *
( empty array)
127.0 .0.1:6379[ 2 ] >
4、给key指定的set集合中存入数据,set会自动去重
127.0 .0.1:6379[ 2 ] > sadd k1 jim lucy tom jim lucy
( integer) 3
127.0 .0.1:6379[ 2 ] > keys *
1 ) "k1"
127.0 .0.1:6379[ 2 ] >
5、返回可以指定的set集合中所有的元素
127.0 .0.1:6379[ 2 ] > keys *
1 ) "k1"
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "lucy"
2 ) "tom"
3 ) "jim"
127.0 .0.1:6379[ 2 ] >
6、返回集合中元素的数量(set cardinality)
127.0 .0.1:6379[ 2 ] > keys *
1 ) "k1"
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "lucy"
2 ) "tom"
3 ) "jim"
127.0 .0.1:6379[ 2 ] > scard k1
( integer) 3
127.0 .0.1:6379[ 2 ] >
7、检查当前指定member是否是集合中的元素
返回1:表示是集合中得元素
返回0:表示不是集合中得元素
0和1不代表下标,它没有下标
127.0 .0.1:6379[ 2 ] > keys *
1 ) "k1"
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "lucy"
2 ) "tom"
3 ) "jim"
127.0 .0.1:6379[ 2 ] > sismember k1 rose
( integer) 0
127.0 .0.1:6379[ 2 ] > sismember k1 lucy
( integer) 1
127.0 .0.1:6379[ 2 ] > sismember k1 jim
( integer) 1
127.0 .0.1:6379[ 2 ] >
8、从集合中删除元素
127.0 .0.1:6379[ 2 ] > keys *
1 ) "k1"
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "lucy"
2 ) "tom"
3 ) "jim"
127.0 .0.1:6379[ 2 ] > srem k1 lucy rose
( integer) 1
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "jim"
127.0 .0.1:6379[ 2 ] >
9、将指定的集合进行交集操作(Set Intersection)
sinter可以理解为它是“Set Intersection”的缩写,用于求取多个集合的交集。
127.0 .0.1:6379[ 2 ] > sadd k1 a b c d
( integer) 4
127.0 .0.1:6379[ 2 ] > sadd k2 b d e f g
( integer) 5
127.0 .0.1:6379[ 2 ] > sinter k1 k2
1 ) "d"
2 ) "b"
127.0 .0.1:6379[ 2 ] >
10、将指定的集合执行并集操作
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
6 ) "c"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "d"
3 ) "f"
4 ) "b"
5 ) "g"
127.0 .0.1:6379[ 2 ] > sunion k1 k2
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "f"
6 ) "c"
7 ) "b"
8 ) "e"
9 ) "g"
127.0 .0.1:6379[ 2 ] >
11、取交集后存入destination
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
6 ) "c"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "d"
3 ) "f"
4 ) "b"
5 ) "g"
127.0 .0.1:6379[ 2 ] > sinter k1 k2
1 ) "d"
2 ) "b"
127.0 .0.1:6379[ 2 ] > sinterstore k3 k1 k2
( integer) 2
127.0 .0.1:6379[ 2 ] > smembers k3
1 ) "d"
2 ) "b"
127.0 .0.1:6379[ 2 ] >
12、取并集后存入destination
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
6 ) "c"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "d"
3 ) "f"
4 ) "b"
5 ) "g"
127.0 .0.1:6379[ 2 ] > sunion k1 k2
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "f"
6 ) "c"
7 ) "b"
8 ) "e"
9 ) "g"
127.0 .0.1:6379[ 2 ] > sunionstore k4 k1 k2
( integer) 9
127.0 .0.1:6379[ 2 ] > smembers k4
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "f"
6 ) "c"
7 ) "b"
8 ) "e"
9 ) "g"
127.0 .0.1:6379[ 2 ] >
13、将指定的集合执行差集操作(Set Difference)
在Redis中,sdiff的英文全称可以理解为“Set Difference”的缩写,用于计算多个集合之间的差集。
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
6 ) "c"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "d"
3 ) "f"
4 ) "b"
5 ) "g"
127.0 .0.1:6379[ 2 ] > sdiff k1 k2
1 ) "a"
2 ) "tom"
3 ) "c"
4 ) "jim"
127.0 .0.1:6379[ 2 ] >
14、将指定的集合执行差集操作,并存储到destination
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
6 ) "c"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "d"
3 ) "f"
4 ) "b"
5 ) "g"
127.0 .0.1:6379[ 2 ] > sdiff k1 k2
1 ) "a"
2 ) "tom"
3 ) "c"
4 ) "jim"
127.0 .0.1:6379[ 2 ] > sdiffstore k5 k1 k2
( integer) 4
127.0 .0.1:6379[ 2 ] > smembers k5
1 ) "a"
2 ) "tom"
3 ) "c"
4 ) "jim"
127.0 .0.1:6379[ 2 ] >
15、把member从source移动到destination
把k1中的c移动到k2里面
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
6 ) "c"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "g"
3 ) "d"
4 ) "f"
5 ) "b"
127.0 .0.1:6379[ 2 ] > smove k1 k2 c
( integer) 1
127.0 .0.1:6379[ 2 ] > smembers k1
1 ) "tom"
2 ) "a"
3 ) "jim"
4 ) "d"
5 ) "b"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "g"
3 ) "d"
4 ) "f"
5 ) "c"
6 ) "b"
127.0 .0.1:6379[ 2 ] >
16、从集合中随机返回count个数量的元素,count不指定就返回1个(数据有可能重复出现)
注意:集合中的元素不会减少
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "g"
3 ) "d"
4 ) "f"
5 ) "c"
6 ) "b"
127.0 .0.1:6379[ 2 ] > srandmember k2
"c"
127.0 .0.1:6379[ 2 ] > srandmember k2
"g"
127.0 .0.1:6379[ 2 ] > srandmember k2
"d"
127.0 .0.1:6379[ 2 ] > srandmember k2 5
1 ) "e"
2 ) "g"
3 ) "d"
4 ) "b"
5 ) "c"
127.0 .0.1:6379[ 2 ] >
17、从集合中随机弹出count个数量的元素,count不指定就弹出1个(保证不会有重复数据出现)
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
2 ) "g"
3 ) "d"
4 ) "f"
5 ) "c"
6 ) "b"
127.0 .0.1:6379[ 2 ] > spop k2 5
1 ) "g"
2 ) "d"
3 ) "f"
4 ) "c"
5 ) "b"
127.0 .0.1:6379[ 2 ] > smembers k2
1 ) "e"
127.0 .0.1:6379[ 2 ] >
18、基于游标的遍历。cursor是游标值,第一次显示第一块内容时,游标取值为0;根据后续返回的新的游标值获取下一块数据。直到游标值变成0,说明数据遍历成功
127.0 .0.1:6379[ 2 ] > keys *
1 ) "k1"
2 ) "k5"
3 ) "k3"
4 ) "k2"
5 ) "k4"
127.0 .0.1:6379[ 2 ] > sadd k6 a b c d e f g h i j k l m n o p q r s t u v w x y z
( integer) 26
127.0 .0.1:6379[ 2 ] > sscan k6 0
1 ) "26"
2 ) 1 ) "s"
2 ) "e"
3 ) "n"
4 ) "m"
5 ) "h"
6 ) "d"
7 ) "y"
8 ) "i"
9 ) "g"
10 ) "a"
11 ) "o"
127.0 .0.1:6379[ 2 ] > sscan k6 26
1 ) "13"
2 ) 1 ) "j"
2 ) "u"
3 ) "t"
4 ) "q"
5 ) "r"
6 ) "x"
7 ) "w"
8 ) "l"
9 ) "p"
10 ) "z"
127.0 .0.1:6379[ 2 ] > sscan k6 13
1 ) "0"
2 ) 1 ) "f"
2 ) "v"
3 ) "b"
4 ) "k"
5 ) "c"
127.0 .0.1:6379[ 2 ] >