Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor: remove global.renovateCache (#6579)
  • Loading branch information
rarkins committed Jun 24, 2020
1 parent e4e76f8 commit 013c636
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 47 deletions.
18 changes: 0 additions & 18 deletions lib/globals.d.ts
Expand Up @@ -2,20 +2,6 @@
* This file should be removed in future.
*/

declare namespace Renovate {
interface Cache {
get<T = any>(namespace: string, key: string): Promise<T>;
rm(namespace: string, key: string): Promise<void>;

set<T = any>(
namespace: string,
key: string,
value: T,
ttlMinutes?: number
): Promise<void>;
}
}

declare interface Error {
configFile?: string;

Expand All @@ -30,14 +16,10 @@ declare namespace NodeJS {
appMode?: boolean;
gitAuthor?: { name: string; email: string };

renovateCache: Renovate.Cache;

trustLevel?: string;
}
}

declare let renovateCache: Renovate.Cache;

// can't use `resolveJsonModule` because it will copy json files and change dist path
declare module '*.json' {
const value: { version: string } & Record<string, any>;
Expand Down
10 changes: 10 additions & 0 deletions lib/util/cache/global/common.ts
@@ -0,0 +1,10 @@
export interface GlobalCache {
get<T = any>(namespace: string, key: string): Promise<T>;

set<T = any>(
namespace: string,
key: string,
value: T,
ttlMinutes?: number
): Promise<void>;
}
23 changes: 12 additions & 11 deletions lib/util/cache/global/file.spec.ts
@@ -1,24 +1,25 @@
import os from 'os';
import { init } from './file';
import { get, init, set } from './file';

describe('lib/util/cache/global/file', () => {
beforeAll(() => {
init(os.tmpdir());
it('returns if uninitiated', async () => {
await set('test', 'key', 1234);
expect(await get('test', 'key')).toBeUndefined();
});

it('gets null', async () => {
expect(
await global.renovateCache.get('test', 'missing-key')
).toBeUndefined();
init(os.tmpdir());
expect(await get('test', 'missing-key')).toBeUndefined();
});

it('sets and gets', async () => {
await global.renovateCache.set('test', 'key', 1234);
expect(await global.renovateCache.get('test', 'key')).toBe(1234);
init(os.tmpdir());
await set('test', 'key', 1234);
expect(await get('test', 'key')).toBe(1234);
});

it('expires', async () => {
await global.renovateCache.set('test', 'key', 1234, -5);
expect(await global.renovateCache.get('test', 'key')).toBeUndefined();
init(os.tmpdir());
await set('test', 'key', 1234, -5);
expect(await get('test', 'key')).toBeUndefined();
});
});
26 changes: 17 additions & 9 deletions lib/util/cache/global/file.ts
Expand Up @@ -7,16 +7,22 @@ function getKey(namespace: string, key: string): string {
return `${namespace}-${key}`;
}

let renovateCache: string;
let cacheFileName: string;

async function rm(namespace: string, key: string): Promise<void> {
logger.trace({ namespace, key }, 'Removing cache entry');
await cacache.rm.entry(renovateCache, getKey(namespace, key));
await cacache.rm.entry(cacheFileName, getKey(namespace, key));
}

async function get<T = never>(namespace: string, key: string): Promise<T> {
export async function get<T = never>(
namespace: string,
key: string
): Promise<T> {
if (!cacheFileName) {
return undefined;
}
try {
const res = await cacache.get(renovateCache, getKey(namespace, key));
const res = await cacache.get(cacheFileName, getKey(namespace, key));
const cachedValue = JSON.parse(res.data.toString());
if (cachedValue) {
if (DateTime.local() < DateTime.fromISO(cachedValue.expiry)) {
Expand All @@ -31,15 +37,18 @@ async function get<T = never>(namespace: string, key: string): Promise<T> {
return undefined;
}

async function set(
export async function set(
namespace: string,
key: string,
value: unknown,
ttlMinutes = 5
): Promise<void> {
if (!cacheFileName) {
return;
}
logger.trace({ namespace, key, ttlMinutes }, 'Saving cached value');
await cacache.put(
renovateCache,
cacheFileName,
getKey(namespace, key),
JSON.stringify({
value,
Expand All @@ -49,7 +58,6 @@ async function set(
}

export function init(cacheDir: string): void {
renovateCache = path.join(cacheDir, '/renovate/renovate-cache-v1');
logger.debug('Initializing Renovate internal cache into ' + renovateCache);
global.renovateCache = global.renovateCache || { get, set, rm };
cacheFileName = path.join(cacheDir, '/renovate/renovate-cache-v1');
logger.debug('Initializing Renovate internal cache into ' + cacheFileName);
}
2 changes: 0 additions & 2 deletions lib/util/cache/global/index.spec.ts
Expand Up @@ -10,15 +10,13 @@ describe(getName(__filename), () => {
expect(await set('test', 'some-key', 'some-value', 5)).toBeUndefined();
});
it('sets and gets file', async () => {
global.renovateCache = { get: jest.fn(), set: jest.fn(), rm: jest.fn() };
init({ cacheDir: 'some-dir' });
expect(
await set('some-namespace', 'some-key', 'some-value', 1)
).toBeUndefined();
expect(await get('some-namespace', 'unknown-key')).toBeUndefined();
});
it('sets and gets redis', async () => {
global.renovateCache = { get: jest.fn(), set: jest.fn(), rm: jest.fn() };
init({ redisUrl: 'some-url' });
expect(
await set('some-namespace', 'some-key', 'some-value', 1)
Expand Down
19 changes: 15 additions & 4 deletions lib/util/cache/global/index.ts
@@ -1,19 +1,22 @@
import { RenovateConfig } from '../../../config/common';
import * as runCache from '../run';
import { GlobalCache } from './common';
import * as fileCache from './file';
import * as redisCache from './redis';

let cacheProxy: GlobalCache;

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

export function get<T = any>(namespace: string, key: string): Promise<T> {
if (!global.renovateCache) {
if (!cacheProxy) {
return undefined;
}
const globalKey = getGlobalKey(namespace, key);
if (!runCache.get(globalKey)) {
runCache.set(globalKey, global.renovateCache.get(namespace, key));
runCache.set(globalKey, cacheProxy.get(namespace, key));
}
return runCache.get(globalKey);
}
Expand All @@ -24,19 +27,27 @@ export function set(
value: any,
minutes: number
): Promise<void> {
if (!global.renovateCache) {
if (!cacheProxy) {
return undefined;
}
const globalKey = getGlobalKey(namespace, key);
runCache.set(globalKey, value);
return global.renovateCache.set(namespace, key, value, minutes);
return cacheProxy.set(namespace, key, value, minutes);
}

export function init(config: RenovateConfig): void {
if (config.redisUrl) {
redisCache.init(config.redisUrl);
cacheProxy = {
get: redisCache.get,
set: redisCache.set,
};
} else {
fileCache.init(config.cacheDir);
cacheProxy = {
get: fileCache.get,
set: fileCache.set,
};
}
}

Expand Down
11 changes: 8 additions & 3 deletions lib/util/cache/global/redis.ts
Expand Up @@ -22,7 +22,13 @@ async function rm(namespace: string, key: string): Promise<void> {
await client?.del(getKey(namespace, key));
}

async function get<T = never>(namespace: string, key: string): Promise<T> {
export async function get<T = never>(
namespace: string,
key: string
): Promise<T> {
if (!client) {
return undefined;
}
logger.trace(`cache.get(${namespace}, ${key})`);
try {
const res = await client?.get(getKey(namespace, key));
Expand All @@ -41,7 +47,7 @@ async function get<T = never>(namespace: string, key: string): Promise<T> {
return undefined;
}

async function set(
export async function set(
namespace: string,
key: string,
value: unknown,
Expand Down Expand Up @@ -73,5 +79,4 @@ export function init(url: string): void {
return Math.min(options.attempt * 100, 3000);
},
});
global.renovateCache = { get, set, rm };
}

0 comments on commit 013c636

Please sign in to comment.