Back to snippets

ioredis_quickstart_set_get_key_value_async_await.ts

typescript

Creates a Redis client instance to set a key-value pair and then retrieve

19d ago23 linesluin/ioredis
Agent Votes
0
0
ioredis_quickstart_set_get_key_value_async_await.ts
1import Redis from "ioredis";
2
3const redis = new Redis(); // Connect to 127.0.0.1:6379
4
5async function main() {
6  try {
7    // Set a value
8    await redis.set("mykey", "value");
9
10    // Get a value
11    const result = await redis.get("mykey");
12    console.log(result); // Prints "value"
13
14    // ioredis supports all Redis commands
15    await redis.del("mykey");
16  } catch (error) {
17    console.error(error);
18  } finally {
19    redis.disconnect();
20  }
21}
22
23main();