当前位置:网站首页>Redis learning notes - data type: Set
Redis learning notes - data type: Set
2022-06-23 09:10:00 【Love Guoba】
List of articles
- Operations within a set
- 1. Additive elements
- 2. Get all the elements
- 3. Remove elements
- 4. Count the number of elements
- 5. Determines whether an element is in the set
- 6. Returns the specified number of elements at random from the collection
- 7. Pop up randomly from the collection ( Delete ) Elements ( You can specify the number )
- Assembly room operation
- Internal encoding
- Use scenarios
aggregate (set) Types are also used to hold multiple string elements , But unlike the list type , Duplicate elements are not allowed in the collection , And the elements in the set are unordered , Elements cannot be retrieved by index index .
Operations within a set
1. Additive elements
<pre>sadd key element [element ...]</pre>
add to key by “myset” Set , Returns the number of additions
127.0.0.1:6379> sadd myset a b c
(integer) 3
127.0.0.1:6379> sadd myset a b
(integer) 0
2. Get all the elements
smembers key
obtain key by “myset” All elements of the collection
127.0.0.1:6379> smembers myset
1) "c"
2) "a"
3) "b"
3. Remove elements
srem key element [element ...]
Delete key by “myset” A collection of “a” Elements
127.0.0.1:6379> srem myset a
(integer) 1
4. Count the number of elements
scard key
Calculation key by “myset” The number of elements in the set
127.0.0.1:6379> scard myset
(integer) 2
5. Determines whether an element is in the set
sismember key element
Judge key by “myset” The elements of the collection
127.0.0.1:6379> sismember myset b
(integer) 1
127.0.0.1:6379> sismember myset a
(integer) 0
6. Returns the specified number of elements at random from the collection
srandmember key count
Random from key by “myset” Collection return element
127.0.0.1:6379> srandmember myset 1
1) "b"
127.0.0.1:6379> srandmember myset 1
1) "b"
127.0.0.1:6379> srandmember myset 1
1) "c"
7. Pop up randomly from the collection ( Delete ) Elements ( You can specify the number )
spop key [count]
Random delete key by “myset” The elements in the collection
127.0.0.1:6379> smembers myset
1) "c"
2) "a"
3) "b"
127.0.0.1:6379> spop myset 1
1) "b"
127.0.0.1:6379> spop myset
"a"
127.0.0.1:6379> smembers myset
1) "c"
Assembly room operation
Add test data
127.0.0.1:6379> sadd act:1 My name is Tom
(integer) 4
127.0.0.1:6379> sadd act:2 It is Jerry
(integer) 3
1. Find the intersection of sets
sinter key [key...]
seek key by “act:1” and “act:2” Intersection
127.0.0.1:6379> sinter act:1 act:2
1) "is"
2. Find the union of sets
sunion key [key ...]
seek key by “act:1” and “act:2” Intersection
127.0.0.1:6379> sunion act:1 act:2
1) "name"
2) "is"
3) "Tom"
4) "Jerry"
5) "My"
6) "It"
3. Find the difference set of multiple sets
sdiff key [key ...]
seek key by “act:1” and “act:2” The difference between the set
127.0.0.1:6379> sdiff act:1 act:2
1) "Tom"
2) "My"
3) "name"
4. The intersection 、 Combine 、 Save the result of the difference set
sinterstore destination key [key ...]
sunionstore destination key [key ...]
sdiffstore destination key [key ...]
preservation key by “act:1” and “act:2” The intersection of is key name “act:1_2:inter”
127.0.0.1:6379> sinterstore act:1_2:inter act:1 act:2
(integer) 1
127.0.0.1:6379> smembers act:1_2:inter
1) "is"
Internal encoding
- intset( Set of integers ): When all the elements in the set are integers and the number of elements is less than set-max-intset-entries To configure ( Default 512 individual ) when ,Redis Will choose intset As an internal implementation of a collection , Thus reducing the use of memory .
- hashtable( Hashtable ): When the set type cannot satisfy intset The condition of ,Redis Will use hashtable As an internal implementation of a collection .
Use scenarios
- sadd=Tagging( label )
- spop/srandmember=Random item( Generate random number , For example, lottery )
- sadd+sinter=Social Graph( Social needs )
边栏推荐
- 自定义标签——jsp标签增强
- [qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
- Redis学习笔记—单个键管理
- Leetcode topic analysis 3sum closest
- 三层架构与SSM之间的对应关系
- Redis学习笔记—持久化机制之RDB
- [QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
- Intelligent operation and maintenance exploration | anomaly detection method in cloud system
- [learning resources] understand and love mathematics
- Combination sum III of leetcode topic analysis
猜你喜欢
随机推荐
Community article | mosn building subset optimization ideas sharing
36氪首发|云原生数据库公司「拓数派」完成新一轮战略融资,估值已达准独角兽级别
Typora设置图片上传服务
多线程初学
点击添加下拉框
Map接口的注意事项
[QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
Cookie和Session入门
瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目
自定义标签——jsp标签增强
Mqtt+flink to subscribe and publish real-time messages
扫码登录基本流程
玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
@Response
670. Maximum Swap
Utilisation du cookie du module de demande de noeud
Balls and cows of leetcode topic analysis
670. Maximum Swap
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME







