Redis database(How Redis represents a database and how database operations are implemented)
When the Redis server is initialized, redis.h/REDIS_DEFAULT_DBNUM (hereinafter abbreviated as N) databases will be created, and the database IDs are from 0 to N-1. All databases are saved in the redis.h/redisServer.db array. .
On the client side, you can switch through the "SELECT" command, where the program directly uses redis.h/redisServer.db[number] to switch. However, some internal programs, such as AOF programs, replication programs and RDB programs, need to know the number of the current database. If there is no id field, the program can only point to the currently used database and all databases in the redisServer.db array. Compare the pointers to figure out which database you are using.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
New:
Redis will add a key-value pair to the key space dictionary, where the key is a string and the value is any value type.
delete:
Redis will delete the key-value pair of the corresponding key in the key space dictionary
renew:
Redis will release the value object corresponding to the previous key in the key space dictionary and let the key point to the new value object
Inquire:
Redis will query the value object of the corresponding key in the key space dictionary:
The key does not exist, return NULL
The key exists and is of the correct type, returning the correct value
Key exists but is of incorrect type, return type is wrong
Other operations:
In addition to the key value operations shown above, there are many commands for the database itself, which are also completed by processing the key space:
FLUSHDB Delete all key-value pairs in the keyspace
RANDOMKEY Returns a random key from the key space
DBSIZE Returns the number of key-value pairs in the key space
Exists check whether the given key is existed in the key space
RENAME In the key space, rename the given key
In the Redis database, the expiration time of all keys is stored in the expires dictionary of the RedisDb structure. The key is a pointer to a key in the dict dictionary (key space), and the value is the modified expiration time. Use long long type representation.
Redis has four commands to set the key's survival time (how long it can survive) and expiration time (when it expires):
EXPIRE sets the key's lifetime in seconds;
PEXPIRE sets the key lifetime in milliseconds;
EXPIREAT sets the expiration UNIX timestamp of the key in seconds;
PEXPIREAT Sets the key's expiration UNIX timestamp in milliseconds.
Although there are so many different units and different forms of setting methods, the value of the expires dictionary only saves the "expired UNIX timestamp in milliseconds". That is to say, through conversion, the effects of all commands are finally the same. The effect is the same as the PEXPIREAT command.
定时清除:
在创建KEY的时候创建一个定时任务,在KEY到期时定时任务会被触发,第一时间清除过期KEY。
此种操作对内存最友好,不会有垃圾数据占用内存情况存在
缺点是会造成很大的服务器负载,特别是CPU负载高的时候,CPU很大一部分负载用在了删除不必要的KEY上了
惰性清除:
放任键空间的键不管,每次查询KEY的时候先去校验KEY是否过期,过期则删除,不过期则正常返回相应的VALUE。
此种操作对CPU最友好,这种策略仅限于当前KEY,相关不必要的KEY不会造成CPU负载
缺点是:容易造成内存空间浪费,特别是当系统中存在大量过期KEY且很少被用到,这十分影响非常依赖于内存大小Redis的性能
定期删除:
由定时脚本cron定时对expires的键扫描判断是否有过期的KEY存在,如存在,将其删除掉。
这是一种折中方案,既不会过多消耗CPU,又可以定时清楚惰性删除忽略到的不必要的内存消耗
Redis采用的“惰性清除”和“定期清楚”相结合的方式,其中定期删除模式是在规定的时间限制内,尽 可能地遍历各个数据库的 expires 字典,随机地检查一部分键的过期时间,并删除其中的过期键。
伪代码如下:
1 2 3 4 5 6 7 8 |
|
The above is the detailed content of Detailed explanation of the internal working mechanism of Redis. For more information, please follow other related articles on the PHP Chinese website!