为了减少重复请求,我希望客户端在获取接口数据时使用 ETag 缓存机制,并在离线时优先使用本地缓存结果。请问在HTTP 模块中,如何处理响应头里的 ETag,并在下次请求时带上 If-None-Match?

鸿蒙问答专区鸿蒙应用开发
为了减少重复请求,我希望客户端在获取接口数据时使用 ETag 缓存机制,并在离线时优先使用本地缓存结果。请问在HTTP 模块中,如何处理响应头里的 ETag,并在下次请求时带上 If-None-Match?
```ts
let etag = '';
async function fetchWithCache(url: string) {
const headers: any = {};
if (etag) headers['If-None-Match'] = etag;
const res = await http.request(url, { method: http.RequestMethod.GET, header: headers });
if (res.responseCode === 304) return 'use cache';
etag = res.header['ETag'];
return res.result;
}
```