UNPKG

vue-admin-core

Version:
1 lines 2.16 kB
{"version":3,"file":"cache.mjs","sources":["../../../../../../package/hooks/useRequest/__utils__/cache.ts"],"sourcesContent":["type Timer = ReturnType<typeof setTimeout>;\ntype CachedKey = string | number;\n\nexport interface CachedData<TData = any, TParams = any> {\n data: TData;\n params: TParams;\n time: number;\n}\ninterface RecordData extends CachedData {\n timer: Timer | undefined;\n}\n\nconst cache = new Map<CachedKey, RecordData>();\n\nconst setCache = (key: CachedKey, cacheTime: number, cachedData: CachedData) => {\n const currentCache = cache.get(key);\n if (currentCache?.timer) {\n clearTimeout(currentCache.timer);\n }\n\n let timer: Timer | undefined;\n\n if (cacheTime > -1) {\n // if cache out, clear it\n timer = setTimeout(() => {\n cache.delete(key);\n }, cacheTime);\n }\n\n cache.set(key, {\n ...cachedData,\n timer\n });\n};\n\nconst getCache = (key: CachedKey) => {\n return cache.get(key);\n};\n\nconst clearCache = (key?: string | string[]) => {\n if (key) {\n const cacheKeys = Array.isArray(key) ? key : [key];\n cacheKeys.forEach((cacheKey) => cache.delete(cacheKey));\n } else {\n cache.clear();\n }\n};\n\nexport { getCache, setCache, clearCache };\n"],"names":[],"mappings":"AAYA,MAAM,KAAA,uBAAY,GAA2B,EAAA,CAAA;AAE7C,MAAM,QAAW,GAAA,CAAC,GAAgB,EAAA,SAAA,EAAmB,UAA2B,KAAA;AAC9E,EAAM,MAAA,YAAA,GAAe,KAAM,CAAA,GAAA,CAAI,GAAG,CAAA,CAAA;AAClC,EAAA,IAAI,6CAAc,KAAO,EAAA;AACvB,IAAA,YAAA,CAAa,aAAa,KAAK,CAAA,CAAA;AAAA,GACjC;AAEA,EAAI,IAAA,KAAA,CAAA;AAEJ,EAAA,IAAI,YAAY,CAAI,CAAA,EAAA;AAElB,IAAA,KAAA,GAAQ,WAAW,MAAM;AACvB,MAAA,KAAA,CAAM,OAAO,GAAG,CAAA,CAAA;AAAA,OACf,SAAS,CAAA,CAAA;AAAA,GACd;AAEA,EAAA,KAAA,CAAM,IAAI,GAAK,EAAA;AAAA,IACb,GAAG,UAAA;AAAA,IACH,KAAA;AAAA,GACD,CAAA,CAAA;AACH,EAAA;AAEM,MAAA,QAAA,GAAW,CAAC,GAAmB,KAAA;AACnC,EAAO,OAAA,KAAA,CAAM,IAAI,GAAG,CAAA,CAAA;AACtB,EAAA;AAEM,MAAA,UAAA,GAAa,CAAC,GAA4B,KAAA;AAC9C,EAAA,IAAI,GAAK,EAAA;AACP,IAAA,MAAM,YAAY,KAAM,CAAA,OAAA,CAAQ,GAAG,CAAI,GAAA,GAAA,GAAM,CAAC,GAAG,CAAA,CAAA;AACjD,IAAA,SAAA,CAAU,QAAQ,CAAC,QAAA,KAAa,KAAM,CAAA,MAAA,CAAO,QAAQ,CAAC,CAAA,CAAA;AAAA,GACjD,MAAA;AACL,IAAA,KAAA,CAAM,KAAM,EAAA,CAAA;AAAA,GACd;AACF;;;;"}