Skip to content

Commit

Permalink
refactor: Extract utilities for package cache key manipulation (#30897)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Aug 20, 2024
1 parent 94f9d76 commit beb97d5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/util/cache/package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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> {
Expand Down
11 changes: 11 additions & 0 deletions lib/util/cache/package/key.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
});
11 changes: 11 additions & 0 deletions lib/util/cache/package/key.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
2 changes: 2 additions & 0 deletions lib/util/cache/package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,5 @@ export type PackageCacheNamespace =
| 'merge-confidence'
| 'preset'
| 'url-sha256';

export type CombinedKey = `global%%${PackageCacheNamespace}%%${string}`;

0 comments on commit beb97d5

Please sign in to comment.