Redis节点设置的最佳做法

编程入门 行业动态 更新时间:2024-10-09 08:27:07

Redis<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点设置的最佳做法"/>

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节点设置的最佳做法

本文发布于:2024-05-07 13:56:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   做法   Redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!