被包装函数的参数类型数组
被包装函数的返回类型
返回包装后的函数,以及缓存相关的额外方法
手动清除缓存
更新 TTL,同时刷新所有未过期缓存的时间
// 异步函数示例
const fetchData = withCache(async function (url: string) {
const data = await fetch(url).then((res) => res.json());
this.setTtl(data.expiresIn); // 根据实际情况调整过期时间
return data;
});
await fetchData(urlA);
await fetchData(urlA); // 使用缓存结果
await fetchData(urlB);
await fetchData(urlB); // 使用缓存结果
fetchData.clear(); // 清除缓存
await fetchData(urlA); // 重新请求
await fetchData(urlB); // 重新请求
// 缓存过期前
await sleep();
fetchData.updateTtl(180); // 更新 ttl 并为所有未过期的缓存续期
await fetchData(urlA); // 使用缓存结果
await fetchData(urlB); // 使用缓存结果
创建一个带缓存的高阶函数