NodeJS连接Redis

2020-05-19  本文已影响0人  高级程序狗

1.安装Redis和启停

$ tar xzf redis-6.0.3.tar.gz
$ cd redis-6.0.3
$ make
vim redis.conf
daemonize yes  #设置redis为后台启动
src/redis-server redis.conf
src/redis-cli shutdown
src/redis-cli

2.初始化node项目

const redis = require("redis");
const client = redis.createClient({
  password: 123456,
});

client.on("error", function (error) {
  console.error(error);
});

client.set("key1", "foo");
client.get("key1", (err, val) => {
  console.log(val);
});
const Redis = require("ioredis");
const redis = new Redis({
  password: 123456
}); // uses defaults unless given configuration object
 
var promise = redis.pipeline().set("foo", "bar").get("foo").exec();
promise.then(function (result) {
  // result === [[null, 'OK'], [null, 'bar']]
  console.log(result);
});
上一篇下一篇

猜你喜欢

热点阅读