关于c:Redis使用SET命令覆盖旧值后旧值是同步还是异步删除

3次阅读

共计 1051 个字符,预计需要花费 3 分钟才能阅读完成。

背景

  • 最近调研了 redis 大 key 的同步和异步删除形式,大 key 同步删除会造成主线程长时间阻塞。
  • set 命令笼罩旧值时,旧值的开释是同步还是异步执行的呢?

查看源码

  • 在原 key 已有值时,set 命令对应的操作函数为:
  • /* Overwrite an existing key with a new value. Incrementing the reference
     * count of the new value is up to the caller.
     * This function does not modify the expire time of the existing key.
     *
     * The program is aborted if the key was not already present. */
    void dbOverwrite(redisDb *db, robj *key, robj *val) {dictEntry *de = dictFind(db->dict,key->ptr);
    
      serverAssertWithInfo(NULL,key,de != NULL);
      dictEntry auxentry = *de;
      robj *old = dictGetVal(de);
      if (server.maxmemory_policy & MAXMEMORY_FLAG_LFU) {val->lru = old->lru;}
      dictSetVal(db->dict, de, val);
    
      if (server.lazyfree_lazy_server_del) {
          /**
           * 异步删除旧值
           */
          freeObjAsync(old);
          /**
           * 将以后须要开释的旧值 auxentry 的值指针设置为 NULL
           * 这样做是为了防止下一步的 dictFreeVal 操作执行实在的开释操作
           */
          dictSetVal(db->dict, &auxentry, NULL);
      }
    
      /**
       * 执行开释操作
       */
      dictFreeVal(db->dict, &auxentry);
    }
  • 能够看到,在服务器参数 lazyfree_lazy_server_del 为 true 时,redis 服务器会采纳异步删除的形式,否则同步删除。

配置项

  • lazyfree_lazy_server_del 参数能够通过配置文件中的 lazyfree-lazy-server-del 来进行管制,其值为 yes 或 no。

论断

  • 当 lazyfree-lazy-server-del 配置项的值为 no 时,redis 的 set 操作采纳同步的形式开释旧值;值为 yes 时,采纳异步的形式开释旧值。
  • lazyfree-lazy-server-del 的默认值为 no。
正文完
 0