Redis 内存淘汰策略

Redis作为一个非关系型数据库,那么肯定也是有内存限制的,而且也不是所有的内存都用来存储信息,并且,redis的实现并没有在内存这块做太多的优化,所以实现者为了防止内存过于饱和,采取了一些措施来管控内存。
查看redis的配置文件:

maxmemory 100000000

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations

# The default is:
# maxmemory-policy noeviction

解析:
内存淘汰(置换)策略 :
1). volatile-lru -> remove the key with an expire set using an LRU algorithm
只从设置失效(expire set)的key中选择最近最不经常使用的key进行删除,用以保存新数据
2). allkeys-lru -> remove any key according to the LRU algorithm
优先删除掉最近最不经常使用的key,用以保存新数据
3). volatile-random -> remove a random key with an expire set
只从设置失效(expire set)的key中,(随机)选择一些key进行删除,用以保存新数据
4). allkeys-random -> remove a random key, any key
随机从all-keys中(随机)选择一些key进行删除,用以保存新数据
5). volatile-ttl -> remove the key with the nearest expire time (minor TTL)
只从设置失效(expire set)的key中,选出存活时间(TTL)最短的key进行删除,用以保存新数据
6). noeviction -> don’t expire at all, just return an error on write operations
不进行淘汰,表示即使内存达到上限也不进行置换,所有能引起内存增加的命令都会返回error
配置(默认) :
maxmemory-policy noeviction
样本数量 :
maxmemory-samples 3
Redis 中的 LRU 不是严格意义上的LRU算法实现,是一种近似的 LRU 实现,主要是为了节约内存占用以及提升性能。Redis 有这样一个配置 —— maxmemory-samples,Redis 的 LRU 是取出配置的数目的key,然后从中选择一个最近最不经常使用的 key 进行置换,默认的5,可以通过调整样本数量来取得 LRU 置换算法的速度或是精确性方面的优势。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值