From beb97d5510d7bd67e7e75bbf6caec3e834efae62 Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Tue, 20 Aug 2024 01:57:43 -0300
Subject: [PATCH] refactor: Extract utilities for package cache key
 manipulation (#30897)

---
 lib/util/cache/package/index.ts    | 15 ++++++---------
 lib/util/cache/package/key.spec.ts | 11 +++++++++++
 lib/util/cache/package/key.ts      | 11 +++++++++++
 lib/util/cache/package/types.ts    |  2 ++
 4 files changed, 30 insertions(+), 9 deletions(-)
 create mode 100644 lib/util/cache/package/key.spec.ts
 create mode 100644 lib/util/cache/package/key.ts

diff --git a/lib/util/cache/package/index.ts b/lib/util/cache/package/index.ts
index 815d56d646..600f569eab 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 0000000000..9925dd56ef
--- /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 0000000000..e862cf704e
--- /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 180b5d6280..ae3e9ddc24 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}`;
-- 
GitLab