当前位置:网站首页>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"
边栏推荐
- NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
- Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
- [try to hack] wide byte injection
- Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
- How to choose a securities company? Is it safe to open an account on your mobile phone
- Notepad++--编辑的技巧
- Redis入门完整教程:慢查询分析
- On-off and on-off of quality system construction
- Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
- Photoshop批量给不同的图片添加不同的编号
猜你喜欢
Tla+ introductory tutorial (1): introduction to formal methods
常用技术指标之一文读懂BOLL布林线指标
Talk about Middleware
Lost in the lock world of MySQL
On-off and on-off of quality system construction
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Close system call analysis - Performance Optimization
串口数据帧
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Redis入门完整教程:集合详解
随机推荐
【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
ECS settings SSH key login
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
How can enterprises cross the digital divide? In cloud native 2.0
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Mongodb aggregation operation summary
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
Redis入门完整教程:集合详解
Redis: redis configuration file related configuration and redis persistence
Sobel filter
Unity vscode emmylua configuration error resolution
常用技术指标之一文读懂BOLL布林线指标
攻防世界 MISC 进阶区 can_has_stdio?
Redis入门完整教程:事务与Lua
Attack and defense world misc advanced area can_ has_ stdio?
攻防世界 misc 高手进阶区 a_good_idea
Complete tutorial for getting started with redis: bitmaps
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
Redis入门完整教程:Redis使用场景
Lost in the lock world of MySQL