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]> 
相关推荐
你的微笑,乱了夏天2 分钟前
linux centos 7 安装 mongodb7
数据库·mongodb
工业甲酰苯胺13 分钟前
分布式系统架构:服务容错
数据库·架构
独行soc1 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘
White_Mountain2 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
Code apprenticeship2 小时前
怎么利用Redis实现延时队列?
数据库·redis·缓存
百度智能云技术站2 小时前
广告投放系统成本降低 70%+,基于 Redis 容量型数据库 PegaDB 的方案设计和业务实践
数据库·redis·oracle
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
梦想平凡3 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
TianyaOAO3 小时前
mysql的事务控制和数据库的备份和恢复
数据库·mysql
Ewen Seong4 小时前
mysql系列5—Innodb的缓存
数据库·mysql·缓存