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]> 
相关推荐
soulteary2 分钟前
突破内存限制:Mac Mini M2 服务器化实践指南
运维·服务器·redis·macos·arm·pika
gma99927 分钟前
Etcd 框架
数据库·etcd
爱吃青椒不爱吃西红柿‍️30 分钟前
华为ASP与CSP是什么?
服务器·前端·数据库
Yz98761 小时前
hive的存储格式
大数据·数据库·数据仓库·hive·hadoop·数据库开发
wkj0011 小时前
php操作redis
开发语言·redis·php
苏-言1 小时前
Spring IOC实战指南:从零到一的构建过程
java·数据库·spring
Ljw...1 小时前
索引(MySQL)
数据库·mysql·索引
菠萝咕噜肉i2 小时前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
长风清留扬2 小时前
一篇文章了解何为 “大数据治理“ 理论与实践
大数据·数据库·面试·数据治理
Mephisto.java2 小时前
【大数据学习 | Spark】Spark的改变分区的算子
大数据·elasticsearch·oracle·spark·kafka·memcache