喝酒过敏 發表於 2019-5-22 11:34:00

node.js中使用Redis

<p><strong>服务端:</strong></p>
<p><strong>启动Redis服务: redis-server</strong></p>
<p>&nbsp;</p>
<p><strong>客户端:</strong></p>
<p><strong>1、安装Redis&nbsp;&nbsp;</strong></p>
<p><strong>&nbsp; &nbsp; &nbsp;npm install redis --save</strong></p>
<p><strong>2、redisTest.js文件</strong></p>
<p><strong>//引入redis</strong><br>var redis = require("redis");<br>//创建redis客户端<br>var client = redis.createClient("6379", "127.0.0.1");<br>//连接错误处理<br>client.on("error", function (error) {<br>    console.log(error);<br>});<br>//redis验证 (如果redis没有开启验证,此配置可以不写)<br>//client.auth("123");<br>//查找</p>
<p>client.select("15", function (error) {<br>    if (error) {<br>      console.log(error);<br>    } else {<br>      client.set("mykey","please 888");<br>      client.get("mykey",function(err,res){<br>        console.log("get mykey:",res);<br>       })<br>      client.hmset("mykey2",["name","mary2","age",100],function(error,res){<br>        console.log("res:",res);<br>      })<br>       <br>       client.hget("mykey2","name",function(err,res){<br>        console.log("name:", res);<br>})</p>
<p><br>      client.set("node_redis_key", JSON.stringify({ "name": "wolfy", age: 28 }), function (error, res) {<br>            if (error) {<br>                //console.log(error);<br>            } else {<br>                //console.log(res);<br>            };<br>            //操作完成,关闭redis连接<br>            client.end(true);</p>
<p>      });<br>    };<br>});</p>
<p>3、执行文件:node&nbsp;<strong>redisTest.js</strong></p><br><br>
来源:https://www.cnblogs.com/zhaodagang8/p/10904921.html
頁: [1]
查看完整版本: node.js中使用Redis