# 案例
# 1.package.json
{
"name": "eg",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"redis": "^2.8.0"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2.
const redis = require("redis");
//列表
client.lpush("links", "a", redis.print);
client.lpush("links", "b", redis.print);
client.lrange("links", 0, -1, redis.print);
//集合
client.sadd("tags", "a", redis.print);
//如何在redis中模拟对象操作
client.hset("person", "name", "zfpx", redis.print);
client.hset("person", "age", "10", redis.print);
client.hset("person", "home", "beijing", redis.print);
client.hkeys("person", (err, replies) => {
console.log(replies);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3.
const redis = require("redis");
let client1 = redis.createClient(6379, "localhsot");
let client2 = redis.createClient(6379, "localhost");
let count = 0;
client1.subscribe("channel_a");
client12.subscribe("channel_b");
client1.on("message", (channel, message) => {
//当收到第一个消息之后,立刻取消订阅频道channel_b,那以后将不再接收频道b发过来的消息
});
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9