(cache: Cache, namespace: string)
| 185 | } |
| 186 | |
| 187 | async function clearNamespacedCache(cache: Cache, namespace: string) { |
| 188 | const namespacePrefix = `${namespace}:`; |
| 189 | |
| 190 | for (const store of cache.stores) { |
| 191 | if (!store.iterator) { |
| 192 | throw new Error( |
| 193 | `[Cache] Cannot clear namespace ${namespace} because a cache store does not support key iteration.`, |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | const keysToDelete: string[] = []; |
| 198 | for await (const [key] of store.iterator(undefined)) { |
| 199 | if (typeof key === 'string' && key.startsWith(namespacePrefix)) { |
| 200 | keysToDelete.push(key); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (keysToDelete.length === 0) { |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | try { |
| 209 | if (store.deleteMany) { |
| 210 | await store.deleteMany(keysToDelete); |
| 211 | } else { |
| 212 | await Promise.all(keysToDelete.map((key) => store.delete(key))); |
| 213 | } |
| 214 | } catch (err) { |
| 215 | throw new Error( |
| 216 | `[Cache] Failed to clear ${keysToDelete.length} keys for namespace "${namespace}": ${(err as Error).message}`, |
| 217 | ); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Run a function with isolated cache namespace. |
no test coverage detected
searching dependent graphs…