-
-
Save femicodes/3e7373b9f08d3e637215e93f9bcadcb5 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) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment