'What is the optimal way of caching Redis?
I am learning about caching Redis for my application. This is the way that i have been caching the database on the server. My question is whether this is the correct way to store data in Redis? And how can I improve this further.
exports.homeView = (req,res) => {
if(req.session.role == 'admin')
{
let booksCached
const getBookCacheAdmin = async () => {
const client = redis.createClient(6379)
await client.connect()
booksCached = await client.get('books_cache_admin')
await client.quit()
}
getBookCacheAdmin()
if(booksCached) {
res.render('index', {title: 'Home' ,books: JSON.parse(booksCached)})
}
else {
bookSchema.find()
.then(async(result)=> {
const client = redis.createClient(6379)
await client.connect()
await client.set('books_cache_admin', JSON.stringify(result))
await client.quit()
res.render('index', {title:'Home', books: result})
})
.catch((err)=> {
console.log(err)
})
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
