Skip to content
Snippets Groups Projects
Unverified Commit 2c2693b5 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(packagist): Use cache decorator for datasource (#14768)

parent 1a6feac0
No related branches found
No related tags found
No related merge requests found
...@@ -95,37 +95,40 @@ export class PackagistDatasource extends Datasource { ...@@ -95,37 +95,40 @@ export class PackagistDatasource extends Datasource {
return meta; return meta;
} }
private async getPackagistFile( private static isPrivatePackage(regUrl: string): boolean {
const opts = PackagistDatasource.getHostOpts(regUrl);
return !!opts.password || !!opts.headers?.authorization;
}
private static getPackagistFileUrl(
regUrl: string, regUrl: string,
file: RegistryFile regFile: RegistryFile
): Promise<PackagistFile> { ): string {
const { key, sha256 } = file; const { key, sha256 } = regFile;
const fileName = key.replace('%hash%', sha256); const fileName = key.replace('%hash%', sha256);
const url = `${regUrl}/${fileName}`;
return url;
}
@cache({
namespace: `datasource-${PackagistDatasource.id}-public-files`,
key: (regUrl: string, regFile: RegistryFile) =>
PackagistDatasource.getPackagistFileUrl(regUrl, regFile),
cacheable: (regUrl: string) =>
!PackagistDatasource.isPrivatePackage(regUrl),
ttlMinutes: 1440,
})
async getPackagistFile(
regUrl: string,
regFile: RegistryFile
): Promise<PackagistFile> {
const url = PackagistDatasource.getPackagistFileUrl(regUrl, regFile);
const opts = PackagistDatasource.getHostOpts(regUrl); const opts = PackagistDatasource.getHostOpts(regUrl);
if (opts.password || opts.headers?.authorization) { const { body: packagistFile } = await this.http.getJson<PackagistFile>(
return ( url,
await this.http.getJson<PackagistFile>(regUrl + '/' + fileName, opts) opts
).body;
}
const cacheNamespace = 'datasource-packagist-files';
const cacheKey = regUrl + key;
// Check the persistent cache for public registries
const cachedResult = await packageCache.get(cacheNamespace, cacheKey);
// istanbul ignore if
if (cachedResult && cachedResult.sha256 === sha256) {
return cachedResult.res as Promise<PackagistFile>;
}
const res = (
await this.http.getJson<PackagistFile>(regUrl + '/' + fileName, opts)
).body;
const cacheMinutes = 1440; // 1 day
await packageCache.set(
cacheNamespace,
cacheKey,
{ res, sha256 },
cacheMinutes
); );
return res; return packagistFile;
} }
private static extractDepReleases(versions: RegistryFile): ReleaseResult { private static extractDepReleases(versions: RegistryFile): ReleaseResult {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment