Skip to content

Commit bb532d2

Browse files
committed
docs: add examples for Redis operations
1 parent c28314b commit bb532d2

File tree

8 files changed

+197
-1
lines changed

8 files changed

+197
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,15 @@ redis.zrange("sortedSet", 0, 2, "WITHSCORES").then((elements) => {
140140
redis.set("mykey", "hello", "EX", 10);
141141
```
142142

143-
See the `examples/` folder for more examples.
143+
See the `examples/` folder for more examples. For example:
144+
145+
* [Strings](examples/string.js)
146+
* [Hashes](examples/hash.js)
147+
* [Lists](examples/list.js)
148+
* [Sets](examples/set.js)
149+
* [Sorted Sets](examples/zset.js)
150+
* [Streams](examples/stream.js)
151+
* [Redis Modules](examples/module.js) e.g. RedisJSON
144152

145153
## Connect to Redis
146154

examples/hash.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
4+
async function main() {
5+
const user = {
6+
name: "Bob",
7+
// The field of a Redis Hash key can only be a string.
8+
// We can write `age: 20` here but ioredis will convert it to a string anyway.
9+
age: "20",
10+
description: "I am a programmer",
11+
};
12+
13+
await redis.hmset("user-hash", user);
14+
15+
const name = await redis.hget("user-hash", "name");
16+
console.log(name); // "Bob"
17+
18+
const age = await redis.hget("user-hash", "age");
19+
console.log(age); // "20"
20+
21+
const all = await redis.hgetall("user-hash");
22+
console.log(all); // { age: '20', name: 'Bob', description: 'I am a programmer' }
23+
24+
// or `await redis.hdel("user-hash", "name", "description")`;
25+
await redis.hdel("user-hash", ["name", "description"]);
26+
27+
const exists = await redis.hexists("user-hash", "name");
28+
console.log(exists); // 0 (means false, and if it's 1, it means true)
29+
30+
await redis.hincrby("user-hash", "age", 1);
31+
const newAge = await redis.hget("user-hash", "age");
32+
console.log(newAge); // 21
33+
}
34+
35+
main();

examples/list.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
4+
async function main() {
5+
const numbers = [1, 3, 5, 7, 9];
6+
await redis.lpush("user-list", numbers);
7+
8+
const popped = await redis.lpop("user-list");
9+
console.log(popped); // 9
10+
11+
const all = await redis.lrange("user-list", 0, -1);
12+
console.log(all); // [ '7', '5', '3', '1' ]
13+
14+
const position = await redis.lpos("user-list", 5);
15+
console.log(position); // 1
16+
17+
setTimeout(() => {
18+
// `redis` is in the block mode due to `redis.blpop()`,
19+
// so we duplicate a new connection to invoke LPUSH command.
20+
redis.duplicate().lpush("block-list", "hello");
21+
}, 1200);
22+
const blockPopped = await redis.blpop("block-list", 0); // Resolved after 1200ms.
23+
console.log(blockPopped); // [ 'block-list', 'hello' ]
24+
}
25+
26+
main();

examples/module.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
4+
async function main() {
5+
// Redis#call() can be used to call arbitrary Redis commands.
6+
// The first parameter is the command name, the rest are arguments.
7+
await redis.call("JSON.SET", "doc", "$", '{"f1": {"a":1}, "f2":{"a":2}}');
8+
const json = await redis.call("JSON.GET", "doc", "$..f1");
9+
console.log(json); // [{"a":1}]
10+
}
11+
12+
main();

examples/set.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
4+
async function main() {
5+
const numbers = [1, 3, 5, 7, 9];
6+
await redis.sadd("user-set", numbers);
7+
8+
const elementCount = await redis.scard("user-set");
9+
console.log(elementCount); // 5
10+
11+
await redis.sadd("user-set", "1");
12+
const newElementCount = await redis.scard("user-set");
13+
console.log(newElementCount); // 5
14+
15+
const isMember = await redis.sismember("user-set", 3);
16+
console.log(isMember); // 1 (means true, and if it's 0, it means false)
17+
}
18+
19+
main();

examples/stream.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
const pub = new Redis();
4+
5+
const processMessage = (message) => {
6+
console.log("Id: %s. Data: %O", message[0], message[1]);
7+
};
8+
9+
async function listenForMessage(lastId = "$") {
10+
// `results` is an array, each element of which corresponds to a key.
11+
// Because we only listen to one key (mystream) here, `results` only contains
12+
// a single element. See more: https://redis.io/commands/xread#return-value
13+
const results = await redis.xread(
14+
"BLOCK",
15+
0,
16+
"STREAMS",
17+
"user-stream",
18+
lastId
19+
);
20+
const [key, messages] = results[0]; // `key` equals to "user-stream"
21+
22+
messages.forEach(processMessage);
23+
24+
// Pass the last id of the results to the next round.
25+
await listenForMessage(messages[messages.length - 1][0]);
26+
}
27+
28+
listenForMessage();
29+
30+
setInterval(() => {
31+
// `redis` is in the block mode due to `redis.xread('BLOCK', ....)`,
32+
// so we use another connection to publish messages.
33+
pub.xadd("user-stream", "*", "name", "John", "age", "20");
34+
}, 1000);

examples/string.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
4+
async function main() {
5+
const user = {
6+
name: "Bob",
7+
// The value of a Redis key can not be a number.
8+
// We can write `age: 20` here but ioredis will convert it to a string anyway.
9+
age: "20",
10+
description: "I am a programmer",
11+
};
12+
13+
await redis.mset(user);
14+
15+
const name = await redis.get("name");
16+
console.log(name); // "Bob"
17+
18+
const age = await redis.get("age");
19+
console.log(age); // "20"
20+
21+
const all = await redis.mget("name", "age", "description");
22+
console.log(all); // [ 'Bob', '20', 'I am a programmer' ]
23+
24+
// or `await redis.del("name", "description")`;
25+
await redis.del(["name", "description"]);
26+
27+
const exists = await redis.exists("name");
28+
console.log(exists); // 0 (means false, and if it's 1, it means true)
29+
30+
await redis.incrby("age", 1);
31+
const newAge = await redis.get("age");
32+
console.log(newAge); // 21
33+
34+
await redis.set("key_with_ttl", "hey", "EX", 1000);
35+
const ttl = await redis.ttl("key_with_ttl");
36+
console.log(ttl); // a number smaller or equal to 1000
37+
}
38+
39+
main();

examples/zset.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const Redis = require("ioredis");
2+
const redis = new Redis();
3+
4+
async function main() {
5+
const scores = [
6+
{ name: "Bob", score: 80 },
7+
{ name: "Jeff", score: 59.5 },
8+
{ name: "Tom", score: 100 },
9+
{ name: "Alex", score: 99.5 },
10+
];
11+
await redis.zadd(
12+
"user-zset",
13+
...scores.map(({ name, score }) => [score, name])
14+
);
15+
16+
console.log(await redis.zrange("user-zset", 2, 3)); // [ 'Alex', 'Tom' ]
17+
console.log(await redis.zrange("user-zset", 2, 3, "WITHSCORES")); // [ 'Alex', '99.5', 'Tom', '100' ]
18+
console.log(await redis.zrange("user-zset", 2, 3, "REV")); // [ 'Bob', 'Jeff' ]
19+
console.log(await redis.zrange("user-zset", 80, 100, "BYSCORE")); // [ 'Bob', 'Alex', 'Tom' ]
20+
console.log(await redis.zrange("user-zset", 2, 3)); // [ 'Alex', 'Tom' ]
21+
}
22+
23+
main();

0 commit comments

Comments
 (0)