Last active
October 18, 2022 19:19
-
-
Save haidarafif0809/5c29004acdef108b9610c7567ac63960 to your computer and use it in GitHub Desktop.
Redis Middleware using in nodejs express js.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const redis = require("redis"); | |
const redis_url = process.env.REDIS_URL || null; | |
const client = redis.createClient(redis_url); | |
module.exports = { | |
getCached: (req, res, next) => { | |
const { redis_key } = req.headers | |
client.get(redis_key, function(err, reply) { | |
if (err) { | |
res.status(500).json({ | |
message: "Somethin Went Wrong" | |
}) | |
} | |
if (reply == null) { | |
next() | |
} else { | |
res.status(200).json({ | |
message: `Success Read ${redis_key}`, | |
data: JSON.parse(reply) | |
}) | |
} | |
}); | |
}, | |
caching: (key, data) => { | |
client.set(key, JSON.stringify(data) ) | |
}, | |
delCache: (key) => { | |
client.del(key) | |
} | |
} |
yeah it is, You pass it to your route before the controller something like router.get('/url', getCached, controller.control)
. he's exporting as an object so i think you destructure the import
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello brother, how to use this middleware. it is not a function?