diff --git a/lib/util/cache/package/index.ts b/lib/util/cache/package/index.ts index 815d56d646a45de1bb0b0deda647fd37a0920be6..600f569eab3494e1856f6f6ccac62b35aaf1b491 100644 --- a/lib/util/cache/package/index.ts +++ b/lib/util/cache/package/index.ts @@ -2,16 +2,13 @@ import type { AllConfig } from '../../../config/types'; import { PackageCacheStats } from '../../stats'; import * as memCache from '../memory'; import * as fileCache from './file'; +import { getCombinedKey } from './key'; import * as redisCache from './redis'; import { SqlitePackageCache } from './sqlite'; import type { PackageCache, PackageCacheNamespace } from './types'; let cacheProxy: PackageCache | undefined; -function getGlobalKey(namespace: string, key: string): string { - return `global%%${namespace}%%${key}`; -} - export async function get<T = any>( namespace: PackageCacheNamespace, key: string, @@ -20,13 +17,13 @@ export async function get<T = any>( return undefined; } - const globalKey = getGlobalKey(namespace, key); - let p = memCache.get(globalKey); + const combinedKey = getCombinedKey(namespace, key); + let p = memCache.get(combinedKey); if (!p) { p = PackageCacheStats.wrapGet(() => cacheProxy!.get<number[]>(namespace, key), ); - memCache.set(globalKey, p); + memCache.set(combinedKey, p); } const result = await p; @@ -47,9 +44,9 @@ export async function set( cacheProxy!.set(namespace, key, value, minutes), ); - const globalKey = getGlobalKey(namespace, key); + const combinedKey = getCombinedKey(namespace, key); const p = Promise.resolve(value); - memCache.set(globalKey, p); + memCache.set(combinedKey, p); } export async function init(config: AllConfig): Promise<void> { diff --git a/lib/util/cache/package/key.spec.ts b/lib/util/cache/package/key.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..9925dd56ef5765f45db0d19f7581109a326fdadc --- /dev/null +++ b/lib/util/cache/package/key.spec.ts @@ -0,0 +1,11 @@ +import { getCombinedKey } from './key'; + +describe('util/cache/package/key', () => { + describe('getCombinedKey', () => { + it('works', () => { + expect(getCombinedKey('datasource-github-releases', 'foo:bar')).toBe( + 'global%%datasource-github-releases%%foo:bar', + ); + }); + }); +}); diff --git a/lib/util/cache/package/key.ts b/lib/util/cache/package/key.ts new file mode 100644 index 0000000000000000000000000000000000000000..e862cf704ebbface8580248615b0f7f61be0869f --- /dev/null +++ b/lib/util/cache/package/key.ts @@ -0,0 +1,11 @@ +import type { CombinedKey, PackageCacheNamespace } from './types'; + +/** + * Returns the key used by underlying storage implementations + */ +export function getCombinedKey( + namespace: PackageCacheNamespace, + key: string, +): CombinedKey { + return `global%%${namespace}%%${key}`; +} diff --git a/lib/util/cache/package/types.ts b/lib/util/cache/package/types.ts index 180b5d628026c10dcfcc3038658e2a71c4878044..ae3e9ddc240584d19718d6678cb4ec8bdced0691 100644 --- a/lib/util/cache/package/types.ts +++ b/lib/util/cache/package/types.ts @@ -113,3 +113,5 @@ export type PackageCacheNamespace = | 'merge-confidence' | 'preset' | 'url-sha256'; + +export type CombinedKey = `global%%${PackageCacheNamespace}%%${string}`;