Skip to content

Commit

Permalink
refactor(maven): Simplify HEAD requests cache (#25868)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Nov 20, 2023
1 parent de1b2a8 commit fa32546
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/modules/datasource/maven/index.ts
Expand Up @@ -3,6 +3,7 @@ import { DateTime } from 'luxon';
import type { XmlDocument } from 'xmldoc';
import { logger } from '../../../logger';
import * as packageCache from '../../../util/cache/package';
import { filterMap } from '../../../util/filter-map';
import * as p from '../../../util/promises';
import { newlineRegex, regEx } from '../../../util/regex';
import { ensureTrailingSlash } from '../../../util/url';
Expand Down Expand Up @@ -220,31 +221,40 @@ export class MavenDatasource extends Datasource {
//
// Even if new version is being released each 10 minutes,
// we still want to reset the whole cache after 24 hours.
const isCacheValid = await packageCache.get<true>(cacheTimeoutNs, cacheKey);
const cacheValid = await packageCache.get<'valid'>(
cacheTimeoutNs,
cacheKey,
);

let cachedReleaseMap: ReleaseMap = {};
// istanbul ignore if
if (isCacheValid) {
if (cacheValid) {
const cache = await packageCache.get<ReleaseMap>(cacheNs, cacheKey);
if (cache) {
cachedReleaseMap = cache;
}
}

// List versions to check with HEAD request
const freshVersions = Object.entries(releaseMap)
.filter(([version, release]) => {
const freshVersions = filterMap(
Object.entries(releaseMap),
([version, release]) => {
// Release is present in maven-metadata.xml,
// but haven't been validated yet
const isValidatedAtPreviousSteps = release !== null;

// Release was validated and cached with HEAD request during previous run
const isValidatedHere = !is.undefined(cachedReleaseMap[version]);

// istanbul ignore if: not easily testable
if (isValidatedAtPreviousSteps || isValidatedHere) {
return null;
}

// Select only valid releases not yet verified with HEAD request
return !isValidatedAtPreviousSteps && !isValidatedHere;
})
.map(([k]) => k);
return version;
},
);

// Update cached data with freshly discovered versions
if (freshVersions.length) {
Expand All @@ -270,9 +280,9 @@ export class MavenDatasource extends Datasource {

await p.all(queue);

if (!isCacheValid) {
if (!cacheValid) {
// Store new TTL flag for 24 hours if the previous one is invalidated
await packageCache.set(cacheTimeoutNs, cacheKey, 'long', 24 * 60);
await packageCache.set(cacheTimeoutNs, cacheKey, 'valid', 24 * 60);
}

// Store updated cache object
Expand Down

0 comments on commit fa32546

Please sign in to comment.