Intro:
Redis is an in-memory store perfect for caching API responses in Node.js.
const redis = require("redis");
const client = redis.createClient();
client.on("connect", () => console.log("Connected to Redis"));
// Set cache
client.setex("user:1", 60, JSON.stringify({ name: "Alice" }));
// Get cache
client.get("user:1", (err, data) => {
if (data) {
console.log("Cached Data:", JSON.parse(data));
}
});
Takeaway:
Use setex to store data with an expiry time.
