我正在使用客户端节点应用程序中的Axios进行get呼叫。我正在尝试在应用程序中实现缓存机制。我有该应用程序的请求和响应拦截器。我试图基本上使用拦截器来检查数据是否在缓存中,如果是,我想取消请求并从缓存中返回响应。你能帮我怎么做。谢谢。以下是我的拦截器和提取调用。
import * as cache from '../cache-service'; //custom cache service with get and put methods I wrote using IndexedDB
axios.interceptors.request.use((req)=>{
if(!cache.get(req.param.id)){
console.log(`${req.method} ${req.url}`);
return req;
}else{
//TODO, how to cancel the request and return the response from cache?
}
},(error)=>{
console.log('In error section of request interceptor')
return Promise.reject(error);
});
axios.interceptors.response.use((res)=>{
console.log(res.data);
return res;
},(error)=>{
return Promise.reject(error);
});
export async function fetchData(ID) {
let response = await axios.get("http://localhost:8443/api/"+"?id="+ID);
return response;
}