Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: run cache -> mem cache #6582

Merged
merged 2 commits into from Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/datasource/index.ts
Expand Up @@ -2,7 +2,7 @@ import is from '@sindresorhus/is';
import _ from 'lodash';
import { logger } from '../logger';
import { ExternalHostError } from '../types/errors/external-host-error';
import * as runCache from '../util/cache/run';
import * as memCache from '../util/cache/memory';
import { clone } from '../util/clone';
import * as allVersioning from '../versioning';
import datasources from './api.generated';
Expand Down Expand Up @@ -190,13 +190,13 @@ function getRawReleases(
config.lookupName +
config.registryUrls;
// By returning a Promise and reusing it, we should only fetch each package at most once
const cachedResult = runCache.get(cacheKey);
const cachedResult = memCache.get(cacheKey);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
const promisedRes = fetchReleases(config);
runCache.set(cacheKey, promisedRes);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/datasource/packagist/index.ts
Expand Up @@ -3,8 +3,8 @@ import URL from 'url';
import pAll from 'p-all';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as memCache from '../../util/cache/memory';
import * as packageCache from '../../util/cache/package';
import * as runCache from '../../util/cache/run';
import * as hostRules from '../../util/host-rules';
import { Http, HttpOptions } from '../../util/http';
import { GetReleasesConfig, ReleaseResult } from '../common';
Expand Down Expand Up @@ -221,13 +221,13 @@ async function getAllPackages(regUrl: string): Promise<AllPackages | null> {

function getAllCachedPackages(regUrl: string): Promise<AllPackages | null> {
const cacheKey = `packagist-${regUrl}`;
const cachedResult = runCache.get(cacheKey);
const cachedResult = memCache.get(cacheKey);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
const promisedRes = getAllPackages(regUrl);
runCache.set(cacheKey, promisedRes);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/manager/bundler/artifacts.ts
Expand Up @@ -3,7 +3,7 @@ import { BUNDLER_INVALID_CREDENTIALS } from '../../constants/error-messages';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { HostRule } from '../../types';
import { get, set } from '../../util/cache/run';
import * as memCache from '../../util/cache/memory';
import { ExecOptions, exec } from '../../util/exec';
import {
deleteLocalFile,
Expand Down Expand Up @@ -74,7 +74,7 @@ export async function updateArtifacts(
} = updateArtifact;
const { compatibility = {} } = config;
logger.debug(`bundler.updateArtifacts(${packageFileName})`);
const existingError = get<string>('bundlerArtifactsError');
const existingError = memCache.get<string>('bundlerArtifactsError');
// istanbul ignore if
if (existingError) {
logger.debug('Aborting Bundler artifacts due to previous failed attempt');
Expand Down Expand Up @@ -182,7 +182,7 @@ export async function updateArtifacts(
'Gemfile.lock update failed due to missing credentials - skipping branch'
);
// Do not generate these PRs because we don't yet support Bundler authentication
set('bundlerArtifactsError', BUNDLER_INVALID_CREDENTIALS);
memCache.set('bundlerArtifactsError', BUNDLER_INVALID_CREDENTIALS);
throw new Error(BUNDLER_INVALID_CREDENTIALS);
}
const resolveMatchRe = new RegExp('\\s+(.*) was resolved to', 'g');
Expand Down
18 changes: 18 additions & 0 deletions lib/util/cache/memory/index.spec.ts
@@ -0,0 +1,18 @@
import * as memCache from '.';

describe('getRepoCache', () => {
it('returns undefined if not init', () => {
expect(memCache.get('key1')).toBeUndefined();
});
it('sets and gets repo cache', () => {
memCache.init();
memCache.set('key2', 'value');
expect(memCache.get('key2')).toEqual('value');
});
it('resets', () => {
memCache.init();
memCache.set('key3', 'value');
memCache.reset();
expect(memCache.get('key3')).toBeUndefined();
});
});
File renamed without changes.
10 changes: 5 additions & 5 deletions lib/util/cache/package/index.ts
@@ -1,5 +1,5 @@
import { RenovateConfig } from '../../../config/common';
import * as runCache from '../run';
import * as memCache from '../memory';
import { PackageCache } from './common';
import * as fileCache from './file';
import * as redisCache from './redis';
Expand All @@ -15,10 +15,10 @@ export function get<T = any>(namespace: string, key: string): Promise<T> {
return undefined;
}
const globalKey = getGlobalKey(namespace, key);
if (!runCache.get(globalKey)) {
runCache.set(globalKey, cacheProxy.get(namespace, key));
if (!memCache.get(globalKey)) {
memCache.set(globalKey, cacheProxy.get(namespace, key));
}
return runCache.get(globalKey);
return memCache.get(globalKey);
}

export function set(
Expand All @@ -31,7 +31,7 @@ export function set(
return undefined;
}
const globalKey = getGlobalKey(namespace, key);
runCache.set(globalKey, value);
memCache.set(globalKey, value);
return cacheProxy.set(namespace, key, value, minutes);
}

Expand Down
18 changes: 0 additions & 18 deletions lib/util/cache/run.spec.ts

This file was deleted.

10 changes: 5 additions & 5 deletions lib/util/http/index.ts
Expand Up @@ -2,7 +2,7 @@ import crypto from 'crypto';
import URL from 'url';
import got from 'got';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as runCache from '../cache/run';
import * as memCache from '../cache/memory';
import { clone } from '../clone';
import { applyAuthorization } from './auth';
import { applyHostRules } from './host-rules';
Expand Down Expand Up @@ -115,7 +115,7 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
.digest('hex');
if (options.method === 'get' && options.useCache !== false) {
// return from cache if present
const cachedRes = runCache.get(cacheKey);
const cachedRes = memCache.get(cacheKey);
// istanbul ignore if
if (cachedRes) {
return resolveResponse<T>(cachedRes, options);
Expand All @@ -124,16 +124,16 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
const startTime = Date.now();
const promisedRes = got(url, options);
if (options.method === 'get') {
runCache.set(cacheKey, promisedRes); // always set if it's a get
memCache.set(cacheKey, promisedRes); // always set if it's a get
}
const res = await resolveResponse<T>(promisedRes, options);
const httpRequests = runCache.get('http-requests') || [];
const httpRequests = memCache.get('http-requests') || [];
httpRequests.push({
method: options.method,
url,
duration: Date.now() - startTime,
});
runCache.set('http-requests', httpRequests);
memCache.set('http-requests', httpRequests);
return res;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/workers/pr/changelog/release-notes.ts
Expand Up @@ -4,8 +4,8 @@ import { linkify } from 'linkify-markdown';
import MarkdownIt from 'markdown-it';

import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import * as packageCache from '../../../util/cache/package';
import * as runCache from '../../../util/cache/run';
import { GithubHttp } from '../../../util/http/github';
import { GitlabHttp } from '../../../util/http/gitlab';
import { ChangeLogNotes, ChangeLogResult } from './common';
Expand Down Expand Up @@ -83,13 +83,13 @@ export function getCachedReleaseList(
repository: string
): Promise<ChangeLogNotes[]> {
const cacheKey = `getReleaseList-${apiBaseUrl}-${repository}`;
const cachedResult = runCache.get(cacheKey);
const cachedResult = memCache.get(cacheKey);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
const promisedRes = getReleaseList(apiBaseUrl, repository);
runCache.set(cacheKey, promisedRes);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}

Expand Down Expand Up @@ -254,13 +254,13 @@ export async function getReleaseNotesMdFile(
apiBaseUrl: string
): Promise<{ changelogFile: string; changelogMd: string }> | null {
const cacheKey = `getReleaseNotesMdFile-${repository}-${apiBaseUrl}`;
const cachedResult = runCache.get(cacheKey);
const cachedResult = memCache.get(cacheKey);
// istanbul ignore if
if (cachedResult !== undefined) {
return cachedResult;
}
const promisedRes = getReleaseNotesMdFileInner(repository, apiBaseUrl);
runCache.set(cacheKey, promisedRes);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/workers/pr/changelog/source-github.ts
Expand Up @@ -2,8 +2,8 @@ import URL from 'url';
import { PLATFORM_TYPE_GITHUB } from '../../../constants/platforms';
import { Release } from '../../../datasource';
import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import * as packageCache from '../../../util/cache/package';
import * as runCache from '../../../util/cache/run';
import * as hostRules from '../../../util/host-rules';
import { GithubHttp } from '../../../util/http/github';
import * as allVersioning from '../../../versioning';
Expand Down Expand Up @@ -47,13 +47,13 @@ async function getTags(
repository: string
): Promise<string[]> {
const cacheKey = `getTags-${endpoint}-${repository}`;
const cachedResult = runCache.get(cacheKey);
const cachedResult = memCache.get(cacheKey);
// istanbul ignore if
if (cachedResult !== undefined) {
return cachedResult;
}
const promisedRes = getTagsInner(endpoint, repository);
runCache.set(cacheKey, promisedRes);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/workers/pr/changelog/source-gitlab.ts
@@ -1,8 +1,8 @@
import URL from 'url';
import { Release } from '../../../datasource';
import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import * as packageCache from '../../../util/cache/package';
import * as runCache from '../../../util/cache/run';
import { GitlabHttp } from '../../../util/http/gitlab';
import { regEx } from '../../../util/regex';
import * as allVersioning from '../../../versioning';
Expand Down Expand Up @@ -52,13 +52,13 @@ async function getTags(
repository: string
): Promise<string[]> {
const cacheKey = `getTags-${endpoint}-${versionScheme}-${repository}`;
const cachedResult = runCache.get(cacheKey);
const cachedResult = memCache.get(cacheKey);
// istanbul ignore if
if (cachedResult !== undefined) {
return cachedResult;
}
const promisedRes = getTagsInner(endpoint, versionScheme, repository);
runCache.set(cacheKey, promisedRes);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/init/index.ts
@@ -1,7 +1,7 @@
import { RenovateConfig } from '../../../config';
import { logger } from '../../../logger';
import { platform } from '../../../platform';
import * as runCache from '../../../util/cache/run';
import * as memCache from '../../../util/cache/memory';
import { checkIfConfigured } from '../configured';
import { checkOnboardingBranch } from '../onboarding/branch';
import { initApis } from './apis';
Expand All @@ -11,7 +11,7 @@ import { detectSemanticCommits } from './semantic';
import { detectVulnerabilityAlerts } from './vulnerability';

export async function initRepo(input: RenovateConfig): Promise<RenovateConfig> {
runCache.init();
memCache.init();
let config: RenovateConfig = {
...input,
errors: [],
Expand Down
8 changes: 4 additions & 4 deletions lib/workers/repository/stats.spec.ts
@@ -1,14 +1,14 @@
import * as runCache_ from '../../util/cache/run';
import * as memCache_ from '../../util/cache/memory';
import { printRequestStats } from './stats';

jest.mock('../../util/cache/run');
jest.mock('../../util/cache/memory');

const runCache: any = runCache_ as any;
const memCache: any = memCache_ as any;

describe('workers/repository/stats', () => {
describe('printRequestStats()', () => {
it('runs', () => {
runCache.get = jest.fn(() => [
memCache.get = jest.fn(() => [
{
method: 'get',
url: 'https://api.github.com/api/v3/user',
Expand Down
4 changes: 2 additions & 2 deletions lib/workers/repository/stats.ts
@@ -1,9 +1,9 @@
import URL from 'url';
import { logger } from '../../logger';
import * as runCache from '../../util/cache/run';
import * as memCache from '../../util/cache/memory';

export function printRequestStats(): void {
const httpRequests = runCache.get('http-requests');
const httpRequests = memCache.get('http-requests');
if (!httpRequests) {
return;
}
Expand Down