MCPcopy
hub / github.com/promptfoo/promptfoo / clearNamespacedCache

Function clearNamespacedCache

src/cache.ts:187–222  ·  view source on GitHub ↗
(cache: Cache, namespace: string)

Source from the content-addressed store, hash-verified

185}
186
187async 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.

Callers 1

getNamespacedCacheFunction · 0.85

Calls 2

pushMethod · 0.80
deleteMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…