Catbox-redis is showing disconnected error on my h

2019-09-18 07:50发布

问题:

I have used to catbox-redis plugin but when i run the code i got disconnected error. When i search where i get this error, i found that it was coming from the catbox lib client.js isReady function

If any about this please help me

    {
    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:

Finally i found that. Because async when my redis start process going my redis is tried to get the key. Now i put that into callback of redis get function into redis start everything works fine