当前位置:网站首页>Redis introduction complete tutorial: Collection details
Redis introduction complete tutorial: Collection details
2022-07-04 22:59:00 【Gu Ge academic】
aggregate (set) Types are also used to hold multiple string elements , But it's not the same as the list type
What it looks like is , Duplicate elements are not allowed in the collection , And the elements in the set are unordered , Cannot pass
Index subscript get element . Pictured 2-22 Shown , aggregate user:1:follow contain
the "it"、"music"、"his"、"sports" Four elements , A collection can store at most 2 32 -1 Yuan
plain .Redis In addition to support the addition, deletion, modification and query within the collection , At the same time, it also supports the intersection of multiple sets 、 and
Set 、 Difference set , Reasonable use of set types , Can solve many practical problems in actual development .
2.5.1 command
Next, we will introduce the common commands of the collection according to the two dimensions within and between the collection .
1. Operations within a set
(1) Additive elements
sadd key element [element ...]
The return result is the number of elements added successfully , for example :
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) Remove elements
srem key element [element ...]
The return result is the number of successfully deleted elements , for example :
127.0.0.1:6379> srem myset a b
(integer) 2
127.0.0.1:6379> srem myset hello
(integer) 0
(3) Count the number of elements
scard key
scard The time complexity of is O(1), It doesn't traverse all the elements of the collection , It's direct use
Redis Internal variables , for example :
127.0.0.1:6379> scard myset
(integer) 1
(4) Determines whether an element is in the set
sismember key element
If you give an element element Returns... Within the collection 1, Instead, return to 0, for example :
127.0.0.1:6379> sismember myset c
(integer) 1
(5) Returns the specified number of elements at random from the collection
srandmember key [count]
[count] Is an optional parameter , If it is not written, the default is 1, for example :
127.0.0.1:6379> srandmember myset 2
1) "a"
2) "c"
127.0.0.1:6379> srandmember myset
"d"
(6) Randomly eject elements from the set
spop key
spop Operation can pop up an element randomly from the collection , For example, the following code is a spop
after , The set element becomes "d b a":
127.0.0.1:6379> spop myset
"c"
127.0.0.1:6379> smembers myset
1) "d"
2) "b"
3) "a"
It should be noted that Redis from 3.2 Version start ,spop Also support [count] Parameters .
srandmember and spop All randomly selected elements from the set , The difference between the two is spop command
After execution , Elements are removed from the collection , and srandmember Can't .
(7) Get all the elements
smembers key
The following code gets the collection myset All the elements , And the return result is unordered :
127.0.0.1:6379> smembers myset
1) "d"
2) "b"
3) "a"
smembers and lrange、hgetall They're all heavy orders , If there are too many elements, there is resistance
stopper Redis The possibility of , You can use it sscan To complete , of sscan command 2.7 Jiehuijie
Shao .
2. Assembly room operation
Now there are two sets , They are user:1:follow and 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) Find the intersection of sets
sinter key [key ...]
For example, the following code is for user:1:follow and user:2:follow The intersection of two sets ,
The return is sports、it:
127.0.0.1:6379> sinter user:1:follow user:2:follow
1) "sports"
2) "it"
(2) Find the union of sets
suinon key [key ...]
For example, the following code is for user:1:follow and user:2:follow The union of two sets ,
The return is 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) Find the difference set of multiple sets
sdiff key [key ...]
For example, the following code is for user:1:follow and user:2:follow The difference set of two sets ,
The return is music and his:
127.0.0.1:6379> sdiff user:1:follow user:2:follow
1) "music"
2) "his"
The first three commands are shown in the figure 2-23 Shown .
(4) The intersection 、 Combine 、 Save the result of the difference set
sinterstore destination key [key ...]
suionstore destination key [key ...]
sdiffstore destination key [key ...]
The operation between sets is time-consuming when there are many elements , therefore Redis Provided above
Three commands ( The original order +store) Intersection of sets 、 Combine 、 The result of the difference set is stored in
destination key in , For example, the following operation will user:1:follow and user:2:follow Two sets
The intersection result of the combination is saved in user:1_2:inter in ,user:1_2:inter It's also a collection class
type :
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"
So far, the command about collection has been introduced , surface 2-6 When giving a set of commonly used commands
The complexity , Developers can choose according to their own needs .
2.5.2 Internal encoding
There are two types of internal encoding for collection types :
·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 the inner reality of the set
present , Thus reducing the use of memory .
·hashtable( Hashtable ): When the set type cannot satisfy intset The condition of ,Redis Can make
use hashtable As an internal implementation of a collection .
Here is an example to illustrate :
1) When the number of elements is small and all are integers , The internal code is 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) When the number of elements exceeds 512 individual , The internal code becomes 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) When an element is not an integer , The internal code will also become hashtable:
127.0.0.1:6379> sadd setkey a
(integer) 1
127.0.0.1:6379> object encoding setkey
"hashtable"
边栏推荐
- Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
- Redis入门完整教程:集合详解
- Locust performance test - environment construction and use
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Attack and defense world misc master advanced zone 001 normal_ png
- [the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
- Redis入门完整教程:事务与Lua
- 页面关闭前,如何发送一个可靠请求
- Gnawing down the big bone - sorting (II)
- Attack and defense world misc advanced area ditf
猜你喜欢
Close system call analysis - Performance Optimization
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Redis入门完整教程:事务与Lua
How to send a reliable request before closing the page
Attack and defense world misc advanced area can_ has_ stdio?
sobel过滤器
攻防世界 MISC 进阶区 hit-the-core
Redis入门完整教程:键管理
随机推荐
Advanced area a of attack and defense world misc Masters_ good_ idea
Redis入门完整教程:哈希说明
How to send a reliable request before closing the page
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
[OpenGL] note 29 anti aliasing (MSAA)
A complete tutorial for getting started with redis: transactions and Lua
Notepad++--编辑的技巧
Redis getting started complete tutorial: Geo
Editplus-- usage -- shortcut key / configuration / background color / font size
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
Complete tutorial for getting started with redis: bitmaps
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
安装人大金仓数据库
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Async await used in map
【剑指offer】1-5题
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Serial port data frame
Common methods in string class
攻防世界 misc 高手进阶区 a_good_idea