MCPcopy
hub / github.com/promptfoo/promptfoo / getCacheInstance

Function getCacheInstance

src/cache.ts:71–115  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

69}
70
71function getCacheInstance() {
72 if (!cacheInstance) {
73 let cachePath = '';
74 const stores = [];
75
76 if (cacheType === 'disk' && enabled) {
77 cachePath =
78 getEnvString('PROMPTFOO_CACHE_PATH') || path.join(getConfigDirectoryPath(), 'cache');
79
80 if (!fs.existsSync(cachePath)) {
81 logger.info(`Creating cache folder at ${cachePath}.`);
82 fs.mkdirSync(cachePath, { recursive: true });
83 }
84
85 const newCacheFile = path.join(cachePath, 'cache.json');
86
87 try {
88 const store = new KeyvFile({
89 filename: newCacheFile,
90 });
91
92 const keyv = new Keyv({
93 store,
94 ttl: getCacheTtlMs(),
95 });
96
97 stores.push(keyv);
98 } catch (err) {
99 logger.warn(
100 `[Cache] Failed to initialize disk cache: ${(err as Error).message}. ` +
101 `Using memory cache instead.`,
102 );
103 // Falls through to memory cache
104 }
105 }
106
107 // Initialize cache (disk if stores array has items, memory otherwise)
108 cacheInstance = createCache({
109 stores,
110 ttl: getCacheTtlMs(),
111 refreshThreshold: 0, // Disable background refresh
112 });
113 }
114 return cacheInstance;
115}
116
117function getNamespacedCache(namespace: string) {
118 const cachedNamespaceInstance = namespacedCacheInstances.get(namespace);

Callers 4

getCacheFunction · 0.85
getNamespacedCacheFunction · 0.85
fetchWithCacheFunction · 0.85
clearCacheFunction · 0.85

Calls 4

getEnvStringFunction · 0.90
getConfigDirectoryPathFunction · 0.90
getCacheTtlMsFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…