In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how the Key of Redis is addressed". In the daily operation, I believe that many people have doubts about how the Key of Redis is addressed. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how the Key of Redis is addressed". Next, please follow the editor to study!
When the Redis server is initialized, 16 databases are pre-allocated by default. Each of these databases is stored in a redisDb structure. There are two important parts in the structure of redisDb:
RedisDb.id: stores the numbers represented by integers in the redis database.
RedisDb.dict: stores all the key-value pair data in the library.
RedisDb.expires: saves the expiration time of each key.
For many databases in Redis, when we use select number to select databases, the program can switch databases directly through redisServer.db [number]. Sometimes when a program needs to know which database it is in, it can also read redisDb.id directly.
Redis's dictionary uses a hash table as its underlying implementation. The dict type uses two pointers to the hash table, of which the hash table No. 0 (ht [0]) is mainly used to store all the key values of the database, while the hash table No. 1 is mainly used for the program to rehash the hash table No. 0. Rehash is usually triggered when a new value is added, which will not be overstated here. So finding a key in redis is actually a lookup operation for ht [0] in the dict structure.
Since it is a hash, then we know that there will be hash collisions, so what if multiple keys hash for the same value? Redis uses a linked list to store multiple hash collided keys. That is, when the list is found based on the hash value of key, if the length of the list is greater than 1, then we need to traverse the linked list to find the key we are looking for. Of course, the length of the linked list is generally 1, so the time complexity can be regarded as o (1).
According to the above explanation, as well as official documentation and source code detoxification. We can conclude that the Key addressing of Redis consists of the following steps:
After getting a key, redis first determines whether the hash table 0 of the current library is empty, that is, if (dict- > ht [0] .size = = 0). Return NULL directly for true.
Determine whether the hash table 0 requires rehash, because if rehash is in progress, it is possible to store the key in both tables. If rehash is in progress, the _ dictRehashStep method is called once, and _ dictRehashStep is used to passively rehash the database dictionary, as well as the dictionary of the hash key.
The hash table is calculated, and the hash value is calculated according to the current dictionary and key.
The index value of the hash table is calculated based on the hash value and the current dictionary.
Take out the linked list in the hash table according to the index value, and traverse the linked list to find the location of the key. In general, the linked list is 1 in length.
When the ht [0] has been searched, the rehash judgment is made again. If it is not in the rehashing, it ends directly, otherwise, the 345th step is repeated for the ht [1].
If you are in Redis cluster mode, you need to determine which node the key is on.
At this point, the study on "how the Key of Redis is addressed" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.