编程知识 cdmana.com

Data structure, thread model and global command of reids

Preface

Then the first one Linux、Docker Lower installation and configuration , Soon I came back , It's really fast . The first two articles are relatively simple . The beginning is always easy for programming , Really understand, but not so simple , Right, good guys .
This article also needs your hands-on practice , Good brother, don't be lazy , After all, thousands of tall buildings rise from the ground , I believe all good brothers know the truth .
OKOK, Meat bomb impact , To rush ...
 impact

1 Get ready

Redis Provides five basic data structures , For the provided API It's different . In the operation of its corresponding API Before , We need to understand Redis Global command 、 Data structure and its single thread execution mechanism , These are for the real master of the back Redis It's a lot of help , All good brothers really don't be lazy , Not installed yet Redis Let's have a look at my first part , The installation of the re - uninstall, according to my first article to re - install a , After all, good guys don't have to be right .
 Hit him

2 Global command

what , You don't know what global command is yet . All right ,Redis It's based on key value storage , Some of these commands are for a data structure , But some commands are applicable to all data structures , It's basically used on keys , These commands are global commands . attack ....

2.1 Look at all the keys

 keys *

2.2 Check the total number of keys

 dbsize
It should be noted that dbsize It 's direct access Redis Built in key count variable , therefore dbsize The time complexity of the command is O(1). and keys The command will traverse There are keys , So its time complexity is O(n), When Redis When a large number of keys are saved , Good production environment, don't use it indiscriminately , It's supposed to block this command , Blocking for Redis It's a nightmare .

2.3 Check key Whether there is

 exists key

2.4 Delete some keys

 ##  One is also some 
 del key [key ...]
 ##  Example 
 del a b c

2.5 Set expiration time

 ##  The key will be deleted after time , Of course not immediately delete , Let's talk about it later 
 expire key seconds
 ##  Add a key value 
 set test hello
 ##  to key by test To set the expiration time to 20 second 
 expire test 20

2.5 Gets the data structure type of the key

 type key
 ##  obtain test The type of , The normal return should be a string 
 type test

2.5 Gets the data structure type of the key

 type key
 ##  obtain test The type of , The normal return should be a string 
 type test

3 data structure

Just one picture should be OK . Good guys should be able to understand , After all, they're all giants .

 data structure

4 Single thread model

4.1 contrast

It was mentioned earlier that Redis Single thread architecture and I/O Multiplexing model to achieve high performance in memory database , As we usually write Http The interface can be understood as a single thread , In this way, the request is sent to the server , The client will block and wait for the server to return the result . and Redis The commands are placed in a queue and executed sequentially . Good brothers, look at the picture and talk .
 contrast

4.2 Why is it so fast under single thread

I didn't think! , I took this out again . But it may be different from the first chapter , It's necessary , To understand this pretense 、 I'm afraid of the interview , Backhand is a master, OK ( Exaggerated , Good brother, don't write proficient ).

  1. Memory operations
  2. Based on non blocking I/O, Use epoll As I/O The realization of multiplexing technology , It's basically event driven . About epoll、poll、select You can see Redis I/O Multiplexing , It will be explained in detail in the future .
  3. Single thread avoids the consumption of race state under multi thread , Good guys, think about what's wrong with multithreading . And how to solve it .

Finally, one more picture , Good guys, let's get to know , I'll talk about it in detail later .
redis  Execution process

That's all for this issue , If there is something wrong, please leave a message in the comments area , in addition $\color{#FF0000}{ Please pay attention to 、 Please thumb up }$

Next :Redis character string API、 Use scenario analysis
Last one :Linux、Docker Lower installation and configuration

版权声明
本文为[]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224140109313i.html

Scroll to Top