bobby.peng

zhendecai

  • 主页
  • 技术
  • 杂谈
  • Tips
所有文章 友链 关于我

bobby.peng

zhendecai

  • 主页
  • 技术
  • 杂谈
  • Tips

redis键过期

2018-04-10

过期数据结构

redis_server结构图例

expires 指向一个dict, dict内部存放每个键的过期时间。

过期键删除策略

定时删除

每次 expire的时候创建一个定时器,通过定时器来删除

  • 优点
    • 键会尽可能快速的被回收,节省内存
  • 缺点
    • 对cpu非常不友好,对性能要求较高的缓存服务器来说不可接受

      惰性删除

      等到使用时采取检查是否过期
  • 优点
    • 对cpu友好,不会花费额外的时间去处理过期键
  • 缺点
    • 对内存不友好,如果不被访问永远不会删除

定期删除

每过一段时间就对数据做一次删除,控制删除的量。
中和策略。但是不能准确的过期。

redis过期策略

惰性删除和定期删除

  • 定时删除是由一个timer来执行
    1
    2
    3
    4
    5
    6
    7
    8
    void aeMain(aeEventLoop *eventLoop) {
    eventLoop->stop = 0;
    while (!eventLoop->stop) {
    if (eventLoop->beforesleep != NULL)
    eventLoop->beforesleep(eventLoop);
    aeProcessEvents(eventLoop, AE_ALL_EVENTS);
    }
    }

aof & rdb 复制功能对过期键的处理

  • 从服务器一律不处理,主一律处理
  • 从服务器不处理惰性过期,等待主服务器的过期信息(expiredIfNeeded 中的源代码)
    1
    2
    3
    4
    5
    6
    7
    8
    /* If we are running in the context of a slave, return ASAP:
    * the slave key expiration is controlled by the master that will
    * send us synthesized DEL operations for expired keys.
    *
    * Still we try to return the right information to the caller,
    * that is, 0 if we think the key should be still valid, 1 if
    * we think the key is expired at this time. */
    if (server.masterhost != NULL) return now > when;
  • redis
  • tech

扫一扫,分享到微信

微信分享二维码
redis复制
redis持久化机制
Like Issue Page
Loading comments...
Login with GitHub
Styling with Markdown is supported
Powered by Gitment
© 2018 bobby.peng
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • tree
  • cocurrent
  • java
  • 复习
  • mysql
  • data-structure
  • ximalaya
  • redis
  • wow
  • 转载
  • jvm
  • concurrent
  • aqs
  • Spring
  • io

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 麻子
  • 王鸿缘
  • 美团技术博客
  • 何登成的技术博客
  • 并发编程网
Blizzard fans
wow & overwatch
Arena master & Gladiator