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);
});
-
ioredis
也是一个Redis实现,支持Pipelining
、Pub/Sub
等高级功能
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);
});
网友评论