Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Created August 20, 2019 12:55
Show Gist options
  • Save bradtraversy/a9dedcdf4350fd417819ee6538482aae to your computer and use it in GitHub Desktop.
Save bradtraversy/a9dedcdf4350fd417819ee6538482aae to your computer and use it in GitHub Desktop.
Node.js & Redis Caching
const express = require('express');
const fetch = require('node-fetch');
const redis = require('redis');
const PORT = process.env.PORT || 5000;
const REDIS_PORT = process.env.PORT || 6379;
const client = redis.createClient(REDIS_PORT);
const app = express();
// Set response
function setResponse(username, repos) {
return `<h2>${username} has ${repos} Github repos</h2>`;
}
// Make request to Github for data
async function getRepos(req, res, next) {
try {
console.log('Fetching Data...');
const { username } = req.params;
const response = await fetch(`https://api.github.com/users/${username}`);
const data = await response.json();
const repos = data.public_repos;
// Set data to Redis
client.setex(username, 3600, repos);
res.send(setResponse(username, repos));
} catch (err) {
console.error(err);
res.status(500);
}
}
// Cache middleware
function cache(req, res, next) {
const { username } = req.params;
client.get(username, (err, data) => {
if (err) throw err;
if (data !== null) {
res.send(setResponse(username, data));
} else {
next();
}
});
}
app.get('/repos/:username', cache, getRepos);
app.listen(5000, () => {
console.log(`App listening on port ${PORT}`);
});
@FottyM
Copy link

FottyM commented Aug 20, 2019

I believe line 6 should be const REDIS_PORT = process.env.REDIS_PORT || 6379;
process.env.REDIS_PORT instead of process.env.PORT

@imizallah
Copy link

I believe line 6 should be const REDIS_PORT = process.env.REDIS_PORT || 6379;
process.env.REDIS_PORT instead of process.env.PORT

That's true

@HansUXdev
Copy link

You can also use console.time().

async function getRepos(req, res, next) {
  try {
    console.log('Fetching Data...');

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

    const data = await response.json();

    const repos = data.public_repos;

    // Set data to Redis
    client.setex(username, 3600, repos);

    res.send(setResponse(username, repos));

    console.timeEnd(`END time`);

  } catch (err) {
    console.error(err);
    res.status(500);
  }
}

And again:

console.time(`START GET time`);
    app.get('/repos/:username', cache, getRepos);
console.timeEnd(`END GET time`);

@asonni
Copy link

asonni commented Aug 21, 2019

I believe line 6 should be const REDIS_PORT = process.env.REDIS_PORT || 6379;
process.env.REDIS_PORT instead of process.env.PORT

And also he should use the const PORT value instead of 5000 on line 57 to be like this app.listen(PORT, () => {

@bradtraversy
Copy link
Author

bradtraversy commented Aug 21, 2019

Yes, you are right. I pinned it as a comment on the video, I will change when I get the chance

@pedro-surf
Copy link

Greetings! [thank you for everything so far, hopefully life makes all equal after all :D] how will this middleware deal with changes on the api data and so on? I mean, if the user creates a new repo.. what'd happen? At line 47, we're just checking if the cached data isn't null, not handling any comparison with api current data, right? Thanks in advance. [btw I'm getting my 2nd mern udemy course, thanks 4all again.]

@pedro-surf
Copy link

Or, will try or/and async , await keywords handle this on the background? Thanks.

@bradtraversy
Copy link
Author

I believe line 6 should be const REDIS_PORT = process.env.REDIS_PORT || 6379;
process.env.REDIS_PORT instead of process.env.PORT

Correct, sorry about that

@jpmoraez
Copy link

jpmoraez commented Apr 9, 2021

Do you have code samples accessing redis through aws elasticache ?

@Islam990
Copy link

Thanks sir

@MoAboabdo
Copy link

This code did not work for me idk why! this error comes out

ClientClosedError: The client is closed

@MartynCastagno
Copy link

I have a same problem. The terminal it shows ClientClosedError: The client is closed. If I use await client.connect(), the system hangs

@YiqiCodes
Copy link

I have the same problem as @MartynCastagno

@YiqiCodes
Copy link

YiqiCodes commented May 4, 2022

Resolved: Keep await client.connect()

Put data in a variable and await the response. See function:

async function cache(req, res, next) {
  const { username } = req.params;
  const data = await client.get(username);
  if (data !== null) {
    res.send(setResponse(username, data));
  } else {
    next();
  }
}

@cayetanopomar
Copy link

I solved the problem : "ClientClosedError: The client is closed"

  1. Stating redis in the terminal:

    @pop-os:~$ redis-server

  2. adding in the code: client.connect();

    that is:

    const client = redis.createClient(REDIS_PORT)
    client.connect();
    client.on('connect', () => {
    console.log('connected');
    });

@javed1af
Copy link

javed1af commented Sep 1, 2022

@MoAboabdo

This code did not work for me idk why! this error comes out
ClientClosedError: The client is closed

try to downgrade your redis and node-fetch packages. It will solve your issue, I have the same issue, now works fine.

@adityagithubraj
Copy link

my case const fetch = require('node-fetch') is not working

@pebueno
Copy link

pebueno commented Jun 21, 2023

my case const fetch = require('node-fetch') is not working

Instal the used version so you don't have to worry about importing it in a different way.
npm i [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment