当前位置:网站首页>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"
边栏推荐
- [cooking record] - stir fried 1000 pieces of green pepper
- Redis入门完整教程:Pipeline
- 页面关闭前,如何发送一个可靠请求
- 攻防世界 MISC 进阶区 hit-the-core
- How to send a reliable request before closing the page
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- How to choose a securities company? Is it safe to open an account on your mobile phone
- Notepad++--编辑的技巧
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Business is too busy. Is there really no reason to have time for automation?
猜你喜欢

位运算符讲解

集群的概述与定义,一看就会

Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集

Explanation of bitwise operators

Complete tutorial for getting started with redis: bitmaps

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

攻防世界 MISC 进阶区 can_has_stdio?

One of the commonly used technical indicators, reading boll Bollinger line indicators

PMO: compare the sample efficiency of 25 molecular optimization methods

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
随机推荐
Redis入门完整教程:初识Redis
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
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
[cooking record] - stir fried 1000 pieces of green pepper
Solana chain application crema was shut down due to hacker attacks
常用技术指标之一文读懂BOLL布林线指标
Analysis of the self increasing and self decreasing of C language function parameters
Redis introduction complete tutorial: slow query analysis
Attack and defense world misc advanced area Hong
A complete tutorial for getting started with redis: transactions and Lua
Redis入门完整教程:Redis Shell
剑指 Offer 65. 不用加减乘除做加法
模拟摇杆控制舵机
【lua】int64的支持
Redis入门完整教程:键管理
华泰证券是国家认可的券商吗?开户安不安全?
图片懒加载的原理
Three stage operations in the attack and defense drill of the blue team
Google collab trample pit
Create Ca and issue certificate through go language