|
1 | | -import type { CacheEventHooks, PongoCacheProvider } from './types'; |
| 1 | +import { resolveCacheConfig } from './configResolution'; |
| 2 | +import { inMemoryCacheProvider } from './inMemoryProvider'; |
| 3 | +import type { CacheConfig, CacheEventHooks, PongoCacheProvider } from './types'; |
2 | 4 |
|
3 | 5 | export const pongoCacheWrapper = (options: { |
4 | 6 | provider: PongoCacheProvider; |
@@ -88,3 +90,25 @@ export const pongoCacheWrapper = (options: { |
88 | 90 | }, |
89 | 91 | }; |
90 | 92 | }; |
| 93 | + |
| 94 | +export const resolveCollectionCacheProvider = ( |
| 95 | + cacheOption: CacheConfig | 'disabled' | PongoCacheProvider | undefined, |
| 96 | + dbName: string, |
| 97 | + collectionName: string, |
| 98 | +): PongoCacheProvider | null => { |
| 99 | + if (cacheOption === 'disabled') return null; |
| 100 | + |
| 101 | + if (cacheOption !== undefined && typeof (cacheOption as PongoCacheProvider).get === 'function') |
| 102 | + return cacheOption as PongoCacheProvider; |
| 103 | + |
| 104 | + const config = resolveCacheConfig( |
| 105 | + cacheOption === undefined ? undefined : (cacheOption as CacheConfig), |
| 106 | + ); |
| 107 | + if (config === 'disabled') return null; |
| 108 | + |
| 109 | + const raw = inMemoryCacheProvider({ |
| 110 | + ...(config.max !== undefined ? { max: config.max } : {}), |
| 111 | + ...(config.ttl !== undefined ? { ttl: config.ttl } : {}), |
| 112 | + }); |
| 113 | + return pongoCacheWrapper({ provider: raw, dbName, collectionName }); |
| 114 | +}; |
0 commit comments