当前位置:网站首页>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"

 

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229264026.html