-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(internal): cached datasource lookups (#5870)
- Loading branch information
Showing
3 changed files
with
106 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { logger } from '../logger'; | ||
|
||
/** | ||
* Cache callback result which has to be returned by the `CacheCallback` function. | ||
*/ | ||
export interface CacheResult<TResult = unknown> { | ||
/** | ||
* The data which should be added to the cache | ||
*/ | ||
data: TResult; | ||
/** | ||
* `data` can only be cached if this is not `true` | ||
*/ | ||
isPrivate?: boolean; | ||
} | ||
|
||
/** | ||
* Simple helper type for defining the `CacheCallback` function return type | ||
*/ | ||
export type CachePromise<TResult = unknown> = Promise<CacheResult<TResult>>; | ||
|
||
/** | ||
* The callback function which is called on cache miss. | ||
*/ | ||
export type CacheCallback<TArg, TResult = unknown> = ( | ||
lookup: TArg | ||
) => CachePromise<TResult>; | ||
|
||
export type CacheConfig<TArg, TResult> = { | ||
/** | ||
* Datasource id | ||
*/ | ||
id: string; | ||
/** | ||
* Cache key | ||
*/ | ||
lookup: TArg; | ||
/** | ||
* Callback to use on cache miss to load result | ||
*/ | ||
cb: CacheCallback<TArg, TResult>; | ||
/** | ||
* Time to cache result in minutes | ||
*/ | ||
minutes?: number; | ||
}; | ||
|
||
/** | ||
* Loads result from cache or from passed callback on cache miss. | ||
* @param param0 Cache config args | ||
*/ | ||
export async function cacheAble<TArg, TResult = unknown>({ | ||
id, | ||
lookup, | ||
cb, | ||
minutes = 60, | ||
}: CacheConfig<TArg, TResult>): Promise<TResult> { | ||
const cacheNamespace = `datasource-${id}`; | ||
const cacheKey = JSON.stringify(lookup); | ||
const cachedResult = await renovateCache.get<TResult>( | ||
cacheNamespace, | ||
cacheKey | ||
); | ||
// istanbul ignore if | ||
if (cachedResult) { | ||
logger.trace({ id, lookup }, 'datasource cachedResult'); | ||
return cachedResult; | ||
} | ||
const { data, isPrivate } = await cb(lookup); | ||
// istanbul ignore if | ||
if (isPrivate) { | ||
logger.trace({ id, lookup }, 'Skipping datasource cache for private data'); | ||
} else { | ||
await renovateCache.set(cacheNamespace, cacheKey, data, minutes); | ||
} | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters