---
title: "Comment by Guillaume on \"Why You Should Consider Caching Data With Redis In Your Node.js Applications\" | Hashnode"
description: "Hello, Thanks a lot for this article 👏 Some though: IMHO, it&#39;s better if cache is agnostic. I mean if developers doesn&#39;t have to worry about how and wh"
url: "https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461/comment/5db6ee0f7d5f5493565f2183"
---

[Discussion](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461)

Comment [Why You Should Consider Caching Data With Redis In Your Node.js Applications](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461)

[Guillaume](https://hashnode.com/@dolig)

[Oct 28, 2019](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461/comment/5db6ee0f7d5f5493565f2183)

Hello,

Thanks a lot for this article 👏

Some though: IMHO, it's better if cache is agnostic. I mean if developers doesn't have to worry about how and what is cached while developing api.

Another thing: I don't understand why you put the cache retrieve code in a middleware. I might be wrong, but right now, if I query `GET /lmao/:username`, and if the cache exist, looks like it will return me the same response as `GET /repos/:username`.

So why not putting all the cache logic inside the cached route. I.E

```
app.get('/repos/:username', cache, async (req, res) => {
  const { username } = req.params

  client.get(username, (err, data) => {
    if (err) throw err
    if (data !== null) {
      res.send(data)
    } else {
      try {
        const response = await fetch(`api.github.com/users${username}/repos`)
        const data = await response.json()
        let repos = data.map(item => item.name)

        // save the repos to redis using the username as key and with an expiration
        client.setex(username, 60, repos)
        res.send(repos)
      } catch (err) {
        res.status(500)
      }
    }
  })
});
```

[Fakorede Abiola](https://hashnode.com/@thefabdev)

[Oct 28, 2019](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461/comment/5db6ee0f7d5f5493565f2183#reply-5db7331f72775b8e5645c918)

Hello Guillaume,

It's definitely better practice to put the cache logic in a middleware function and not directly inside a route handler.

This way if you have multiple routes that needs to be cached, you can just call the middleware.

Imagine adding the cache logic directly into your routes, that will be messy. It's just like have different routes that only an authenticated user can have access to.

**Note:** If you query the `/repos/:username` endpoint and the cache already exists, yes, it returns the same response but this time, much faster.

I hope that was helpful. 😃

[Guillaume](https://hashnode.com/@dolig)

[Oct 29, 2019](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461/comment/5db6ee0f7d5f5493565f2183#reply-5db80968a2342e8354087647)

Hello [Fakorede Abiola](https://hashnode.com/@thefabdev),

Thanks you for your response. After reading it and the code again and again, I understand that I've missed a point: `app.route` take as many middleware as we want.

Your cache middleware is scoped here and not global to all the app:

```
app.get('/repos/:username', cache, async (req, res) => {
```

So everythings is good. Thanks again for your article, this is a great discovery for me :)

[Fakorede Abiola](https://hashnode.com/@thefabdev)

[Oct 29, 2019](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461/comment/5db6ee0f7d5f5493565f2183#reply-5db849d4a2342e83540879e2)

Glad you found it helpful ☺

[Lyseky Wu](https://hashnode.com/@lyseky)

[Nov 1, 2019](https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461/comment/5db6ee0f7d5f5493565f2183#reply-5dbbe8ddaaa616c464d61c9b)

get it
