当前位置:网站首页>Redis入门完整教程:集合详解
Redis入门完整教程:集合详解
2022-07-04 22:29:00 【谷哥学术】
集合(set)类型也是用来保存多个的字符串元素,但和列表类型不一
样的是,集合中不允许有重复元素,并且集合中的元素是无序的,不能通过
索引下标获取元素。如图2-22所示,集合user:1:follow包含
着"it"、"music"、"his"、"sports"四个元素,一个集合最多可以存储2 32 -1个元
素。Redis除了支持集合内的增删改查,同时还支持多个集合取交集、并
集、差集,合理地使用好集合类型,能在实际开发中解决很多实际问题。
2.5.1 命令
下面将按照集合内和集合间两个维度对集合的常用命令进行介绍。
1.集合内操作
(1)添加元素
sadd key element [element ...]
返回结果为添加成功的元素个数,例如:
127.0.0.1:6379> exists myset
(integer) 0
127.0.0.1:6379> sadd myset a b c
(integer) 3
127.0.0.1:6379> sadd myset a b
(integer) 0
(2)删除元素
srem key element [element ...]
返回结果为成功删除元素个数,例如:
127.0.0.1:6379> srem myset a b
(integer) 2
127.0.0.1:6379> srem myset hello
(integer) 0
(3)计算元素个数
scard key
scard的时间复杂度为O(1),它不会遍历集合所有元素,而是直接用
Redis内部的变量,例如:
127.0.0.1:6379> scard myset
(integer) 1
(4)判断元素是否在集合中
sismember key element
如果给定元素element在集合内返回1,反之返回0,例如:
127.0.0.1:6379> sismember myset c
(integer) 1
(5)随机从集合返回指定个数元素
srandmember key [count]
[count]是可选参数,如果不写默认为1,例如:
127.0.0.1:6379> srandmember myset 2
1) "a"
2) "c"
127.0.0.1:6379> srandmember myset
"d"
(6)从集合随机弹出元素
spop key
spop操作可以从集合中随机弹出一个元素,例如下面代码是一次spop
后,集合元素变为"d b a":
127.0.0.1:6379> spop myset
"c"
127.0.0.1:6379> smembers myset
1) "d"
2) "b"
3) "a"
需要注意的是Redis从3.2版本开始,spop也支持[count]参数。
srandmember和spop都是随机从集合选出元素,两者不同的是spop命令
执行后,元素会从集合中删除,而srandmember不会。
(7)获取所有元素
smembers key
下面代码获取集合myset所有元素,并且返回结果是无序的:
127.0.0.1:6379> smembers myset
1) "d"
2) "b"
3) "a"
smembers和lrange、hgetall都属于比较重的命令,如果元素过多存在阻
塞Redis的可能性,这时候可以使用sscan来完成,有关sscan命令2.7节会介
绍。
2.集合间操作
现在有两个集合,它们分别是user:1:follow和user:2:follow:
127.0.0.1:6379> sadd user:1:follow it music his sports
(integer) 4
127.0.0.1:6379> sadd user:2:follow it news ent sports
(integer) 4
(1)求多个集合的交集
sinter key [key ...]
例如下面代码是求user:1:follow和user:2:follow两个集合的交集,
返回结果是sports、it:
127.0.0.1:6379> sinter user:1:follow user:2:follow
1) "sports"
2) "it"
(2)求多个集合的并集
suinon key [key ...]
例如下面代码是求user:1:follow和user:2:follow两个集合的并集,
返回结果是sports、it、his、news、music、ent:
127.0.0.1:6379> sunion user:1:follow user:2:follow
1) "sports"
2) "it"
3) "his"
4) "news"
5) "music"
6) "ent"
(3)求多个集合的差集
sdiff key [key ...]
例如下面代码是求user:1:follow和user:2:follow两个集合的差集,
返回结果是music和his:
127.0.0.1:6379> sdiff user:1:follow user:2:follow
1) "music"
2) "his"
前面三个命令如图2-23所示。
(4)将交集、并集、差集的结果保存
sinterstore destination key [key ...]
suionstore destination key [key ...]
sdiffstore destination key [key ...]
集合间的运算在元素较多的情况下会比较耗时,所以Redis提供了上面
三个命令(原命令+store)将集合间交集、并集、差集的结果保存在
destination key中,例如下面操作将user:1:follow和user:2:follow两个集
合的交集结果保存在user:1_2:inter中,user:1_2:inter本身也是集合类
型:
127.0.0.1:6379> sinterstore user:1_2:inter user:1:follow user:2:follow
(integer) 2
127.0.0.1:6379> type user:1_2:inter
set
127.0.0.1:6379> smembers user:1_2:inter
1) "it"
2) "sports"
至此有关集合的命令基本已经介绍完了,表2-6给出集合常用命令的时
间复杂度,开发人员可以根据自身需求进行选择。
2.5.2 内部编码
集合类型的内部编码有两种:
·intset(整数集合):当集合中的元素都是整数且元素个数小于set-max-
intset-entries配置(默认512个)时,Redis会选用intset来作为集合的内部实
现,从而减少内存的使用。
·hashtable(哈希表):当集合类型无法满足intset的条件时,Redis会使
用hashtable作为集合的内部实现。
下面用示例来说明:
1)当元素个数较少且都为整数时,内部编码为intset:
127.0.0.1:6379> sadd setkey 1 2 3 4
(integer) 4
127.0.0.1:6379> object encoding setkey
"intset"
2.1)当元素个数超过512个,内部编码变为hashtable:
127.0.0.1:6379> sadd setkey 1 2 3 4 5 6 ... 512 513
(integer) 509
127.0.0.1:6379> scard setkey
(integer) 513
127.0.0.1:6379> object encoding listkey
"hashtable"
2.2)当某个元素不为整数时,内部编码也会变为hashtable:
127.0.0.1:6379> sadd setkey a
(integer) 1
127.0.0.1:6379> object encoding setkey
"hashtable"
边栏推荐
- Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
- Redis入门完整教程:慢查询分析
- 集群的概述与定义,一看就会
- The difference between Max and greatest in SQL
- Sword finger offer 67 Convert a string to an integer
- Redis入门完整教程:客户端通信协议
- It is said that software testing is very simple, but why are there so many dissuasions?
- Tla+ introductory tutorial (1): introduction to formal methods
- Attack and defense world misc advanced area Hong
- Attack and defense world misc master advanced zone 001 normal_ png
猜你喜欢
Business is too busy. Is there really no reason to have time for automation?
[roommate learned to use Bi report data processing in the time of King glory in one game]
LOGO special training camp section I identification logo and Logo Design Ideas
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Introducing QA into the software development lifecycle is the best practice that engineers should follow
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
【机器学习】手写数字识别
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
集群的概述与定义,一看就会
SPSS installation and activation tutorial (including network disk link)
随机推荐
Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
Unity-VScode-Emmylua配置报错解决
醒悟的日子,我是怎么一步一步走向软件测试的道路
Analysis of environmental encryption technology
模拟摇杆控制舵机
攻防世界 MISC 进阶区 Ditf
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Why is Dameng data called the "first share" of domestic databases?
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Detailed explanation of heap sort code
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
攻防世界 MISC 进阶区 hit-the-core
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
Sobel filter
[Lua] Int64 support
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
Logo special training camp section 1 Identification logo and logo design ideas
剑指 Offer 65. 不用加减乘除做加法
SQL中MAX与GREATEST的区别