Skip to content

Commit

Permalink
feat: Add cachePrivatePackages global config option (#30045)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Jul 4, 2024
1 parent c55dc8e commit 8fc2a7b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/usage/self-hosted-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/config/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class GlobalConfig {
'autodiscoverRepoSort',
'autodiscoverRepoOrder',
'userAgent',
'cachePrivatePackages',
];

private static config: RepoGlobalConfig = {};
Expand Down
8 changes: 8 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export interface RepoGlobalConfig {
autodiscoverRepoSort?: RepoSortMethod;
autodiscoverRepoOrder?: SortMethod;
userAgent?: string;
cachePrivatePackages?: boolean;
}

export interface LegacyAdminConfig {
Expand Down
23 changes: 23 additions & 0 deletions lib/util/cache/package/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
7 changes: 6 additions & 1 deletion lib/util/cache/package/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 8fc2a7b

Please sign in to comment.