首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >退出命令在node-redis中出现错误

退出命令在node-redis中出现错误
EN

Stack Overflow用户
提问于 2013-09-02 16:37:25
回答 2查看 436关注 0票数 0
代码语言:javascript
复制
function Redis(){

this.redis=require('redis-node');
this.client =this.redis.createClient(6377, '127.0.0.1', {detect_buffers: true});
this.client.auth("pwd");

}

module.exports=Redis;

Redis.prototype.setKeyValue=function(key,value){

var obj=this;
this.client.get(key,function(err,res){

    if(res==null){


        obj.client.set(key,value,function (err, result) {
            console.log(result); 
            obj.client.quit();//here im getting error as client doesn't have method quit
         });

    }
    else{

        console.log('Sorry!!!key is already exist');
    }
});


  };
EN

回答 2

Stack Overflow用户

发布于 2013-09-02 16:55:52

最后我才知道

代码语言:javascript
复制
https://github.com/bnoguchi/redis-node

在上面的库中,客户端没有名为client.quit()的方法,我们可以使用client.close()。

代码语言:javascript
复制
https://github.com/mranney/node_redis

在这里,它有一个名为quit的方法来关闭连接。

票数 0
EN

Stack Overflow用户

发布于 2013-09-03 20:46:03

node_redis是NodeJS的首选库。此外,您的代码没有受到race condition的保护(在getset之间,另一个进程的密钥可能是set的),希望Redis为此提供一个命令:。

最后,您的代码可以简化为:

代码语言:javascript
复制
var redis = require("redis");

function Redis() {
  this.client = redis.createClient(6377, '127.0.0.1', {
    detect_buffers: true,
    auth_pass: "pwd"
  });
}

/**
 * @param {String} key   key name
 * @param {String} value key value
 * @param {Function} f(err, ret)
 */
Redis.prototype.setKeyValue = function(key, value, f) {
  this.client.setnx(key, value, f);
};

module.exports = Redis;

但是,我不明白为什么不直接使用redis客户端api,而是将其包装在一个Redis函数中?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18568841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档