加入收藏 | 设为首页 | 会员中心 | 我要投稿 淮北站长网 (https://www.0561zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 交互 > 正文

redis lru实现策略

发布时间:2016-11-01 18:21:21 所属栏目:交互 来源:站长网
导读:副标题#e# 在使用redis作为缓存的场景下,内存淘汰策略决定的redis的内存使用效率。在大部分场景下,我们会采用LRU(Least Recently Used)来作为redis的淘汰策略。本文将由浅入深的介绍redislru策略的具体实现。 首先我们来科普下,什么是LRU ?(以下来自维

点击(此处)折叠或打开

  1. # MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
  2. # is reached. You can select among five behaviors:
  3. #
  4. # volatile-lru -> Evict using approximated LRU among the keys with an expire set. //在设置了过期时间的key中,使用近似的lru淘汰策略
  5. # allkeys-lru -> Evict any key using approximated LRU. //所有的key均使用近似的lru淘汰策略
  6. # volatile-lfu -> Evict using approximated LFU among the keys with an expire set. //在设置了过期时间的key中,使用lfu淘汰策略
  7. # allkeys-lfu -> Evict any key using approximated LFU. //所有的key均使用lfu淘汰策略
  8. # volatile-random -> Remove a random key among the ones with an expire set. //在设置了过期时间的key中,使用随机淘汰策略
  9. # allkeys-random -> Remove a random key, any key. //所有的key均使用随机淘汰策略
  10. # volatile-ttl -> Remove the key with the nearest expire time (minor TTL) //使用ttl淘汰策略
  11. # noeviction -> Don't evict anything, just return an error on write operations . //不允许淘汰,在写操作发生,但内存不够时,将会返回错误
  12. #
  13. # LRU means Least Recently Used
  14. # LFU means Least Frequently Used
  15. #
  16. # Both LRU, LFU and volatile-ttl are implemented using approximated
  17. # randomized algorithms.
这里暂不讨论LFU,TTL淘汰算法和noeviction的情况,仅仅讨论lru所有场景下的,淘汰策略具体实现。(LFU和TTL将在下一篇文章中详细分析)。
   LRU淘汰的场景:      1.主动淘汰。         1.1 通过定时任务serverCron定期的清理过期的key。      2.被动淘汰         2.1 每次写入key时,发现内存不够,调用activeExpireCycle释放一部分内存。         2.2 每次访问相关的key,如果发现key过期,直接释放掉该key相关的内存。

 首先我们来分析LRU主动淘汰的场景:  serverCron每间隔1000/hz ms会调用databasesCron方法来检测并淘汰过期的key。

(编辑:淮北站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读