Redis client.get不工作,但client.set正在工作


async function getRepos(req, res, next) {
try {
console.log('Fetching Data...');
const { username } = req.params;
const response = await fetch(`https://api.github.com/users/${username}`);
const data = await response.json();
const repos = data.public_repos;
// Set data to Redis
await client.setex(username, 3600, repos);
res.send(setResponse(username, repos));
} catch (err) {
console.error(err);
res.status(500);
}
}
// Cache middleware
async function cache(req, res, next) {
const { username } = req.params;
console.log(username)
await client.get(username, (err, data) => {
if (err) throw err;
if (data !== null) {
res.send(setResponse(username, data));
} else {
next();
}
});
}

当这段代码执行时,set函数可以工作(使用reds-cli检查(。但当我向api发送请求时,get函数不工作。

您正在混合Redis客户端的promise API和回调API。如果要使用await,则不要设置回调,反之亦然。

// Cache middleware
async function cache(req, res, next) {
const {username} = req.params;
console.log(username);
const data = await client.get(username);
if (data !== null) {
res.send(setResponse(username, data));
} else {
next();
}
}

最新更新