(namespace: string)
| 115 | } |
| 116 | |
| 117 | function getNamespacedCache(namespace: string) { |
| 118 | const cachedNamespaceInstance = namespacedCacheInstances.get(namespace); |
| 119 | if (cachedNamespaceInstance) { |
| 120 | return cachedNamespaceInstance; |
| 121 | } |
| 122 | |
| 123 | const cache = getCacheInstance(); |
| 124 | const namespacedCache = { |
| 125 | ...cache, |
| 126 | get: (key: string) => cache.get(getScopedCacheKey(key, namespace)), |
| 127 | set: (key: string, value: unknown, ttl?: number) => |
| 128 | cache.set(getScopedCacheKey(key, namespace), value, ttl), |
| 129 | del: (key: string) => cache.del(getScopedCacheKey(key, namespace)), |
| 130 | mget: <T>(keys: string[]) => |
| 131 | cache.mget<T>(keys.map((key) => getScopedCacheKey(key, namespace))), |
| 132 | mset: async <T>(list: Array<{ key: string; value: T; ttl?: number }>) => { |
| 133 | const scopedList = list.map(({ key, value, ttl }) => ({ |
| 134 | key: getScopedCacheKey(key, namespace), |
| 135 | value, |
| 136 | ttl, |
| 137 | })); |
| 138 | const savedList = await cache.mset<T>(scopedList); |
| 139 | return (savedList ?? scopedList).map(({ key, value, ttl }) => ({ |
| 140 | key: getUnscopedCacheKey(key, namespace), |
| 141 | value, |
| 142 | ttl, |
| 143 | })); |
| 144 | }, |
| 145 | mdel: (keys: string[]) => cache.mdel(keys.map((key) => getScopedCacheKey(key, namespace))), |
| 146 | ttl: (key: string) => cache.ttl(getScopedCacheKey(key, namespace)), |
| 147 | clear: () => clearNamespacedCache(cache, namespace), |
| 148 | wrap: (...args: Parameters<Cache['wrap']>) => |
| 149 | cache.wrap( |
| 150 | getScopedCacheKey(args[0] as string, namespace), |
| 151 | ...(args.slice(1) as Parameters<Cache['wrap']> extends [string, ...infer Rest] |
| 152 | ? Rest |
| 153 | : never), |
| 154 | ), |
| 155 | } as Cache; |
| 156 | |
| 157 | namespacedCacheInstances.set(namespace, namespacedCache); |
| 158 | return namespacedCache; |
| 159 | } |
| 160 | |
| 161 | function getCurrentCacheNamespace() { |
| 162 | return cacheNamespaceStorage.getStore()?.namespace; |
no test coverage detected
searching dependent graphs…