()
| 69 | } |
| 70 | |
| 71 | function 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 | |
| 117 | function getNamespacedCache(namespace: string) { |
| 118 | const cachedNamespaceInstance = namespacedCacheInstances.get(namespace); |
no test coverage detected
searching dependent graphs…