1 缓存替代算法
cache algorithms (also frequently called cache replacement algorithms or cache replacement policies).
When the cache is full, the algorithm must choose which items to discard to make room for the new ones.
当缓存已满时, 缓存替代算法必须要选择要丢弃的数据项, 以便为新的数据项腾出空间.
wiki 上有很多 缓存替代算法 的实现, 参考这里.
2 Redis 内存驱逐策略(Eviction policies)
以下内容翻译自 Redis 官网的 Eviction policies 介绍.
驱逐策略定义了 在数据库超出内存限制时, Redis 所采用的处理方法.
驱逐策略有:
Policy | Description |
---|---|
noeviction | Returns an error if the memory limit has been reached when trying to insert more data(内存使用超过限制时, 插入新的数据返回会报错) |
allkeys-lru | Evicts the Least Recently Used keys out of all keys(对所有 key 进行驱逐, 删除 最近最少使用 的key) |
allkeys-lfu | Evicts the Least Frequently Used keys out of all keys(对所有 key 进行驱逐, 删除 最不常使用 的key) |
allkeys-random | Randomly evicts keys out of all keys(对所有 key 进行驱逐, 随机删除key) |
volatile-lru | Evicts the Least Recently Used keys out of all keys with an “expire” field set(对设置了过期时间的 key 进行驱逐, 删除 最近最少使用 的key) |
volatile-lfu | Evicts the Least Frequently Used keys out of all keys with an “expire” field set(对设置了过期时间的 key 进行驱逐, 删除 最不常使用 的key) |
volatile-random | Randomly evicts keys with an “expire” field set(对设置了过期时间的 key 进行驱逐, 随机删除key) |
volatile-ttl | Evicts the shortest time-to-live keys out of all keys with an “expire” field set.(对设置了过期时间的 key 进行驱逐, 删除存活时间最短的key, 越早过期的key越优先被删除) |
其中, volatile-lru
是默认的驱逐策略.