当前位置:网站首页>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 )
边栏推荐
- 力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
- Redis学习笔记—主从复制
- 523. Continuous Subarray Sum
- 986. Interval List Intersections
- 文件的打开新建与存储
- 学习SCI论文绘制技巧(F)
- 【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
- [QNX Hypervisor 2.2用户手册]6.1 使用QNX Hypervisor系统
- [qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
- 一元函数求极限三大方法---洛必达法则,泰勒公式
猜你喜欢
随机推荐
36 krypton launched | cloud native database company "tuoshupai" completed a new round of strategic financing, and the valuation has reached the level of quasi Unicorn
Servlet-02 生命周期
社区文章|MOSN 构建 Subset 优化思路分享
125. Valid Palindrome
一元函数求极限三大方法---洛必达法则,泰勒公式
Happy number of leetcode topic analysis
Combination sum III of leetcode topic analysis
栈(Stack)的链式实现详解----线性结构
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
[qnx hypervisor 2.2 user manual]6.2 network
Detailed explanation of srl16e in xilinxffpga
Community article | mosn building subset optimization ideas sharing
The results of CDN node and source station are inconsistent
RGB与CMYK颜色模式
How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
Unity grid programming 08
"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems
类型从属名称的使用必须以“typename”为前缀
Custom tag - JSP tag Foundation
Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“






