feat: Add cachePrivatePackages global config option (#30045)

This commit is contained in:
Sergei Zharinov 2024-07-04 16:57:26 -03:00 committed by GitHub
parent c55dc8e4ec
commit 8fc2a7bdb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 43 additions and 1 deletions

View file

@ -327,6 +327,10 @@ Results which are soft expired are reused in the following manner:
- The `etag` from the cached results will be reused, and may result in a 304 response, meaning cached results are revalidated
- If an error occurs when querying the `npmjs` registry, then soft expired results will be reused if they are present
## cachePrivatePackages
In the self-hosted setup, use option to enable caching of private packages to improve performance.
## cacheTtlOverride
Utilize this key-value map to override the default package cache TTL values for a specific namespace. This object contains pairs of namespaces and their corresponding TTL values in minutes.

View file

@ -37,6 +37,7 @@ export class GlobalConfig {
'autodiscoverRepoSort',
'autodiscoverRepoOrder',
'userAgent',
'cachePrivatePackages',
];
private static config: RepoGlobalConfig = {};

View file

@ -3117,6 +3117,14 @@ const options: RenovateOptions[] = [
default: 90,
globalOnly: true,
},
{
name: 'cachePrivatePackages',
description:
'Cache private packages in the datasource cache. This is useful for self-hosted setups',
type: 'boolean',
default: false,
globalOnly: true,
},
];
export function getOptions(): RenovateOptions[] {

View file

@ -166,6 +166,7 @@ export interface RepoGlobalConfig {
autodiscoverRepoSort?: RepoSortMethod;
autodiscoverRepoOrder?: SortMethod;
userAgent?: string;
cachePrivatePackages?: boolean;
}
export interface LegacyAdminConfig {

View file

@ -67,6 +67,29 @@ describe('util/cache/package/decorator', () => {
expect(setCache).not.toHaveBeenCalled();
});
it('forces cache if cachePrivatePackages=true', async () => {
GlobalConfig.set({ cachePrivatePackages: true });
class Class {
@cache({
namespace: '_test-namespace',
key: 'key',
cacheable: () => false,
})
public fn(): Promise<string | null> {
return getValue();
}
}
const obj = new Class();
expect(await obj.fn()).toBe('111');
expect(await obj.fn()).toBe('111');
expect(await obj.fn()).toBe('111');
expect(getValue).toHaveBeenCalledTimes(1);
expect(setCache).toHaveBeenCalledOnce();
});
it('caches null values', async () => {
class Class {
@cache({ namespace: '_test-namespace', key: 'key' })

View file

@ -50,7 +50,12 @@ export function cache<T>({
ttlMinutes = 30,
}: CacheParameters): Decorator<T> {
return decorate(async ({ args, instance, callback, methodName }) => {
if (!cacheable.apply(instance, args)) {
const cachePrivatePackages = GlobalConfig.get(
'cachePrivatePackages',
false,
);
const isCacheable = cachePrivatePackages || cacheable.apply(instance, args);
if (!isCacheable) {
return callback();
}