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');
}
});
};发布于 2013-09-02 16:55:52
最后我才知道
https://github.com/bnoguchi/redis-node在上面的库中,客户端没有名为client.quit()的方法,我们可以使用client.close()。
https://github.com/mranney/node_redis在这里,它有一个名为quit的方法来关闭连接。
发布于 2013-09-03 20:46:03
node_redis是NodeJS的首选库。此外,您的代码没有受到race condition的保护(在get和set之间,另一个进程的密钥可能是set的),希望Redis为此提供一个命令:。
最后,您的代码可以简化为:
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函数中?
https://stackoverflow.com/questions/18568841
复制相似问题