Redis-数据类型-Set(不允许重复)

文章目录

1、查看redis是否启动

bash 复制代码
[root@localhost ~]# ps -ef | grep redis
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

bash 复制代码
[root@localhost ~]# /usr/local/redis/bin/redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

3、切换到2数据库

bash 复制代码
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会自动去重

bash 复制代码
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集合中所有的元素

bash 复制代码
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)

bash 复制代码
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不代表下标,它没有下标

bash 复制代码
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、从集合中删除元素

bash 复制代码
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"的缩写,用于求取多个集合的交集。

bash 复制代码
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、将指定的集合执行并集操作

bash 复制代码
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

bash 复制代码
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

bash 复制代码
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"的缩写,用于计算多个集合之间的差集。

bash 复制代码
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

bash 复制代码
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里面

bash 复制代码
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个(数据有可能重复出现)

注意:集合中的元素不会减少

bash 复制代码
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个(保证不会有重复数据出现)

bash 复制代码
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,说明数据遍历成功

bash 复制代码
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]> 
相关推荐
数据智能老司机8 小时前
CockroachDB权威指南——CockroachDB SQL
数据库·分布式·架构
数据智能老司机8 小时前
CockroachDB权威指南——开始使用
数据库·分布式·架构
松果猿9 小时前
空间数据库学习(二)—— PostgreSQL数据库的备份转储和导入恢复
数据库
Kagol9 小时前
macOS 和 Windows 操作系统下如何安装和启动 MySQL / Redis 数据库
redis·后端·mysql
无名之逆9 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
s9123601019 小时前
rust 同时处理多个异步任务
java·数据库·rust
数据智能老司机9 小时前
CockroachDB权威指南——CockroachDB 架构
数据库·分布式·架构
hzulwy9 小时前
Redis常用的数据结构及其使用场景
数据库·redis
程序猿熊跃晖10 小时前
解决 MyBatis-Plus 中 `update.setProcInsId(null)` 不生效的问题
数据库·tomcat·mybatis
ashane131411 小时前
Redis 哨兵集群(Sentinel)与 Cluster 集群对比
redis