Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Redis get function

I’m getting github repo data, and then i store it in redis with set. with get am getting current data, but when i trying add function to get it’s not working.

let redisClient;

(async () => {
  redisClient = redis.createClient();

  redisClient.on("error", (error) => console.error(`Error : ${error}`));
  redisClient.on("connect", function () {
    console.log("Redis Connected!");
  });

  await redisClient.connect();
})();


// Make request to Github for data
async function getRepos(req, res, next) {
  try {
    console.log("Fetching Data...");

    const { username } = req.params;

    // with this am getting result
    const cacheResults = await redisClient.get(username);
    console.log(cacheResults);

    // with this am not getting result, how can i fix this? 
    redisClient.get(username, (err, data) => {
      console.log(data);
    });

    const response = await fetch(`https://api.github.com/users/${username}`);

    const data = await response.json();

    const repos = data.public_repos;

    // Set data to Redis
    redisClient.set(username, JSON.stringify(repos));

    res.send(setResponse(username, repos));
  } catch (e) {
    console.log(e);
    res.status(500);
  }
}

it’s don’t console.log(data), i searched a lot and everyone have one example how to use get function, but in me case it’s don’t log, whats am doing wrong?

this is my cache function

// Cache middleware
async function cache(req, res, next) {
  const { username } = req.params;

  try {
    await redisClient.get(username).then((data) => {
      if (data !== null) {
        res.send(setResponse(username, data));
      } else {
        next();
      }
    });
  } catch (error) {
    console.log(error.toString());
  }
}

app.get("/repos/:username", cache, getRepos);

it’s works, but time finish times with cache and without it are same? am doing something wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

can you try like this

redisClient.get(username).then((data) => {
    console.log(data);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading