Redis的LRU机制介绍
在Redis中,如果设置的maxmemory,那就要配置key的回收机制参数maxmemory-policy,默认volatile-lru,参阅Redis作者的原博客:antirezweblog>>RedisasanLRUcache
原文中写得很清楚:
AnotherwaytouseRedisasacacheisthemaxmemorydirective,afeaturethatallowsspecifyingamaximumamountofmemorytouse.Whennewdataisaddedtotheserver,andthememorylimitwasalreadyreached,theserverwillremovesomeolddatadeletingavolatilekey,thatis,akeywithanEXPIRE(atimeout)set,evenifthekeyisstillfarfromexpiringautomatically.
在Redis服务器占用内存达到maxmemory的情况下,当再想增加内存占用时,会按maxmemory-policy机制将老的数据删除。这里简单说一下volatile-lru,Redis会按LRU算法删除设置了过期时间但还没有过期的key,而对于没有设置过期时间的key,Redis是永远保留的。当然,如果你不想删除没有过期的key,那可以使用noeviction机制
#MAXMEMORYPOLICY:howRediswillselectwhattoremovewhenmaxmemory #isreached?Youcanselectamongfivebehavior: # #volatile-lru->removethekeywithanexpiresetusinganLRUalgorithm #allkeys-lru->removeanykeyaccordinglytotheLRUalgorithm #volatile-random->removearandomkeywithanexpireset #allkeys-random->removearandomkey,anykey #volatile-ttl->removethekeywiththenearestexpiretime(minorTTL) #noeviction->don'texpireatall,justreturnanerroronwriteoperations