function createCachingFunction(apiCall, cacheDuration = 60 * 1000) {
let cache = null;
let cacheTimestamp = null;
return async function() {
if (cache && Date.now() - cacheTimestamp < cacheDuration) {
return cache;
}
const newData = await apiCall.apply(this, arguments);
cache = newData;
cacheTimestamp = Date.now();
return cache;
};
}
// 示例使用
async function sampleApiCall() {
// 模拟API调用
return new Promise(resolve => setTimeout(() => resolve('API data'), 1000));
}
const cachedApiCall = createCachingFunction(sampleApiCall, 60 * 1000);
// 调用缓存函数
cachedApiCall().then(data => console.log(data));