(
fn: () => Promise<T>,
keyParts: string[],
options: CacheOptions = {}
)
| 121 | } |
| 122 | |
| 123 | export function createCachedFunction<T>( |
| 124 | fn: () => Promise<T>, |
| 125 | keyParts: string[], |
| 126 | options: CacheOptions = {} |
| 127 | ) { |
| 128 | if (keyParts.length === 0) { |
| 129 | throw new Error('keyParts must contain at least one element'); |
| 130 | } |
| 131 | const [prefix, ...parts] = keyParts; |
| 132 | const cacheKey = getCacheKey(prefix, ...parts); |
| 133 | |
| 134 | return async (): Promise<T> => { |
| 135 | if (!Settings.CACHE_ENABLED) { |
| 136 | return fn(); |
| 137 | } |
| 138 | |
| 139 | const cached = await getCache<T>(cacheKey); |
| 140 | if (cached !== null) { |
| 141 | return cached; |
| 142 | } |
| 143 | |
| 144 | const result = await fn(); |
| 145 | await setCache(cacheKey, result, options); |
| 146 | return result; |
| 147 | }; |
| 148 | } |
| 149 |
no test coverage detected