Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Last active May 31, 2026 10:16
Show Gist options
  • Select an option

  • Save Gaurav8757/d9111c73a10e035f1202635b2dad8b61 to your computer and use it in GitHub Desktop.

Select an option

Save Gaurav8757/d9111c73a10e035f1202635b2dad8b61 to your computer and use it in GitHub Desktop.
Advanced Redis or redisio

Redis Methods:

Single key-value pairs set/get karne ke liye use hota hai.

-- set/get => data store in string format

-- hset/hget/hgetall:

  • Data store in Object format
  • Used in store userProfile to get data fast not db calls and if any update invalidate data.
  1. SET/set: await redisClient.set(`user:${req.params.id}:json`, JSON.Stringfy(objData), "EX", 300);
  2. GET/get: const storedOtp = await redisClient.get(otpKey(phone));

Multiple key-value pairs set/get karne ke liye use hota hai.

  1. MSET/mSet:

    await redisClient.mSet({
        name: "Alice",
        city: "Mumbai",
        role: "Developer",
        }); 
    
  2. 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.

Redis ke Hash data structure ke commands hain | Hash ko JavaScript object ki tarah samjho.

// 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" });
});
  1. HDEL/hdel:
    await redisClient.hdel("user:1");
    
  2. HEXISTS/hexists:
     const isExists = await redisClient.hexists(
        "user:1"
      );
    
      console.log(Boolean(isExists));
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment