()
| 52 | } |
| 53 | |
| 54 | export function getCache(): { |
| 55 | cache: Keyv | null; |
| 56 | systemCache: Keyv; |
| 57 | deploymentCache: Keyv; |
| 58 | localSchemaCache: Keyv; |
| 59 | lockCache: Keyv; |
| 60 | } { |
| 61 | if (env['CACHE_ENABLED'] === true && cache === null) { |
| 62 | validateEnv(['CACHE_NAMESPACE', 'CACHE_TTL', 'CACHE_STORE']); |
| 63 | cache = getKeyvInstance(env['CACHE_STORE'] as Store, getMilliseconds(env['CACHE_TTL'])); |
| 64 | cache.on('error', (err) => logger.warn(err, `[cache] ${err}`)); |
| 65 | } |
| 66 | |
| 67 | if (systemCache === null) { |
| 68 | systemCache = getKeyvInstance(env['CACHE_STORE'] as Store, getMilliseconds(env['CACHE_SYSTEM_TTL']), '_system'); |
| 69 | systemCache.on('error', (err) => logger.warn(err, `[system-cache] ${err}`)); |
| 70 | } |
| 71 | |
| 72 | if (deploymentCache === null) { |
| 73 | const ttl = getMilliseconds(env['CACHE_DEPLOYMENT_TTL']) || 5000; // Default 5s |
| 74 | deploymentCache = getKeyvInstance(env['CACHE_STORE'] as Store, ttl, '_deployment'); |
| 75 | deploymentCache.on('error', (err) => logger.warn(err, `[deployment-cache] ${err}`)); |
| 76 | } |
| 77 | |
| 78 | if (localSchemaCache === null) { |
| 79 | localSchemaCache = getKeyvInstance('memory', getMilliseconds(env['CACHE_SYSTEM_TTL']), '_schema'); |
| 80 | localSchemaCache.on('error', (err) => logger.warn(err, `[schema-cache] ${err}`)); |
| 81 | } |
| 82 | |
| 83 | if (lockCache === null) { |
| 84 | lockCache = getKeyvInstance(env['CACHE_STORE'] as Store, undefined, '_lock'); |
| 85 | lockCache.on('error', (err) => logger.warn(err, `[lock-cache] ${err}`)); |
| 86 | } |
| 87 | |
| 88 | return { cache, systemCache, deploymentCache, localSchemaCache, lockCache }; |
| 89 | } |
| 90 | |
| 91 | export async function flushCaches(forced?: boolean): Promise<void> { |
| 92 | const { cache } = getCache(); |
no test coverage detected