---
title: "Discussion on \"Why You Should Consider Caching Data With Redis In Your Node.js Applications\" | Hashnode"
description: "Discussion on \"Why You Should Consider Caching Data With Redis In Your Node.js Applications\". What is Caching?\nCaching is process of storing data in a cache. A cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier comp..."
url: "https://hashnode.com/posts/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications/5db02038ec5ff27d15810461"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Why You Should Consider Caching Data With Redis In Your Node.js Applications](https://fabcodes.hashnode.dev/why-you-should-consider-caching-data-with-redis-in-your-nodejs-applications?utm_source=hashnode&utm_medium=feed)

What is Caching? Caching is process of storing data in a cache. A cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier comp...

Oct 23, 2019 · fabcodes.hashnode.dev  
Read the article

-   [#redis](https://hashnode.com/tag/redis)
-   [#nodejs](https://hashnode.com/tag/nodejs)
-   [#backend](https://hashnode.com/tag/backend)
-   [#databases](https://hashnode.com/tag/databases)
-   [#caching](https://hashnode.com/tag/caching)

## Written by

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

@thefabdev · Lagos, Nigeria

  
[View profile](https://hashnode.com/@thefabdev)

## Responses 1

Markdown supported · ⌘↵ to post

[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)
      }
    }
  })
});
```

[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
