preface

Then lastLinux, Docker installation and configurationSoon I’m here again. really soon. The main reason is that the first two chapters are relatively simple. It’s always easy to get started with programming, but it’s not that easy to really figure it out, right guys.

This article is also need you good brothers hands-on practice, good brother don’t lazy oh, after all, great oaks from little acorns grow, the truth I believe that good brothers understand it.

OKOK, flesh bullet impact, flush…

1 prepare

Redis provides five basic data structures, with different apis for each. Before operating its corresponding API, we need to understand the Redis global command, data structure and its single thread command execution mechanism, these for the real master of Redis behind the help is a lot of big, all good brothers really don’t lazy ah, haven’t installed Redis quickly look at my previous work, Install the uninstall according to my previous reinstall a, after all, good brothers installed is not necessarily right.

2 Global Commands

What? You don’t know what a global command is. Well, Redis is key-value based. Some commands are specific to a particular data structure, but some commands are universal to all data structures. Launch an attack….

2.1 Viewing All Keys

 keys *
Copy the code

2.2 Viewing the Total Number of Keys

 dbsize
Copy the code

It should be noted that dbsize is a variable that directly obtains the total number of keys built into Redis, so the time complexity of dbsize command is O (1). The keys command will iterate over all the keys, so its time complexity is O (n). If Redis saves a large number of keys, do not use this command.

2.3 Checking whether the Key exists

 exists key
Copy the code

2.4 Deleting Some Keys

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

2.5 Setting the Expiration Time

 ## Delete the key when the time is over, not immediately, but later
 expire key seconds
 ## Add a key value
 set test hello
 Set the expiration time for key test to 20 seconds
 expire test 20
Copy the code

2.5 Obtaining the data structure type of the key

 type key
 ## Retrieve the type of test
 type test
Copy the code

2.5 Obtaining the data structure type of the key

 type key
 ## Retrieve the type of test
 type test
Copy the code

3 Data Structure

I think it’s okay to put just one picture. I think any good brother would understand. They’re giants, after all.

Single threaded model

4.1 contrast

As mentioned above, Redis uses a single-threaded architecture and I/O multiplexing model to achieve high performance in-memory database. The interface like Http can be understood as a single-threaded interface. In this way, the request is sent to the server, and the client will block and wait for the server to return the result. Redis, on the other hand, executes commands in a queue. Good brothers look at pictures and talk.

4.2 Why is it so fast in a single thread

Oh, surprise. Here I go again. But with the first chapter said may be some different, or it is necessary to understand this loaded force, interview also afraid of what, the backhand is a good master (exaggerated, good brothers can not write proficient ah).

  1. Memory operation
  2. Based on non-blocking I/O, the implementation of I/O multiplexing using EPoll is basically event-driven. See Redis I/O multiplexing for details on epoll, Poll, and Select.
  3. Single threading avoids the consumption of races in multithreading. Think about the problem with multithreading. And how to solve it.

Finally, a picture, good brothers first understand, and then in detail.

That will do for this issue, there is not welcome good brothers comment area, additional attention and thumb up \ color # FF0000} {{attention and thumb up} attention and thumb up

Next :Redis string API, application scenario analysis last :Linux, Docker installation and configuration