'Redis Client in NodeJs
I have just started to learn redis, and want to implement it in my nodejs/express project.
I understand, that you must first make a client and then connect the client to a redis server. The code below is what is on the redis npm package (changed slightly so it better displays what I am doing):
(async () => {
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
app.post('/redis-post', async (req, res) => {
const {key, value} = req.body;
let response = await client.set(key, value);
res.status(200).json(response);
});
})();
However, from this code it seems as if it creates a new client for every request. I don't know much about redis, but I feel as though I am missing or misunderstood something, since connecting to the redis server on a per request basis seems inefficient.
Could someone let me know if what I assumed about creating a client per request was right, and whether or not there is a better way to implement redis in node. (If so, any examples would be appreciated!)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
