Solution & Challenge: What is Redis? · Redis
Ch 2 · Redis Introduction and Setup — card 4 of 4.
Check your work against the worked solution, which sets and reads a name, stores a token that expires after 300 seconds, then deletes a key and confirms it has gone. The challenge asks you to build a small command-line key-value store that understands SET, GET, DEL and TTL, lists keys with SCAN rather than KEYS, and handles errors such as a missing key gracefully.
const Redis = require("ioredis");
const redis = new Redis();
async function main() {
await redis.set("name", "Alice");
console.log(await redis.get("name")); // Alice
await redis.set("token", "xyz", "EX", 300);
console.log("TTL:", await redis.ttl("token")); // TTL: 300
await redis.del("name");
console.log("Exists:", await redis.exists("name")); // Exists: 0
}
main().finally(() => redis.quit());- SET key value [EX seconds]
- GET key
- DEL key
- TTL key
- List keys matching a pattern with SCAN (not KEYS, which blocks Redis while it checks every key, so is only safe on a tiny practice database)