Skip to content

Commit 3601bd0

Browse files
authored
fix: Force centralized datasource cache via cachePrivatePackages (renovatebot#36990)
1 parent 02ef480 commit 3601bd0

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

lib/modules/datasource/index.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ async function getRegistryReleases(
6969
const cacheNamespace: PackageCacheNamespace = `datasource-releases-${datasource.id}`;
7070
const cacheKey = `${registryUrl}:${config.packageName}`;
7171

72-
if (datasource.caching) {
72+
const cacheEnabled = !!datasource.caching; // tells if `isPrivate` flag is supported in datasource result
73+
const cacheForced = GlobalConfig.get('cachePrivatePackages', false); // tells if caching is forced via admin config
74+
75+
if (cacheEnabled || cacheForced) {
7376
const cachedResult = await packageCache.get<ReleaseResult>(
7477
cacheNamespace,
7578
cacheKey,
7679
);
7780

78-
// istanbul ignore if
7981
if (cachedResult) {
8082
logger.trace({ cacheKey }, 'Returning cached datasource response');
8183
DatasourceCacheStats.hit(datasource.id, registryUrl, config.packageName);
@@ -90,32 +92,30 @@ async function getRegistryReleases(
9092
res.registryUrl ??= registryUrl;
9193
}
9294

93-
// cache non-null responses unless marked as private
94-
if (res) {
95-
let cachingAllowed = false;
96-
97-
if (GlobalConfig.get('cachePrivatePackages', false)) {
98-
cachingAllowed = true;
99-
}
95+
if (!res) {
96+
return null;
97+
}
10098

101-
if (datasource.caching && !res.isPrivate) {
102-
cachingAllowed = true;
103-
}
99+
let cache = false;
100+
if (cacheForced) {
101+
cache = true;
102+
} else if (cacheEnabled && !res.isPrivate) {
103+
cache = true;
104+
}
104105

105-
if (cachingAllowed) {
106-
logger.trace({ cacheKey }, 'Caching datasource response');
106+
if (cache) {
107+
logger.trace({ cacheKey }, 'Caching datasource response');
107108

108-
// This is meant to be short-lived cache, so we ignore `hardTtlMinutes`
109-
// and stick to the `softTtlMinutes` value
110-
const { softTtlMinutes: cacheMinutes } = resolveTtlValues(
111-
cacheNamespace,
112-
15,
113-
);
114-
await packageCache.set(cacheNamespace, cacheKey, res, cacheMinutes);
115-
DatasourceCacheStats.set(datasource.id, registryUrl, config.packageName);
116-
} else {
117-
DatasourceCacheStats.skip(datasource.id, registryUrl, config.packageName);
118-
}
109+
// This is meant to be short-lived cache, so we ignore `hardTtlMinutes`
110+
// and stick to the `softTtlMinutes` value
111+
const { softTtlMinutes: cacheMinutes } = resolveTtlValues(
112+
cacheNamespace,
113+
15,
114+
);
115+
await packageCache.set(cacheNamespace, cacheKey, res, cacheMinutes);
116+
DatasourceCacheStats.set(datasource.id, registryUrl, config.packageName);
117+
} else {
118+
DatasourceCacheStats.skip(datasource.id, registryUrl, config.packageName);
119119
}
120120

121121
return res;

lib/modules/datasource/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ export interface DatasourceApi extends ModuleApi {
148148
sourceUrlNote?: string;
149149

150150
/**
151-
* Whether to perform caching in the datasource index/wrapper or not.
152-
* true: datasoure index wrapper should cache all results (based on registryUrl/packageName)
153-
* false: caching is not performed, or performed within the datasource implementation
151+
* Whether to perform centralized caching in the datasource index/wrapper or not.
152+
*
153+
* - `true`: datasource index wrapper will cache all results (based on registryUrl/packageName)
154+
* - **Must be set only if datasource is able to determine and return `isPrivate` flag**
155+
* - `false`: centralized caching is not performed, implementation still could do caching internally
154156
*/
155157
caching?: boolean | undefined;
156158

0 commit comments

Comments
 (0)