微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nodejs中的IOredis连接已关闭错误

如何解决Nodejs中的IOredis连接已关闭错误

IOredis 连接在 Nodejs 中关闭错误。我们正在使用ioredis npm,当我们尝试从redis获取数据时,我们收到“连接已关闭错误

实际上,在调用 hnormalget() 函数时会给出 Connection is Closed 错误

var redis = require('ioredis');
var config = require('./../../config');
var redisConfig = config.database;
var clientRead = Promise.promisifyAll(redis.createClient({
    port: config.database.redisPort,host: config.database.redisHost,prefix: config.database.redisPrefix,password: config.database.redisPassword,connectionName: 'redisDB-gpRedisCacheRead',db:10,readOnly: true,keepAlive: 1,retryStrategy : function retryStrategy(times) {
        log.error("Redis retry strategy called for gpRedisCache.js...44")
        if (typeof times != "number") {
            // Manual Reconnect would be require
            // redis.connect()
            return new Error('Redis Retry time exhausted');
        }
        const delay = Math.min(times * 50,2000);
        return delay;
    }
}));

    var dbnUM = config.redis.GPDatabaseNum;
    clientRead.on('connect',function() {
        clientRead.select(dbnUM,function(e,r) {
            if (e) {
                log.error(`Error in redis Read select db server/lib/gpRedisCache.js: ${e}`);
                clientRead.end(true); //also initiate reconnection
            } else {
                // console.log('clientRead connected');
            }

        });
    }); //end of client.on('connect')

    clientRead.on('error',function(err) {
        log.error(`Error in redis connection on file gpRedisCache.js:95: ${err}`);
        clientRead.end(true); //also initiate reconnection here
    });




var redisCacheSeparateReadWritePooledConn = {
    hnormalget: (hashKey,key,cb) => {
        return clientRead.hget(hashKey,key).then((data) => {
            return { err: null,data };
        }).catch(err => {
            return { err };
        });
    },}

//TILL above is the handling for single conn
module.exports = redisCacheSeparateReadWritePooledConn;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。