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