admin管理员组

文章数量:814948

Redis节点设置的最佳做法

我已经在单独的文件上设置了redis客户端

export function getRedisClient() {
  const port: number = redisConfig.port || 6379;
  const host = redisConfig.host;

  const client = redis.createClient(port, host, {
    no_ready_check: true,
    retry_strategy: function(options) {
      if (options.error && options.error.code === 'ECONNREFUSED') {
        // End reconnecting on a specific error and flush all commands with
        // a individual error
        return new Error('The server refused the connection');
      }
      if (options.total_retry_time > 1000 * 60 * 60) {
        // End reconnecting after a specific timeout and flush all commands
        // with a individual error
        return new Error('Retry time exhausted');
      }
      if (options.attempt > 10) {
        // End reconnecting with built in error
        return undefined;
      }
      // reconnect after
      return Math.min(options.attempt * 100, 3000);
    },
  });

  client.on('error', function(error) {
    console.error(error);
  });

  client.on('ready', function() {
    console.log('Redis Server Connected...');
  });

  return client;
}

问题是它让我max number of clients reached

每次调用函数时,调用函数都会创建一个新客户端吗?

如果可以的话,你们建议我在多服务中实现Redis的更好做法。

回答如下:

您不必每次都创建另一个客户端。只需创建一个客户端并导出即可。每当您想使用redis时都将其导入。

本文标签: Redis节点设置的最佳做法