JeromeD3 / react-hooks

my hooks
0 stars 0 forks source link

useRequest缓存 --- 删除缓存 #4

Open JeromeD3 opened 1 year ago

JeromeD3 commented 1 year ago

ahooks 提供了一个 clearCache 方法,可以清除指定 cacheKey 的缓存数据。

JeromeD3 commented 1 year ago

当我们想强制删除缓存,一定要让组件重新请求的时候,我们可以这样子做。 直接在cache 这个map中新增一个删除函数就行啦

cache

const cache = new Map();

const setCache = (key, cachedData) => {
  cache.set(key, {
    ...cachedData
  });
};

const getCache = key => {
  return cache.get(key);
};

+const clearCache = key => {
+  if (key) {
+    const cacheKeys = Array.isArray(key) ? key : [key];
+    cacheKeys.forEach(cacheKey => cache.delete(cacheKey));
+  } else {
+    cache.clear();
+  }
+};
export { getCache, setCache, clearCache };