Chapter 8 Redis database structure and read/write Principles, chapter 8 redis
Note: This article mainly references Redis Design and Implementation
1. Database Structure
The internal data structure of each redis Server is a redisDb []. The size of this array can be found in redis. configuration in conf ("database 16", default value: 16), and all our cache operations (set/hset/get) are a redisDb (database) in redisDb) the redisDb is redisDb [0] by default.
Note::
- You can use "select 1" to select the next operation on redisDb [1 ].
- In actual use, we only operate on redisDb [0], because
- Redis does not obtain the current redisDb operation function, so it is easy to select multiple times, we do not know which database, and since the operation is only performed on redisDb [0], "database" can be set to 1,
- After this parameter is set to 1, you can not only allocate the memory occupied by the original redisDb to redisDb [0], but also in the "Regular deletion" policy, we can scan only one redisDb.
"Regular deletion" see Chapter 9 Redis expiration Policy
2. read/write principles
In each redisDb, A dict (dictionary) is used to store "key-value ".
Example:
If the following four commands are executed in redis and no select is executed, the redisDb [0] operation is selected by default.
Set msg "hello nana"
Rpush mylist "a" "B" "c"
Hset book name "lover"
Hset book author "nana"
The storage structure is as follows:
3. Maintenance during read/write operations
After reading a key (The key must be read for both read and write operations,
- Server Update cache hits and missed hits
- Last time used to update the key
- Check whether the key has expired (For details, refer to Chapter 9 Redis Expiration Policy)
- Write counter + 1 for persistence