vue-hooks-plus
Version:
Vue hooks library
38 lines (37 loc) • 809 B
JavaScript
const cache = /* @__PURE__ */ new Map();
const setCache = (key, cacheTime, cachedData) => {
const currentCache = cache.get(key);
if (currentCache == null ? void 0 : currentCache.timer) {
clearTimeout(currentCache.timer);
}
let timer = void 0;
if (cacheTime > -1) {
timer = setTimeout(() => {
cache.delete(key);
}, cacheTime);
}
cache.set(key, {
...cachedData,
timer
});
};
const getCache = (key) => {
return cache.get(key);
};
const getCacheAll = () => {
return Object.fromEntries(cache.entries());
};
const clearCache = (key) => {
if (key) {
const cacheKeys = Array.isArray(key) ? key : [key];
cacheKeys.forEach((cacheKey) => cache.delete(cacheKey));
} else {
cache.clear();
}
};
export {
clearCache,
getCache,
getCacheAll,
setCache
};