MCPcopy
hub / github.com/promptfoo/promptfoo / getNamespacedCache

Function getNamespacedCache

src/cache.ts:117–159  ·  view source on GitHub ↗
(namespace: string)

Source from the content-addressed store, hash-verified

115}
116
117function 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
161function getCurrentCacheNamespace() {
162 return cacheNamespaceStorage.getStore()?.namespace;

Callers 1

getCacheFunction · 0.85

Calls 6

getCacheInstanceFunction · 0.85
getScopedCacheKeyFunction · 0.85
getUnscopedCacheKeyFunction · 0.85
clearNamespacedCacheFunction · 0.85
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…