Skip to content

Commit

Permalink
refactor: global cache (#6198)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 11, 2020
1 parent 180544a commit 6104c46
Show file tree
Hide file tree
Showing 47 changed files with 148 additions and 98 deletions.
3 changes: 2 additions & 1 deletion lib/config/presets/github/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { mocked } from '../../../../test/util';
import { PLATFORM_FAILURE } from '../../../constants/error-messages';
import { GotResponse } from '../../../platform';
import * as globalCache from '../../../util/cache/global';
import { clear } from '../../../util/cache/run';
import _got from '../../../util/got';
import * as _hostRules from '../../../util/host-rules';
Expand All @@ -16,7 +17,7 @@ const hostRules = mocked(_hostRules);
describe('config/presets/github', () => {
beforeEach(() => {
got.mockReset();
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
describe('fetchJSONFile()', () => {
beforeEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion lib/config/presets/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GotResponse } from '../../../platform';
import { api } from '../../../platform/gitlab/gl-got-wrapper';
import * as globalCache from '../../../util/cache/global';
import * as gitlab from '.';
import { PartialDeep } from 'type-fest';

Expand All @@ -11,7 +12,7 @@ const glGot: jest.Mock<Promise<PartialDeep<GotResponse>>> = api.get as never;
describe('config/presets/gitlab', () => {
beforeEach(() => {
glGot.mockReset();
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
describe('getPreset()', () => {
it('throws if non-default', async () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/config/presets/local/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as globalCache from '../../../util/cache/global';
import * as github from '../github';
import * as gitlab from '../gitlab';
import * as local from '.';
Expand All @@ -18,7 +19,7 @@ describe('config/presets/local', () => {
gitlabGetPreset.mockResolvedValueOnce({ resolved: 'preset' });
githubGetPreset.mockReset();
githubGetPreset.mockResolvedValueOnce({ resolved: 'preset' });
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
describe('getPreset()', () => {
it('throws for unsupported platform', async () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/config/presets/npm/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import nock from 'nock';
import * as globalCache from '../../../util/cache/global';
import * as npm from '.';

jest.mock('registry-auth-token');
Expand All @@ -10,7 +11,7 @@ describe('config/presets/npm', () => {
jest.resetAllMocks();
global.trustLevel = 'low';
nock.cleanAll();
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
afterEach(() => {
delete process.env.RENOVATE_CACHE_NPM_MINUTES;
Expand Down
8 changes: 3 additions & 5 deletions lib/datasource/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../logger';
import * as globalCache from '../util/cache/global';

/**
* Cache callback result which has to be returned by the `CacheCallback` function.
Expand Down Expand Up @@ -57,10 +58,7 @@ export async function cacheAble<TArg, TResult = unknown>({
}: CacheConfig<TArg, TResult>): Promise<TResult> {
const cacheNamespace = `datasource-${id}`;
const cacheKey = JSON.stringify(lookup);
const cachedResult = await renovateCache.get<TResult>(
cacheNamespace,
cacheKey
);
const cachedResult = await globalCache.get<TResult>(cacheNamespace, cacheKey);
// istanbul ignore if
if (cachedResult) {
logger.trace({ id, lookup }, 'datasource cachedResult');
Expand All @@ -71,7 +69,7 @@ export async function cacheAble<TArg, TResult = unknown>({
if (isPrivate) {
logger.trace({ id, lookup }, 'Skipping datasource cache for private data');
} else {
await renovateCache.set(cacheNamespace, cacheKey, data, minutes);
await globalCache.set(cacheNamespace, cacheKey, data, minutes);
}
return data;
}
3 changes: 2 additions & 1 deletion lib/datasource/cdnjs/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';
import * as globalCache from '../../util/cache/global';
import _got from '../../util/got';
import { getReleases } from '.';

Expand All @@ -22,7 +23,7 @@ describe('datasource/cdnjs', () => {
describe('getReleases', () => {
beforeEach(() => {
jest.clearAllMocks();
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
it('throws for empty result', async () => {
got.mockResolvedValueOnce(null);
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/crate/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { Http } from '../../util/http';
import {
DatasourceError,
Expand All @@ -16,7 +17,7 @@ export async function getReleases({
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const cacheNamespace = 'datasource-crate';
const cacheKey = lookupName;
const cachedResult = await renovateCache.get<ReleaseResult>(
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
cacheKey
);
Expand Down Expand Up @@ -92,7 +93,7 @@ export async function getReleases({
});

const cacheMinutes = 10;
await renovateCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
await globalCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
return result;
} catch (err) {
if (err.statusCode === 404 || err.code === 'ENOTFOUND') {
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AWS from 'aws-sdk';
import AWSMock from 'aws-sdk-mock';
import { getPkgReleases } from '..';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';
import * as globalCache from '../../util/cache/global';
import { clear } from '../../util/cache/run';
import _got from '../../util/got';
import * as _hostRules from '../../util/host-rules';
Expand Down Expand Up @@ -36,7 +37,7 @@ describe('api/docker', () => {
password: 'some-password',
});
hostRules.hosts = jest.fn(() => []);
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
it('returns null if no token', async () => {
got.mockReturnValueOnce({ body: {} });
Expand Down Expand Up @@ -281,7 +282,7 @@ describe('api/docker', () => {
beforeEach(() => {
jest.clearAllMocks();
clear();
return global.renovateCache.rmAll();
return globalCache.rmAll();
});
it('returns null if no token', async () => {
got.mockReturnValueOnce({ body: {} });
Expand Down
13 changes: 7 additions & 6 deletions lib/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import parseLinkHeader from 'parse-link-header';
import wwwAuthenticate from 'www-authenticate';
import { logger } from '../../logger';
import { HostRule } from '../../types';
import * as globalCache from '../../util/cache/global';
import * as hostRules from '../../util/host-rules';
import { Http, HttpResponse } from '../../util/http';
import { DatasourceError, GetReleasesConfig, ReleaseResult } from '../common';
Expand Down Expand Up @@ -334,7 +335,7 @@ export async function getDigest(
try {
const cacheNamespace = 'datasource-docker-digest';
const cacheKey = `${registry}:${repository}:${newTag}`;
const cachedResult = await renovateCache.get(cacheNamespace, cacheKey);
const cachedResult = await globalCache.get(cacheNamespace, cacheKey);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
Expand All @@ -350,7 +351,7 @@ export async function getDigest(
const digest = extractDigestFromResponse(manifestResponse);
logger.debug({ digest }, 'Got docker digest');
const cacheMinutes = 30;
await renovateCache.set(cacheNamespace, cacheKey, digest, cacheMinutes);
await globalCache.set(cacheNamespace, cacheKey, digest, cacheMinutes);
return digest;
} catch (err) /* istanbul ignore next */ {
if (err instanceof DatasourceError) {
Expand All @@ -376,7 +377,7 @@ async function getTags(
try {
const cacheNamespace = 'datasource-docker-tags';
const cacheKey = `${registry}:${repository}`;
const cachedResult = await renovateCache.get<string[]>(
const cachedResult = await globalCache.get<string[]>(
cacheNamespace,
cacheKey
);
Expand Down Expand Up @@ -405,7 +406,7 @@ async function getTags(
page += 1;
} while (url && page < 20);
const cacheMinutes = 15;
await renovateCache.set(cacheNamespace, cacheKey, tags, cacheMinutes);
await globalCache.set(cacheNamespace, cacheKey, tags, cacheMinutes);
return tags;
} catch (err) /* istanbul ignore next */ {
if (err instanceof DatasourceError) {
Expand Down Expand Up @@ -485,7 +486,7 @@ async function getLabels(
logger.debug(`getLabels(${registry}, ${repository}, ${tag})`);
const cacheNamespace = 'datasource-docker-labels';
const cacheKey = `${registry}:${repository}:${tag}`;
const cachedResult = await renovateCache.get<Record<string, string>>(
const cachedResult = await globalCache.get<Record<string, string>>(
cacheNamespace,
cacheKey
);
Expand Down Expand Up @@ -542,7 +543,7 @@ async function getLabels(
);
}
const cacheMinutes = 60;
await renovateCache.set(cacheNamespace, cacheKey, labels, cacheMinutes);
await globalCache.set(cacheNamespace, cacheKey, labels, cacheMinutes);
return labels;
} catch (err) {
if (err instanceof DatasourceError) {
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/galaxy/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { Http } from '../../util/http';
import {
DatasourceError,
Expand All @@ -16,7 +17,7 @@ export async function getReleases({
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const cacheNamespace = 'datasource-galaxy';
const cacheKey = lookupName;
const cachedResult = await renovateCache.get<ReleaseResult>(
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
cacheKey
);
Expand Down Expand Up @@ -93,7 +94,7 @@ export async function getReleases({
}
);
const cacheMinutes = 10;
await renovateCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
await globalCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
return result;
} catch (err) {
if (
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/git-refs/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import _simpleGit from 'simple-git/promise';
import * as globalCache from '../../util/cache/global';
import { getDigest, getReleases } from '.';

jest.mock('simple-git/promise');
Expand All @@ -13,7 +14,7 @@ const lsRemote1 = fs.readFileSync(
);

describe('datasource/git-refs', () => {
beforeEach(() => global.renovateCache.rmAll());
beforeEach(() => globalCache.rmAll());
describe('getReleases', () => {
it('returns nil if response is wrong', async () => {
simpleGit.mockReturnValue({
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/git-refs/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import simpleGit from 'simple-git/promise';
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import * as semver from '../../versioning/semver';
import { DigestConfig, GetReleasesConfig, ReleaseResult } from '../common';

Expand All @@ -23,7 +24,7 @@ export async function getRawRefs({
try {
const cacheNamespace = 'git-raw-refs';

const cachedResult = await renovateCache.get<RawRefs[]>(
const cachedResult = await globalCache.get<RawRefs[]>(
cacheNamespace,
lookupName
);
Expand Down Expand Up @@ -67,7 +68,7 @@ export async function getRawRefs({
})
.filter(Boolean)
.filter((ref) => ref.type !== 'pull' && !ref.value.endsWith('^{}'));
await renovateCache.set(cacheNamespace, lookupName, refs, cacheMinutes);
await globalCache.set(cacheNamespace, lookupName, refs, cacheMinutes);
return refs;
} catch (err) {
logger.error({ err }, `Git-Raw-Refs lookup error in ${lookupName}`);
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/git-submodules/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _simpleGit from 'simple-git/promise';
import * as globalCache from '../../util/cache/global';
import { getDigest, getReleases } from '.';

jest.mock('simple-git/promise');
Expand All @@ -8,7 +9,7 @@ const lookupName = 'https://github.com/example/example.git';
const registryUrls = [lookupName, 'master'];

describe('datasource/git-submoduless', () => {
beforeEach(() => global.renovateCache.rmAll());
beforeEach(() => globalCache.rmAll());
describe('getReleases', () => {
it('returns null if response is wrong', async () => {
simpleGit.mockReturnValue({
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/git-submodules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { URL } from 'url';
import Git from 'simple-git/promise';

import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { DigestConfig, GetReleasesConfig, ReleaseResult } from '../common';

export const id = 'git-submodules';
Expand All @@ -12,7 +13,7 @@ export async function getReleases({
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const cacheNamespace = 'datasource-git-submodules';
const cacheKey = `${registryUrls[0]}-${registryUrls[1]}`;
const cachedResult = await renovateCache.get<ReleaseResult>(
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
cacheKey
);
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function getReleases({
],
};
const cacheMinutes = 60;
await renovateCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
await globalCache.set(cacheNamespace, cacheKey, result, cacheMinutes);
return result;
} catch (err) {
logger.debug({ err }, `Git-SubModules lookup error in ${lookupName}`);
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/git-tags/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import _simpleGit from 'simple-git/promise';
import * as globalCache from '../../util/cache/global';
import { getDigest, getReleases } from '.';

jest.mock('simple-git/promise');
Expand All @@ -13,7 +14,7 @@ const lsRemote1 = fs.readFileSync(
);

describe('datasource/git-tags', () => {
beforeEach(() => global.renovateCache.rmAll());
beforeEach(() => globalCache.rmAll());
describe('getReleases', () => {
it('returns nil if response is wrong', async () => {
simpleGit.mockReturnValue({
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/github-releases/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { api } from '../../platform/github/gh-got-wrapper';
import * as globalCache from '../../util/cache/global';

import * as github from '.';

Expand All @@ -9,9 +10,9 @@ jest.mock('../../util/host-rules');
const ghGot: any = api.get;

describe('datasource/github-releases', () => {
beforeEach(() => global.renovateCache.rmAll());
beforeEach(() => globalCache.rmAll());
describe('getReleases', () => {
beforeAll(() => global.renovateCache.rmAll());
beforeAll(() => globalCache.rmAll());
it('returns releases', async () => {
const body = [
{ tag_name: 'a', published_at: '2020-03-09T13:00:00Z' },
Expand Down
5 changes: 3 additions & 2 deletions lib/datasource/github-releases/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '../../logger';
import { api } from '../../platform/github/gh-got-wrapper';
import * as globalCache from '../../util/cache/global';
import { GetReleasesConfig, ReleaseResult } from '../common';

const { get: ghGot } = api;
Expand Down Expand Up @@ -27,7 +28,7 @@ export async function getReleases({
lookupName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
let githubReleases: GithubRelease[];
const cachedResult = await renovateCache.get<ReleaseResult>(
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
repo
);
Expand Down Expand Up @@ -58,6 +59,6 @@ export async function getReleases({
releaseTimestamp: published_at,
}));
const cacheMinutes = 10;
await renovateCache.set(cacheNamespace, repo, dependency, cacheMinutes);
await globalCache.set(cacheNamespace, repo, dependency, cacheMinutes);
return dependency;
}

0 comments on commit 6104c46

Please sign in to comment.