Skip to content

Instantly share code, notes, and snippets.

@femicodes
Forked from haidarafif0809/redis.js
Created May 19, 2020 23:36
Show Gist options
  • Select an option

  • Save femicodes/3e7373b9f08d3e637215e93f9bcadcb5 to your computer and use it in GitHub Desktop.

Select an option

Save femicodes/3e7373b9f08d3e637215e93f9bcadcb5 to your computer and use it in GitHub Desktop.
Redis Middleware using in nodejs express js.
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