Skip to content

Commit

Permalink
feat(internal): use runtime cache for buffering global cache requests (
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 11, 2020
1 parent 29fe28c commit 92009c6
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 141 deletions.
2 changes: 2 additions & 0 deletions lib/datasource/github-tags/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { api } from '../../platform/github/gh-got-wrapper';
import * as globalCache from '../../util/cache/global';
import * as runCache from '../../util/cache/run';
import * as _hostRules from '../../util/host-rules';
import * as github from '.';

Expand All @@ -16,6 +17,7 @@ describe('datasource/github-tags', () => {
beforeEach(() => {
jest.resetAllMocks();
hostRules.hosts = jest.fn(() => []);
runCache.clear();
return globalCache.rmAll();
});
it('returns null if no token', async () => {
Expand Down
118 changes: 0 additions & 118 deletions lib/datasource/helm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,123 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`datasource/helm getReleases returns list of versions for normal response if index.yaml is cached 1`] = `
Object {
"homepage": "https://www.getambassador.io/",
"name": "ambassador",
"releases": Array [
Object {
"releaseTimestamp": "2019-06-02T08:56:36.119Z",
"version": "2.7.0",
},
Object {
"releaseTimestamp": "2019-05-31T18:57:01.540Z",
"version": "2.6.2",
},
Object {
"releaseTimestamp": "2019-05-27T23:28:25.443Z",
"version": "2.6.1",
},
Object {
"releaseTimestamp": "2019-05-24T10:56:37.781Z",
"version": "2.6.0",
},
Object {
"releaseTimestamp": "2019-05-20T23:56:32.309Z",
"version": "2.5.1",
},
Object {
"releaseTimestamp": "2019-05-16T21:56:48.001Z",
"version": "2.5.0",
},
Object {
"releaseTimestamp": "2019-05-15T07:56:25.942Z",
"version": "2.4.1",
},
Object {
"releaseTimestamp": "2019-05-09T17:29:35.612Z",
"version": "2.4.0",
},
Object {
"releaseTimestamp": "2019-05-08T19:26:25.925Z",
"version": "2.3.1",
},
Object {
"releaseTimestamp": "2019-05-06T21:28:21.342Z",
"version": "2.3.0",
},
Object {
"releaseTimestamp": "2019-05-06T20:56:39.732Z",
"version": "2.2.5",
},
Object {
"releaseTimestamp": "2019-05-01T19:56:23.829Z",
"version": "2.2.4",
},
Object {
"releaseTimestamp": "2019-05-01T14:27:55.475Z",
"version": "2.2.3",
},
Object {
"releaseTimestamp": "2019-04-29T20:56:20.987Z",
"version": "2.2.2",
},
Object {
"releaseTimestamp": "2019-04-27T01:56:41.147Z",
"version": "2.2.1",
},
Object {
"releaseTimestamp": "2019-04-23T19:56:09.380Z",
"version": "2.2.0",
},
Object {
"releaseTimestamp": "2019-04-23T14:57:59.089Z",
"version": "2.1.0",
},
Object {
"releaseTimestamp": "2019-04-08T13:26:12.863Z",
"version": "2.0.2",
},
Object {
"releaseTimestamp": "2019-04-06T13:26:04.916Z",
"version": "2.0.1",
},
Object {
"releaseTimestamp": "2019-03-22T14:26:18.094Z",
"version": "2.0.0",
},
Object {
"releaseTimestamp": "2019-03-21T15:25:53.910Z",
"version": "1.1.5",
},
Object {
"releaseTimestamp": "2019-02-24T04:55:38.454Z",
"version": "1.1.4",
},
Object {
"releaseTimestamp": "2019-02-22T16:28:27.398Z",
"version": "1.1.3",
},
Object {
"releaseTimestamp": "2019-02-15T23:56:28.304Z",
"version": "1.1.2",
},
Object {
"releaseTimestamp": "2019-02-14T16:55:51.519Z",
"version": "1.1.1",
},
Object {
"releaseTimestamp": "2019-02-14T15:25:43.743Z",
"version": "1.1.0",
},
Object {
"releaseTimestamp": "2019-02-13T00:56:01.476Z",
"version": "1.0.0",
},
],
"sourceUrl": "https://github.com/datawire/ambassador",
}
`;

exports[`datasource/helm getReleases returns list of versions for normal response if index.yaml is not cached 1`] = `
Object {
"homepage": "https://www.getambassador.io/",
Expand Down
24 changes: 3 additions & 21 deletions lib/datasource/helm/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fs from 'fs';
import * as globalCache from '../../util/cache/global';
import * as runCache from '../../util/cache/run';
import _got from '../../util/got';
import { getReleases, getRepositoryData } from '.';
import { getReleases } from '.';

const got: any = _got;

Expand All @@ -17,6 +18,7 @@ describe('datasource/helm', () => {
describe('getReleases', () => {
beforeEach(() => {
jest.resetAllMocks();
runCache.clear();
return globalCache.rmAll();
});
it('returns null if lookupName was not provided', async () => {
Expand Down Expand Up @@ -137,25 +139,5 @@ describe('datasource/helm', () => {
expect(releases).not.toBeNull();
expect(releases).toMatchSnapshot();
});
it('returns list of versions for normal response if index.yaml is cached', async () => {
const repository = 'example-repository.com';
const cacheNamespace = 'datasource-helm';
const cacheKey = repository;
const cacheMinutes = 10;
got.mockReturnValueOnce({ body: indexYaml });
const repositoryData = await getRepositoryData(repository);
await globalCache.set(
cacheNamespace,
cacheKey,
repositoryData,
cacheMinutes
);
const releases = await getReleases({
lookupName: 'ambassador',
registryUrls: [repository],
});
expect(releases).not.toBeNull();
expect(releases).toMatchSnapshot();
});
});
});
1 change: 1 addition & 0 deletions lib/datasource/helm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function getRepositoryData(
const cacheNamespace = 'datasource-helm';
const cacheKey = repository;
const cachedIndex = await globalCache.get(cacheNamespace, cacheKey);
// istanbul ignore if
if (cachedIndex) {
return cachedIndex;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/datasource/nuget/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import * as globalCache from '../../util/cache/global';
import * as runCache from '../../util/cache/run';
import _got from '../../util/got';
import * as _hostRules from '../../util/host-rules';
import * as nuget from '.';
Expand Down Expand Up @@ -83,7 +84,10 @@ const configV3NotNugetOrg = {
};

describe('datasource/nuget', () => {
beforeEach(() => globalCache.rmAll());
beforeEach(() => {
runCache.clear();
return globalCache.rmAll();
});
describe('getReleases', () => {
beforeEach(() => {
jest.resetAllMocks();
Expand Down
2 changes: 2 additions & 0 deletions lib/datasource/orb/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as globalCache from '../../util/cache/global';
import * as runCache from '../../util/cache/run';
import _got from '../../util/got';
import * as datasource from '.';

Expand Down Expand Up @@ -31,6 +32,7 @@ describe('datasource/orb', () => {
describe('getReleases', () => {
beforeEach(() => {
jest.clearAllMocks();
runCache.clear();
return globalCache.rmAll();
});
it('returns null for empty result', async () => {
Expand Down
2 changes: 2 additions & 0 deletions lib/datasource/ruby-version/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import * as globalCache from '../../util/cache/global';
import * as runCache from '../../util/cache/run';
import _got from '../../util/got';
import { getReleases } from '.';

Expand All @@ -15,6 +16,7 @@ const rubyReleasesHtml = fs.readFileSync(
describe('datasource/gradle', () => {
describe('getReleases', () => {
beforeEach(() => {
runCache.clear();
return globalCache.rmAll();
});
it('parses real data', async () => {
Expand Down
14 changes: 13 additions & 1 deletion lib/util/cache/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import * as runCache from './run';

function getGlobalKey(namespace: string, key: string): string {
return `global%%${namespace}%%${key}`;
}

export function get<T = any>(namespace: string, key: string): Promise<T> {
return renovateCache.get(namespace, key);
const globalKey = getGlobalKey(namespace, key);
if (!runCache.get(globalKey)) {
runCache.set(globalKey, renovateCache.get(namespace, key));
}
return runCache.get(globalKey);
}

export function set(
Expand All @@ -8,6 +18,8 @@ export function set(
value: any,
minutes: number
): Promise<void> {
const globalKey = getGlobalKey(namespace, key);
runCache.set(globalKey, value);
return renovateCache.set(namespace, key, value, minutes);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/workers/pr/changelog/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mocked, partial } from '../../../../test/util';
import { PLATFORM_TYPE_GITHUB } from '../../../constants/platforms';
import { api } from '../../../platform/github/gh-got-wrapper';
import * as globalCache from '../../../util/cache/global';
import * as runCache from '../../../util/cache/run';
import * as hostRules from '../../../util/host-rules';
import * as semverVersioning from '../../../versioning/semver';
import { BranchConfig } from '../../common';
Expand Down Expand Up @@ -44,6 +45,7 @@ describe('workers/pr/changelog', () => {
token: 'abc',
});
await globalCache.rmAll();
runCache.clear();
});
it('returns null if @types', async () => {
expect(
Expand Down

0 comments on commit 92009c6

Please sign in to comment.