Redis 异步库没有 Redis 库中用于Node.js的功能



我需要使用 redis 异步函数。目前我正在使用 redis 库。我的要求是在这个库中,我使用 exists 函数来检查密钥是否在 redis 中。如果没有,我正在此存在函数中进行数据库调用并尝试返回数据库响应。这是代码的一部分: -

var redis = require('redis');
var client = redis.createClient(port, 'anyhost');
client.exists(obj.empId, function(err, reply) {
if (reply == 0) {
console.log('indb call');
return db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
.then(iuidtest => {
console.log(iuidtest.iuid);
return iuidtest.empid;
})
}
});

在这里,我可以在控制台中打印 iuid 值,但不能从中返回值。我在某处读到的原因可能是我从同步方法客户端中的异步方法 db.one 返回值。 所以我尝试使用 redis-async 库。

var asyncredis = require('async-redis');
var myCache= asyncredis.createClient(port, 'vsseacgmy13');

但是在这里,这个 myCache 变量没有像客户端变量中的 exists(( 那样的 redis 函数。我的要求是在检查缓存中的键后返回数据库调用值。有没有办法像使用另一个库或使这个存在的函数异步,以便我可以返回数据库调用的值?

完成两个异步函数后,只需将最终值传递给新函数并对其进行一些处理即可。

var redis = require('redis');
var client = redis.createClient(port, 'anyhost');
client.exists(obj.empId, function(err, reply) {
if (reply == 0) {
console.log('indb call');
db.one('SELECT * FROM iuidtest WHERE empid = $1', [obj.empId])
.then(iuidtest => {
console.log(iuidtest.iuid);
doSomethingWith(iuidtest.empid);
})
}
});
const doSomethingWith = empid => {
console.log( "empid = ", empid );
}

最新更新