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]> 
相关推荐
瓜牛_gn43 分钟前
mysql特性
数据库·mysql
奶糖趣多多2 小时前
Redis知识点
数据库·redis·缓存
CoderIsArt3 小时前
Redis的三种模式:主从模式,哨兵与集群模式
数据库·redis·缓存
师太,答应老衲吧5 小时前
SQL实战训练之,力扣:2020. 无流量的帐户数(递归)
数据库·sql·leetcode
Channing Lewis6 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce
毕业设计制作和分享7 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis
ketil277 小时前
Redis - String 字符串
数据库·redis·缓存
Hsu_kk8 小时前
MySQL 批量删除海量数据的几种方法
数据库·mysql
编程学无止境8 小时前
第02章 MySQL环境搭建
数据库·mysql
knight-n8 小时前
MYSQL库的操作
数据库·mysql