我想使用redis作为我的数据库的缓存。我写了下面的代码,它给了我错误:
const offerData = await client.getAsync(offerID)
^ TypeError: client.getAsync is not a function
我的当前代码:
const redis = require('redis')
// Connect to the Redis server
const client = redis.createClient()
client.on('connect', () => {
console.log('[PASS]'.green + ' Redis Connected')
require('bluebird').promisifyAll(redis)
})
router.get('/', async (req, res) => {
const offerData = await client.getAsync(offerID)
let link
if (offerData) {
// If the offer data is in Redis, parse it and assign it to the link variable
link = JSON.parse(offerData)
} else {
// If the offer data is not in Redis, get it from the database and store it in Redis
link = await Offers.findOne({_id: offerID})
if (link == null) return res.sendStatus(404)
client.set(offerID, JSON.stringify(link))
}
//Do some
})
我该如何解决这个问题?尝试了promisifyAll
,但没有成功
1条答案
按热度按时间ltskdhd11#
下面是一个示例参考: