- Data store in Object format
- Used in store userProfile to get data fast not db calls and if any update invalidate data.
- SET/set:
await redisClient.set(`user:${req.params.id}:json`, JSON.Stringfy(objData), "EX", 300); - GET/get:
const storedOtp = await redisClient.get(otpKey(phone));
-
MSET/mSet:
await redisClient.mSet({ name: "Alice", city: "Mumbai", role: "Developer", }); -
MGET/mGet:
const values = await redisClient.mGet([ "name", "city", "role", ]); Output: ["Alice", "Mumbai", "Developer"] as Array
-
Important
- MSET me TTL set nahi kar sakte.
-
Why use MSET?
- Multiple network round trips avoid hote hain.
- Performance better hoti hai.
- Ek command me multiple keys set ho jati hain.
// 1. set => JSON.stringify(data) and get => JSON.parse(data)
app.post("/user/:id/json", async (req, res) => {
const { id } = req.params;
const { name, email, age, city } = req.body;
if(!id){
return res.status(400).json({ success: false, error: "User ID is required!" });
}
const user = { id, name, email, age, city };
const key = `user:${id}:json`;
await redisClient.set(key, JSON.stringify(user), 'EX', 100);
return res.status(200).json({ success: true, data: "User created successfully" });
});
// Get whole string
app.get("/user/:id/json", async (req, res) => {
const { id } = req.params;
const key = `user:${id}:json`;
const user = await redisClient.get(key);
return res.status(200).json({ success: true, data: JSON.parse(user) }); // Need to parse JSON and replace whole data of string
});
// delete the whole string
app.delete("/user/:id/json", async (req, res) => {
const { id } = req.params;
const key = `user:${id}:json`;
await redisClient.del(key);
return res.status(200).json({ success: true, data: "User deleted successfully" });
});
// 2. HSET => store user profile as hash map
app.post("/user/:id/hset", async (req, res) => {
const { id } = req.params;
const { name, email, age, city } = req.body;
if(!id){
return res.status(400).json({ success: false, error: "User ID is required!" });
}
const user = { id, name, email, age, city };
const key = `user:${id}:hash`;
await redisClient.hset(key, user);
return res.status(200).json({ success: true, data: "User created successfully" });
});
// Get whole hash | object map of user
app.get("/user/:id/hget", async (req, res) => {
const { id } = req.params;
const key = `user:${id}:hash`;
const user = await redisClient.hgetall(key);
return res.status(200).json({ success: true, data: user }); // No need to parse and modify key value objects
});
// delete the whole hash | object
app.delete("/user/:id/hdel", async (req, res) => {
const { id } = req.params;
const key = `user:${id}:hash`;
await redisClient.del(key);
return res.status(200).json({ success: true, data: "User deleted successfully" });
});
// Update Object
app.put("/user/:id/hset", async (req, res) => {
const { id } = req.params;
const { name, email, age, city } = req.body;
if(!id){
return res.status(400).json({ success: false, error: "User ID is required!" });
}
const user = { id, name, email, age, city };
const key = `user:${id}:hash`;
await redisClient.hset(key, user);
return res.status(200).json({ success: true, data: "User updated successfully" });
});
- HDEL/hdel:
await redisClient.hdel("user:1"); - HEXISTS/hexists:
const isExists = await redisClient.hexists( "user:1" ); console.log(Boolean(isExists));