当前位置:网站首页>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"
边栏推荐
- Attack and defense world misc advanced area Hong
- Redis: redis configuration file related configuration and redis persistence
- Wake up day, how do I step by step towards the road of software testing
- Explanation of bitwise operators
- Sobel filter
- 10 schemes to ensure interface data security
- 攻防世界 MISC 进阶区 hit-the-core
- NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
- 串口数据帧
- MYSQL架构——用户权限与管理
猜你喜欢
小程序vant tab组件解决文字过多显示不全的问题
Wake up day, how do I step by step towards the road of software testing
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
Locust performance test - environment construction and use
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
Redis入门完整教程:列表讲解
A complete tutorial for getting started with redis: hyperloglog
安装人大金仓数据库
[machine learning] handwritten digit recognition
随机推荐
Redis入门完整教程:哈希说明
The difference between Max and greatest in SQL
Redis introduction complete tutorial: slow query analysis
UML图记忆技巧
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
【机器学习】手写数字识别
Complete tutorial for getting started with redis: bitmaps
Sobel filter
Notepad++ -- editing skills
剑指 Offer 67. 把字符串转换成整数
Redis入门完整教程:发布订阅
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
Redis getting started complete tutorial: Geo
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Analysis of environmental encryption technology
MySQL Architecture - user rights and management
mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
攻防世界 misc 进阶区 2017_Dating_in_Singapore