在 AWS Lambda 函数中使用缓存控制头,禁止 Lambda 数据被浏览器或其他客户端缓存。
示例代码:
exports.handler = async (event, context) => {
// your code here
const response = {
statusCode: 200,
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0"
},
body: JSON.stringify("Hello from Lambda!")
};
return response;
};
在上面的示例代码中,我们在 Lambda 函数响应的头信息中添加了缓存控制头。这里的“no-cache”表示客户端不应该将 Lambda 函数的响应数据缓存在本地,而是需要每次请求时重新获取数据。