Redis异步库没有Node.js的Redis库中的函数 [英] Redis async library does not have function that are in redis library for node.js

查看:98
本文介绍了Redis异步库没有Node.js的Redis库中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用redis异步功能.目前,我正在使用redis库.我的要求是,在此库中,im使用exist函数检查密钥是否在redis中.如果不是,我正在这个存在函数内部进行数据库调用,并试图返回数据库响应.这是代码的一部分:-

I need to use redis async functions. Currently I am using the redis library. My require ment is that in this library im using the exists function to check if a key is in redis or not. If not i'm making a DB call inside this exists function and trying to return the DB response. Here is the part of code: -

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值,但不能从中返回该值.我在某处读到了原因,原因可能是我从同步方法client.exists中的异步方法db.one返回值. 所以我尝试使用redis-async库.

Here I am able to print the iuid value in console but not return the value from it. I read somewhere the reason maybe that I am returning value from async method db.one inside a sync method client.exists. So I tried using the redis-async library.

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

但是在这里,此myCache变量不具有客户端变量中存在的redis函数(例如exist()).我的要求是在检查缓存中的键后返回数据库调用值.有没有办法使用另一个库或使这个存在的函数异步,以便我可以返回数据库调用的值?

But here this myCache variable does not have the redis functions like exists() that were in client variable. My requirement is here to return the DB call value after checking the key in cache. Is there any way like using another lib or making this exists function async so I can return the value of DB call?

推荐答案

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

After the two async functions are completed, simply pass the final value to a new function and do some work with it.

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 );
}

这篇关于Redis异步库没有Node.js的Redis库中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆