问题描述
有没有办法让 Redis 只为特定键选择 LRU(最近最少使用)驱逐策略?如果没有足够的内存,我希望一组键是持久的并且永远不会被驱逐.另一方面,如果内存不足,我希望可以自由驱逐另一组密钥.
Is there a way to make Redis choose a LRU (least recently used) eviction policy for only specific keys? I want a set of keys to be persistent and never be evicted if there's not enough memory. On the other hand, I want another set of keys to be freely evicted if there's low memory.
推荐答案
Redis 有一个驱逐政策,这可能对你的情况有好处.您可以将 maxmemory-policy 设置为 volatile-lru,这会导致 Redis:
Redis has an eviction policy which might be good for your case.You can set the maxmemory-policy to volatile-lru which causes Redis to:
使用 LRU 算法删除设置了过期时间的密钥
这意味着未设置 TTL 的键不是易失性的,因此不会被逐出,但具有 TTL 的键将按最近最少使用的顺序删除.
Which means that keys that are not set with TTL are not volatile, and therefor will not be evicted but keys that have TTL will be removed by Least-Recently-Used order.
实际上,volatile-lru 是默认策略,因此您要做的就是确保为您愿意在内存已满时丢失的密钥设置 TTL.
Actually, volatile-lru is the default policy, so all you have to do is to make sure TTL is set for the keys you are willing to lose when memory is getting full.
从 3.0 版开始,默认的驱逐策略是noeviction".(更改日志)
Since version 3.0 the default eviction policy is "noeviction". (changelog)
这篇关于如何让Redis只为部分键选择LRU驱逐策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!