in src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java [124:182]
public Optional<Build> findBuild(CacheContext context) throws IOException {
Path buildInfoPath = remoteBuildPath(context, BUILDINFO_XML);
LOGGER.debug("Checking if build is already downloaded: {}", buildInfoPath);
if (Files.exists(buildInfoPath)) {
LOGGER.info(
"Downloaded build found by checksum {}",
context.getInputInfo().getChecksum());
try {
org.apache.maven.buildcache.xml.build.Build dto = xmlService.loadBuild(buildInfoPath.toFile());
return Optional.of(new Build(dto, CacheSource.REMOTE));
} catch (Exception e) {
LOGGER.info("Downloaded build info is not valid, deleting: {}", buildInfoPath, e);
Files.delete(buildInfoPath);
}
}
if (!cacheConfig.isRemoteCacheEnabled()) {
return Optional.empty();
}
try {
Path lookupInfoPath = remoteBuildPath(context, LOOKUPINFO_XML);
if (Files.exists(lookupInfoPath)) {
final BasicFileAttributes fileAttributes =
Files.readAttributes(lookupInfoPath, BasicFileAttributes.class);
final long lastModified = fileAttributes.lastModifiedTime().toMillis();
final long created = fileAttributes.creationTime().toMillis();
final long now = System.currentTimeMillis();
// throttle remote cache calls, maven like
if (now < created + ONE_HOUR_MILLIS
&& now < lastModified + ONE_MINUTE_MILLIS) { // fresh file, allow lookup every minute
LOGGER.info("Skipping remote lookup, last unsuccessful lookup less than 1m ago.");
return Optional.empty();
} else if (now < created + ONE_DAY_MILLIS
&& now < lastModified + ONE_HOUR_MILLIS) { // less than 1 day file, allow 1 per hour lookup
LOGGER.info("Skipping remote lookup, last unsuccessful lookup less than 1h ago.");
return Optional.empty();
} else if (now > created + ONE_DAY_MILLIS
&& now < lastModified + ONE_DAY_MILLIS) { // more than 1 day file, allow 1 per day lookup
LOGGER.info("Skipping remote lookup, last unsuccessful lookup less than 1d ago.");
return Optional.empty();
}
}
final Optional<Build> build = remoteRepository.findBuild(context);
if (build.isPresent()) {
LOGGER.info("Build info downloaded from remote repo, saving to: {}", buildInfoPath);
Files.createDirectories(buildInfoPath.getParent());
Files.write(buildInfoPath, xmlService.toBytes(build.get().getDto()), CREATE_NEW);
} else {
FileUtils.touch(lookupInfoPath.toFile());
}
return build;
} catch (Exception e) {
LOGGER.error("Remote build info is not valid, cached data is not compatible", e);
return Optional.empty();
}
}