Catbox-redis在我的hapijs应用程序上显示断开连接的错误

时间:2017-01-21 12:44:33

标签: node.js redis hapijs

我已经习惯了catbox-redis插件,但是当我运行代码时,我遇到了断开连接的错误。当我搜索出现此错误的地方时,我发现它来自catbox lib client.js isReady函数

如果有任何相关信息,请帮助我

    {
    method : 'POST',
    path   : "/signup",
    config : {
        tags        : ['api'],
        description : 'Customer signup',
        validate    : {
            failAction: Relish.failAction,
            options   : {
                abortEarly: false,
            },
            payload   : signupSchema,
        }
    },
    handler: function(request, response){
        let responseData = {
            'message': 'Data inserted', 
            'errors': [],
            'data': [
                {
                    'id': 'name'
                }
            ]
        };

        const options = {
            //partition: 'examples',               // For redis this will store items under keys that start with examples:
            host: '127.0.0.1',                   // If you don't supply, 127.0.0.1 is the default
            port: 6379,                        // If you don't supply, 6379 is the default
            password: ''                         // If you don't supply, auth command not sent to redis
        };

        var client = new Catbox.Client(require('catbox-redis'), options);    // Chance require('../') to 'catbox-redis' when running in your project
        client.start((error) => {
            console.log('Cache server started');
            console.log('---------------------------------');
        });

        const key = {
            segment: 'example',
            id: 'myExample'
        };

        const cacheValue = 'my example';
        const ttl = 10000;                         // How long item will be cached in milliseconds

        client.get(key, (err, cached) => {

            if (err) {
                console.log(err);
            }
            else if (cached) {
                return callback(null, 'From cache: ' + cached.item);
            }

            client.set(key, cacheValue, ttl, (error) => {

                if(error)
                    console.log(error);
                console.log("Cache was set on the redis");
                console.log('---------------------------------');
            });
        });


        return response(responseData);
    }
}

1 个答案:

答案 0 :(得分:0)

最后我发现了。因为在我的redis启动过程中异步,我的redis会尝试获取密钥。现在我把它放入redis get函数回调redis启动一切正常