2020-10-23 16:11:00 +00:00
|
|
|
let caches: any = {}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
const get = (cacheName: string, key: string): any => {
|
2020-10-23 16:11:00 +00:00
|
|
|
const gotCache: Map<string, any> = caches[cacheName]
|
|
|
|
if (gotCache === undefined || !(gotCache instanceof Map)) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const gotMap = gotCache.get(key)
|
|
|
|
return gotMap
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
const set = (cacheName: string, key: string, value: any): any => {
|
2020-10-23 16:11:00 +00:00
|
|
|
let gotCache: Map<string, any> = caches[cacheName]
|
|
|
|
if (gotCache === undefined || !(gotCache instanceof Map)) {
|
|
|
|
gotCache = caches[cacheName] = new Map<string, any>()
|
|
|
|
}
|
|
|
|
|
|
|
|
gotCache.set(key, value)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
const del = (cacheName: string, key: string): boolean | undefined => {
|
2020-10-23 16:11:00 +00:00
|
|
|
const gotCache: Map<string, any> = caches[cacheName]
|
|
|
|
if (gotCache === undefined || !(gotCache instanceof Map)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return gotCache.delete(key)
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
const deleteCache = (cacheName: string): void => {
|
2020-10-23 16:11:00 +00:00
|
|
|
const gotCache = caches[cacheName]
|
|
|
|
if (gotCache === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
2020-10-23 16:11:00 +00:00
|
|
|
delete caches[cacheName]
|
|
|
|
}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
const resetCaches = (): void => {
|
2020-10-23 16:11:00 +00:00
|
|
|
caches = {}
|
|
|
|
}
|
|
|
|
|
2020-10-29 14:43:27 +00:00
|
|
|
export default { get, set, del, deleteCache, resetCaches }
|
2020-10-23 16:11:00 +00:00
|
|
|
export { get, set, del, deleteCache, resetCaches }
|