1. knowledge required in advance
1. Every database in redis is stored in a redisDb structure. Here, redisDb. id stores the numbers represented by integers in the redis database. RedisDb. dict stores all key-value pairs of the database. RedisDb. expires stores the expiration time of each key.
2. When the redis server is initialized, 16 databases will be allocated in advance (this quantity can be configured through the configuration file), and all databases will be saved to a redisServer member redisServer. db array. When selecting database select number, the program directly switches the database through redisServer. db [number. Sometimes, when the program needs to know which database it is in, it can directly read redisDb. id.
3. Since we know that all the key values of a database are stored in redisDb. dict, we need to know the structure of dict if the key is located:
Copy codeThe Code is as follows: typedef struct dict {
// Type-specific processing functions
DictType * type;
// Private Data of the type processing function
Void * privdata;
// Hash table (2)
Dictht ht [2];
// Indicates the rehash progress record. The value-1 indicates that rehash is not performed.
Int rehashidx;
// Number of currently running security iterators
Int iterators;
} Dict;
From the above structure, we can see that the redis dictionary uses a hash table as its underlying implementation. The dict type uses two pointers to the hash table. The hash table No. 0 (ht [0]) is mainly used to store all the key values of the database, the hash table No. 1 is mainly used by the program to rehash the hash table No. 0. rehash is generally triggered when a new value is added, so we will not repeat it here. Therefore, redis searches for a key, which is to find the ht [0] In the dict structure.
4. Since it is a hash, we know that there will be a hash collision. What should we do if the hash of multiple keys is the same value? Redis uses a linked list to store multiple hash collision keys. That is to say, after finding the list based on the hash value of the key, if the list length is greater than 1, we need to traverse the linked list to find the key we are looking. Of course, in general, the chain table length is 1, so the time complexity can be considered as o (1 ).
2. When redis obtains a key, if the location of the key is found.
After learning the above knowledge, we can analyze redis if a key is found in the memory.
1. When a key is obtained, redis first checks whether the hash table No. 0 of the current database is empty, that is, if (dict-> ht [0]. size = 0 ). If it is true, NULL is directly returned.
2. Determine whether rehash is required for the hash table No. 0. If rehash is performed, the two tables may store the key. If rehash is in progress, the _ dictRehashStep method will be called once. The _ dictRehashStep method is used to passively rehash the dictionary of the database dictionary and hash key, which is not described here.
3. Calculate the hash table and calculate the hash value based on the current dictionary and key.
4. Calculate the index value of the hash table based on the hash value and the current dictionary.
5. Retrieve the linked list based on the index value in the hash table and traverse the linked list to find the key position. Generally, the length of the linked list is 1.
6. After ht [0] is searched, rehash is performed again. If it is not found in rehashing, it ends directly. Otherwise, repeat Step 345 for ht [1.
So far, we can find the key in the memory.