redis设置配置文件的maxmemory参数,可以控制其最大可用内存大小(字节),如果内存超过了,没有设置内存策略,则程序报错。需要设置内存策略。
配置文件中的maxmemory-policy:其默认值是noeviction。
LRU算法:最近最少使用算法。默认删除最近最少使用的键。同时,我们需要注意一下redis中并不会准确的删除所有键中最近最少使用的键,而是随机抽取3个键,删除这三个键中最近最少使用的键.并且这三个数字是可以配置。位置在配置文件中:maxmeory-samples。
################################### LIMITS ####################################
#最大客户端连接数量
# maxclients 10000
# maxmemory <bytes>
#最大内存
maxmemory 473000000
# 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
#设置内存策略
maxmemory-policy volatile-lru
# and minimal TTL algorithms are not precise algorithms but approximated
#LRU 随机删除key的数量
maxmemory-samples 5
############################## APPEND ONLY MODE ###############################