Redis learning and finishing

A redis principle

1.1 Data access

Processing diagram of fds

1.1.1 select

1. The select model directly copies all rset (fds) to the kernel mode every time , because the kernel mode is much faster than the user space mode.
2. If there is no data, the select function will block. If there is data: there will be the fds mark of the data, the select is not blocked, the whole fds is traversed, and the fd that finds the data reads the data for processing. The fd cannot be reused, and a new fds needs to be recreated every time and the fds in the user space is copied to the kernel mode.
Disadvantages:

  • Because fds maximum support 1024 Ge
  • Because fd is not reusable , in order to mark fd, a new rset must be created, which causes fds to be copied multiple times in the user mode memory state.
  • After copy, traverse again . Because after the fd in rset is set, the select function does not know which is set, it needs to traverse from the beginning to the end, and compare them one by one.

1.1.2 poll

  1. Solve the problems of select
    . Use linked list storage instead of bitmap, solve the 1024 length limit problem.
    Take the structure to set the revens field in the structure every time without destroying the fd itself, so it can be reused, and there is no need to create a new one every time. Fd.
  2. Disadvantages It
    also takes time to copy rset from user control mode to kernel mode. Although the execution of kernel mode is faster than user mode, copy also requires overhead
    O(n) to traverse the problem again. Because after the fd in the rset is set, the select function does not know which is set, it needs to traverse from the beginning to the end, and compare them one by one.

1.1.3 epoll

  1. The model of epoll is a red-black tree model
  2. Each time it is set, nfds will increase by one. And it will call back epoll_wait, so after epoll_wait is executed, it will return several fd with data, just for directly traverse nfds times.
nfds = epoll_wait(epfd, events, 5, 10000);
for(i=0;i<nfds;i++){
    
    
	memset(buffer, 0, MAXBUF);
	read(events[i].data.fd, buffer, MAXBUF);
	puts(buffer);
}

1.2 Endurance

RDB: Triggered by the bgsave command, and then the parent process executes a fork operation to create a child process, the child process creates an RDB file, and generates a temporary snapshot file based on the memory of the parent process, and atomically replaces the original file after completion (all data will be snapshotted at a time) Generate a copy and store it in the hard disk)
AOF: After opening, every time Redis executes a command to modify data, this command will be added to the AOF file

Two redis operations

2.1 redis data type

type of data
string
Hash
list
set

2.2 Redis commands

2.2.1 The basic syntax of redis client

./redis-cli

command Brief description description
OF THE DEL key This command is used to delete the key when it exists.
DUMP DUMP key Serialize the given key and return the serialized value.
EXISTS EXISTS key Check whether the given key exists.
EXPIRE EXPIRE key Set the expiration time for a given key, in seconds.
EXPIREAT EXPIREAT key The function of EXPIREAT is similar to EXPIRE, both of which are used to set the expiration time for the key. The difference is that the time parameter accepted by the EXPIREAT command is UNIX timestamp.
PEXPIRE PEXPIRE key Set the expiration time of the key in milliseconds.
PEXPIREAT PEXPIREAT key Set the timestamp of the key expiration time (unix timestamp) in milliseconds
KEYS KEYS pattern Find all keys that match a given pattern (pattern).
MOVE MOVE key db Move the key of the current database to the given database db.
PTTL PTTL key Return the remaining expiration time of the key in milliseconds.
TTL TTL key In seconds, return the remaining time to live (TTL, time to live) of the given key.
RANDOMKEY RANDOMKEY key A key is randomly returned from the current database.
RENAME RENAME key newkey Modify the name of the key.
RENAMENX RENAMENX key newkey Only when newkey does not exist, rename the key to newkey.
SCAN SCAN cursor [MATCH pattern] [COUNT count] Iterate over the database keys in the database.
TYPE TYPE key Returns the type of the value stored in the key.

Guess you like

Origin blog.csdn.net/m0_37111373/article/details/114064801