From 013c63664b0d83fa8d493edc21d612de074566f1 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Wed, 24 Jun 2020 22:33:27 +0200 Subject: [PATCH] refactor: remove global.renovateCache (#6579) --- lib/globals.d.ts | 18 ------------------ lib/util/cache/global/common.ts | 10 ++++++++++ lib/util/cache/global/file.spec.ts | 23 ++++++++++++----------- lib/util/cache/global/file.ts | 26 +++++++++++++++++--------- lib/util/cache/global/index.spec.ts | 2 -- lib/util/cache/global/index.ts | 19 +++++++++++++++---- lib/util/cache/global/redis.ts | 11 ++++++++--- 7 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 lib/util/cache/global/common.ts diff --git a/lib/globals.d.ts b/lib/globals.d.ts index 79df2b6aedf0cf..2eeb507f25fcec 100644 --- a/lib/globals.d.ts +++ b/lib/globals.d.ts @@ -2,20 +2,6 @@ * This file should be removed in future. */ -declare namespace Renovate { - interface Cache { - get(namespace: string, key: string): Promise; - rm(namespace: string, key: string): Promise; - - set( - namespace: string, - key: string, - value: T, - ttlMinutes?: number - ): Promise; - } -} - declare interface Error { configFile?: string; @@ -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; diff --git a/lib/util/cache/global/common.ts b/lib/util/cache/global/common.ts new file mode 100644 index 00000000000000..0742921000bc7c --- /dev/null +++ b/lib/util/cache/global/common.ts @@ -0,0 +1,10 @@ +export interface GlobalCache { + get(namespace: string, key: string): Promise; + + set( + namespace: string, + key: string, + value: T, + ttlMinutes?: number + ): Promise; +} diff --git a/lib/util/cache/global/file.spec.ts b/lib/util/cache/global/file.spec.ts index 26be177a9c8649..d4d26fcac869ce 100644 --- a/lib/util/cache/global/file.spec.ts +++ b/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(); }); }); diff --git a/lib/util/cache/global/file.ts b/lib/util/cache/global/file.ts index 0fcbda3010bd78..e517e612b1320b 100644 --- a/lib/util/cache/global/file.ts +++ b/lib/util/cache/global/file.ts @@ -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 { 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(namespace: string, key: string): Promise { +export async function get( + namespace: string, + key: string +): Promise { + 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)) { @@ -31,15 +37,18 @@ async function get(namespace: string, key: string): Promise { return undefined; } -async function set( +export async function set( namespace: string, key: string, value: unknown, ttlMinutes = 5 ): Promise { + if (!cacheFileName) { + return; + } logger.trace({ namespace, key, ttlMinutes }, 'Saving cached value'); await cacache.put( - renovateCache, + cacheFileName, getKey(namespace, key), JSON.stringify({ value, @@ -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); } diff --git a/lib/util/cache/global/index.spec.ts b/lib/util/cache/global/index.spec.ts index d88d80496a5e35..c91c9d1abeca27 100644 --- a/lib/util/cache/global/index.spec.ts +++ b/lib/util/cache/global/index.spec.ts @@ -10,7 +10,6 @@ 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) @@ -18,7 +17,6 @@ describe(getName(__filename), () => { 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) diff --git a/lib/util/cache/global/index.ts b/lib/util/cache/global/index.ts index 055f01cae922ab..0fb6a2819aa11f 100644 --- a/lib/util/cache/global/index.ts +++ b/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(namespace: string, key: string): Promise { - 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); } @@ -24,19 +27,27 @@ export function set( value: any, minutes: number ): Promise { - 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, + }; } } diff --git a/lib/util/cache/global/redis.ts b/lib/util/cache/global/redis.ts index bd3406837fe808..b86f7e05b82246 100644 --- a/lib/util/cache/global/redis.ts +++ b/lib/util/cache/global/redis.ts @@ -22,7 +22,13 @@ async function rm(namespace: string, key: string): Promise { await client?.del(getKey(namespace, key)); } -async function get(namespace: string, key: string): Promise { +export async function get( + namespace: string, + key: string +): Promise { + if (!client) { + return undefined; + } logger.trace(`cache.get(${namespace}, ${key})`); try { const res = await client?.get(getKey(namespace, key)); @@ -41,7 +47,7 @@ async function get(namespace: string, key: string): Promise { return undefined; } -async function set( +export async function set( namespace: string, key: string, value: unknown, @@ -73,5 +79,4 @@ export function init(url: string): void { return Math.min(options.attempt * 100, 3000); }, }); - global.renovateCache = { get, set, rm }; }