数据淘汰机制
为了防止内存溢出,可采取以下方法:
在redis中,允许用户设置最大使用内存大小
单台redis服务器,最大内存使用不应超过20G(建议)
maxmemory 512G
给key设置过期时间
给maxmemory-policy设置相应的数据淘汰策略
# LRU means Least Recently Used
LRU意思是:最近最少使用。判断最近被使用的时间,目前最远的数据优先被淘汰
# LFU means Least Frequently Used
LFU意思是:最不经常使用。在一段时间内,数据被使用次数最少的,优先被淘汰
# volatile-lru -> Evict using approximated LRU among the keys with an expire set.
从已设置过期时间的数据集中挑选最近最少使用的数据淘汰
# volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
从已设置过期时间的数据集中,删除一段时间内使用次数最少的数据
# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
从已设置过期时间的数据集中挑选最近将要过期的数据淘汰
# volatile-random -> Remove a random key among the ones with an expire set.
从已设置过期时间的数据集中,随机挑选数据淘汰
# allkeys-lru -> Evict any key using approximated LRU. ----------------------》 广泛采用的策略
从数据集中挑选最近最少使用的数据淘汰
# allkeys-lfu -> Evict any key using approximated LFU.
从数据集中,删除一段时间内使用次数最少的数据
# allkeys-random -> Remove a random key, any key.
从数据集中随机挑选数据淘汰
# noeviction -> Don't evict anything, just return an error on write operations.
禁止淘汰任何数据(默认配置),对于写操作,返回错误信息