当前位置:网站首页>Redis---1. Data structure characteristics and operation
Redis---1. Data structure characteristics and operation
2022-07-02 06:20:00 【Srwici】
Catalog
Redis
Redis: Full name Remote Dictionary Service, Remote Dictionary Service .
1. Redis Infrastructure
Redis All data structures of are based on a unique string key, And then by going to this key To get different data structures .
The basic data structure is divided into 5 Kind of :
- string: character string
- list: list
- hash: Hash
- set: aggregate
- Zset: Ordered set
1.1 string( character string )
1.1.1 Concept
Redis The string of is len( length ) and cap( Capacity ) The concept of ,len Is the actual length of the string ,cap Is the capacity of the string . When cap The size is less than 1m when , Every increase doubles , exceed 1m Will increase each time 1m,cap The maximum value of is 512m.
Redis The string adopts the method of pre allocating redundant space (cap > len), Avoid frequent memory allocation .
1.1.2 operation
String type operations
For a single string K-V Make changes ,get
、set
command
# key-value Addition, deletion and modification of
>set key value # Add and modify
OK
>get key # Inquire about value
"value"
>exiist key # Inquire about key
(integer) 1
>del key # Delete key
(integer) 1
>get key # The query cannot be found after deletion
(nil)
Batch key value pair setting acquisition
command mget
,mset
>mset name1 11111 name2 22222 name3 33333
OK
>mget name1 name2 name3
"11111"
"22222"
"33333"
expire
,key Expiration time setting and setex
、setnx
set The extension of the command
#key Expiration time settings
>set name "test"
OK
>get name
"test"
>expire name 5 # The unit is in seconds
#wait for 5s
>get name
(nil)
#set Expand
>setex name 5 "test" # Set the expiration time while setting
>setnx name "test" # Set up key, If it exists, the setting fails , If it does not exist, the setting is successful
Self increasing order incr
、incrby
, Used in value Is an integer . The scope of self increase is signed long Maximum and minimum of
>set name 5
OK
>get name
5
>incr name
>get name
6
>incrby name 5
>get name
11
>incrby name -5
>get name
6
bitmap: A string consists of bytes ,1 Bytes are 8 individual bit form , So a string has multiple bit form , This is it. bitmap data structure .
1.2 list( list )
1.2.1 Concept
Redis The list of is equivalent to Java Of LinkedList, It is a linked list rather than an array , The linked list feature is to add and delete quickly O(1). But finding a specific location is slow O(n).
When the last element pops up in the list , The data structure is automatically deleted , Memory is recycled .
List Data structures are often used as asynchronous queues , Serialize the structures that need to be processed later into strings and insert redis A list of , Another thread polls the data from the list for processing .
1.2.2 operation
rpush
and rpop
as well as lpop
Redis Both of them are based on rpush
To build . according to rpop
and lpop
Take data from different directions .rpop
Deposit first list Number of numbers , Like a queue , First in, first out ,lpop
Go first and deposit later list The data of , Similar stack , First in, then out .
> rpush books Math English Golang
(integer) 3
> llen books
(integer) 3
> rpop books
"Golang"
> lpop books
"Math"
lindex
, amount to Java In the middle of the table get(int index) operation , Need to traverse the list , Then take the value of the number .ltrim
, Within the reserved interval , Remove the , Get a new linked list .lrange
, Print the values in the interval
> rpush days Monday Tuesday Wednesday Thursday Friday Saturday Sunday # Make a new one rpush
(integer) 7
> llen days
(integer) 7
> lindex days 2 # Get the subscript as 2 Value
"Wednesday"
> lindex days 0 # The subscript is from 0 At the beginning
"Monday"
> llen days
(integer) 7
> lrange days 0 -1 #-1 Is the penultimate number ,-2 Is the penultimate number
1) "Monday"
2) "Tuesday"
3) "Wednesday"
4) "Thursday"
5) "Friday"
6) "Saturday"
7) "Sunday"
> llen days
(integer) 7
> ltrim days 1 -1 # Get the list from the first element to the last element
OK
> llen days # The original article 0 Bit Monday Remove
(integer) 6
> lrange days 0 -1 # Newly generated list 2
1) "Tuesday"
2) "Wednesday"
3) "Thursday"
4) "Friday"
5) "Saturday"
6) "Sunday"
> ltrim days 1 -2 # Get the newly generated list 2 The list from the first element to the last two elements is from Wednesday To Saturday
OK
> llen days
(integer) 4
> lrange days 0 -1 # Traverse the list , Found that the original
1) "Wednesday"
2) "Thursday"
3) "Friday"
4) "Saturday"
list Primary solution of bottom layer
When there are fewer list elements, use ziplist
, By compressing the elements and storing them in a continuous memory , When there are many elements, we use quicklist
, Through the linked list and ziplist
Combine , By putting more than one ziplist
Carry out two-way series , It avoids the problems of space waste and memory fragmentation of ordinary linked lists .
1.3 hash( Dictionaries )
1.3.1 Concept
Redis The dictionary in is equivalent to Java Medium HashMap,
The same thing : Array combined with linked list , disorder .
Difference :Redis in hash The value of can only store strings .Java Of rehash Operation is allin, All at once rehash,redis It's progressive rehash, In a progressive way rehash Will be in rehash Keep the old and new hash, Then query two , Only later will the old hash The content of is slowly synchronized to the new hash in , When removing the last old hash after , The original data structure will be deleted .
hash The advantages of , When storing, you can store different fields of an object separately , When storing strings, you need to store the whole object , On complex object access Consumption is less than string
For a single string ,hash The storage consumption of the structure will be higher than that of a single string .
1.3.2 operation
hget
、hset
、hgetall
、hmset
,hget
: Get a single key Corresponding value.hset
: Set up a k-v Key value pair .hgetall
: Get the key All under value.hmset
: Set up multiple k-v Key value pair .
> hset books java "first book"
1
> hset books golang "second book"
1
> hset books php "third book"
1
> hgetall books
1) "java"
2) "first book"
3) "golang"
4) "second book"
5) "php"
6) "third book"
> hlen book
0
> hget books java
"first book"
> hset books golang "second book" # There is no change before and after the update
0
> hget books golang
"second book"
> hmset books java "update first book" golang "update second book" php "update third book" # according to key Bulk changes value
OK
> hgetall books # according to key Batch acquisition value
1) "java"
2) "update first book"
3) "golang"
4) "update second book"
5) "php"
6) "update third book"
1.4 Set( aggregate )
1.4.1 Concept
Redus The set of is equivalent to Java Inside HashSet, Internal key value pairs are unordered and unique , That is, random de restocking , be-all value All are null
1.4.2 operation
sadd
、spop
、smembers
、sismember
、scard
sadd
: add to value Get into key aggregate .spop
: from key Take it out of the collection value.smembers
: Show all the elements in the collection .sismember
: Query whether there are elements in the collection .scard
: Count the total number of elements in the set .
> sadd member golang
(integer) 1
> sadd member golang
(integer) 0
> sadd member java php python
(integer) 3
> smembers member
1) "java"
2) "python"
3) "golang"
4) "php"
> sismember member java
(integer) 1
> sismember member javas
(integer) 0
> scard member
4
> spop member
"php"
> spop member
"golang"
> spop member
"java"
> spop member
"python"
> spop member
(nil)
1.5 ZSet( Ordered list )
1.5.1 Concept
ZSet There are two characteristics , One is internal value only , Second, every value All have their own score, Through this score You can sort . The same when value After being removed , The data structure will be deleted , Automatic memory recycling .
1.5.2 operation
zadd
: Additive elements zrange
: Output by sorting parameters zrevrange
: Output in reverse order by sorting parameters zcard
: Count k-v Number zscore
: Get specified k-v Of scorezrank
: Get the k-v Ranking zrankbyscore
: Traverse the output score In the interval value, Portability parameter withscores
Can output value and score.zrem
: Remove elements
> zadd items 9.99 "math" # zadd Additive elements
(integer) 1
> zadd items 21.11 "chinese"
(integer) 1
> zadd items 101.11 "MaxMoney"
(integer) 1
> zadd items 3033.11 "RMB"
(integer) 1
> zrange items 0 -1 # Traversal range value, Press score Sequential output
1) "math"
2) "chinese"
3) "MaxMoney"
4) "RMB"
> zrevrange items 0 -1 # Traversal range value, Press score Output in reverse order
1) "RMB"
2) "MaxMoney"
3) "chinese"
4) "math"
> zcard items # Calculation k-v The number of
4
> zscore items "RMB" # Output k-v Corresponding score
3033.11
> zrank items "RMB" # Output k-v Corresponding sorting , Starting from scratch , The order
3
> zrank items "math"
0
> zrangebyscore items 0 100 # Traverse score In the interval value, Output value
1) "math"
2) "chinese"
> zrangebyscore items 0 100 withscores # Traverse score In the interval value, Output value and score
1) "math"
2) 9.99
3) "chinese"
4) 21.11
> zrem items math # Remove elements
1
> zrangebyscore items 0 100 withscores # The result display after removing elements , For comparison , Exhibition zrem The role of
1) "chinese"
2) 21.11
1.6 Jump list
The jump watch is ZSet The internal data structure of the list , Its basic data structure is a common linked list , String all the elements .
Then select a higher-level node from these basic linked list nodes , Then thread these higher-level nodes with a linked list , this
As a result, some linked list nodes may have multiple roles , Even the nodes of the low-level linked list are also the nodes of the high-level list . When adding a new section
The probability of a point becoming a node with several functions is random . Two points decrease , The bottom layer, that is, the probability of wearing all elements, is 1, Every higher probability *0.5.
1.7 General rules of container data structure
Container data structure :
- list
- set
- zset
- hash
The rules 1:create if not exists
If the container does not exist , Then create a new data structure .
The rules 2:drop if no elements
If there are no elements in the container , Delete the element now , Free memory .
1.8 Expiration time
Redis Expiration time can be set for all data structures in , When the time comes, it will be automatically deleted k-v object , Instead of just deleting one value.
Learn from 《Redis Deep Adventure : Core principles and application practices 》
边栏推荐
- Flutter 混合开发: 开发一个简单的快速启动框架 | 开发者说·DTalk
- Web components series (VIII) -- custom component style settings
- 深入学习JVM底层(五):类加载机制
- I/o multiplexing & event driven yyds dry inventory
- 10 erreurs classiques de MySQL
- Deep learning classification network -- alexnet
- Scheme and implementation of automatic renewal of token expiration
- 浅谈三点建议为所有已经毕业和终将毕业的同学
- CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
- Shenji Bailian 3.54-dichotomy of dyeing judgment
猜你喜欢
Deep learning classification network -- Network in network
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
Classic literature reading -- deformable Detr
浏览器原理思维导图
来自读者们的 I/O 观后感|有奖征集获奖名单
Shenji Bailian 3.54-dichotomy of dyeing judgment
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
从设计交付到开发,轻松畅快高效率!
Sudo right raising
Detailed explanation of BGP message
随机推荐
深入学习JVM底层(三):垃圾回收器与内存分配策略
10 erreurs classiques de MySQL
Database learning summary 5
谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!
深入学习JVM底层(二):HotSpot虚拟机对象
Use of Arduino wire Library
Pbootcms collection and warehousing tutorial quick collection release
ROS create workspace
Golang--map扩容机制(含源码)
Sumo tutorial Hello World
Don't use the new WP collection. Don't use WordPress collection without update
The official zero foundation introduction jetpack compose Chinese course is coming!
注解和反射详解以及运用
Comment utiliser mitmproxy
On Web server
Browser principle mind map
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
VLAN experiment of switching technology
Deep learning classification network -- alexnet