node.js中redis使用有什么坑,怎么做好
这篇文章主要介绍“node.js中redis使用有什么坑,怎么做好”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“node.js中redis使用有什么坑,怎么做好”文章能帮助大家解决问题。
以上就是关于“node.js中redis使用有什么坑,怎么做好”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注博信,小编每天都会为大家更新不同的知识。
v
ar redis = require("redis"), client = redis.createClient({host:'tc-arch-osp33.tc', port: 4300,no_ready_check:true}); // if you'd like to select database 3, instead of 0 (default), call // client.select(3, function() { /* ... */ }); client.on("error", function (err) { console.log("Error " + err); }); client.on("connect", function (err) { console.log("success" + err); client.set("stringkey", "string val",function (err1, re1){ console.log(err1); console.log(re1); });;
上面的代码连接我厂(baidu)提供的redis会出现以下现象:
successundefined Error AbortError: Ready check failed: Redis connection lost and command aborted. It might have been processed. successundefined Error AbortError: Ready check failed: Redis connection lost and command aborted. It might have been processed. successundefined
//如此反复出现
但通过命令行redis-cli可以正常连接并操作redis。
到此反复试验N次,花费很久。
抓包看看,结果如下:
可以看到nodejs不断发起针对 redis server的链接,redis server不断的关闭,如此反复,跟前端的日志输出也是对应的。
细看其中一个包:
貌似看到nodejs发了一个未知的命令给redis server,经过询问我厂的redis维护人员,说是不支持info命令。。。。
至此真想大白
再查nodejs redis文档,有一条:
no_ready_check false When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will not respond to any commands. To work around this,node_redis
has a "ready check" which sends theINFO
command to the server. The response from theINFO
command indicates whether the server is ready for more commands. When ready,node_redis
emits aready
event. Settingno_ready_check
totrue
will inhibit this check.
所以给我们的代码加一个参数即可,如下:
no_ready_check:true
client = redis.createClient({host:'tc-arch-osp33.tc', port: 4300,no_ready_check:true});
问题解决,我厂真是坑多呀!
以上就是关于“node.js中redis使用有什么坑,怎么做好”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注博信,小编每天都会为大家更新不同的知识。
版权声明
本文仅代表作者观点,不代表博信信息网立场。