From 2cdb78ee1d0a8387447a233f1510c3d4d5e22444 Mon Sep 17 00:00:00 2001 From: anonpenguin23 Date: Fri, 7 Nov 2025 06:47:42 +0200 Subject: [PATCH] Add multiGet functionality to CacheClient for batch retrieval of cache values - Introduced CacheMultiGetRequest and CacheMultiGetResponse interfaces to define the structure for multi-get operations. - Implemented multiGet method in CacheClient to retrieve multiple cache values in a single request, handling cache misses gracefully. - Enhanced error handling to manage 404 errors when the backend does not support multiGet, returning null for missing keys. --- src/cache/client.ts | 73 +++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 2 ++ 2 files changed, 75 insertions(+) diff --git a/src/cache/client.ts b/src/cache/client.ts index 89b05f2..46b8012 100644 --- a/src/cache/client.ts +++ b/src/cache/client.ts @@ -36,6 +36,19 @@ export interface CacheDeleteResponse { dmap: string; } +export interface CacheMultiGetRequest { + dmap: string; + keys: string[]; +} + +export interface CacheMultiGetResponse { + results: Array<{ + key: string; + value: any; + }>; + dmap: string; +} + export interface CacheScanRequest { dmap: string; match?: string; // Optional regex pattern @@ -118,6 +131,66 @@ export class CacheClient { }); } + /** + * Get multiple values from cache in a single request + * Returns a map of key -> value (or null if not found) + * Gracefully handles 404 errors (endpoint not implemented) by returning empty results + */ + async multiGet( + dmap: string, + keys: string[] + ): Promise> { + try { + if (keys.length === 0) { + return new Map(); + } + + const response = await this.httpClient.post( + "/v1/cache/mget", + { + dmap, + keys, + } + ); + + // Convert array to Map + const resultMap = new Map(); + + // First, mark all keys as null (cache miss) + keys.forEach((key) => { + resultMap.set(key, null); + }); + + // Then, update with found values + if (response.results) { + response.results.forEach(({ key, value }) => { + resultMap.set(key, value); + }); + } + + return resultMap; + } catch (error) { + // Handle 404 errors silently (endpoint not implemented on backend) + // This is expected behavior when the backend doesn't support multiGet yet + if (error instanceof SDKError && error.httpStatus === 404) { + // Return map with all nulls silently - caller can fall back to individual gets + const resultMap = new Map(); + keys.forEach((key) => { + resultMap.set(key, null); + }); + return resultMap; + } + + // Log and return empty results for other errors + const resultMap = new Map(); + keys.forEach((key) => { + resultMap.set(key, null); + }); + console.error(`[CacheClient] Error in multiGet for ${dmap}:`, error); + return resultMap; + } + } + /** * Scan keys in a distributed map, optionally matching a regex pattern */ diff --git a/src/index.ts b/src/index.ts index 3aad4cf..d224e86 100644 --- a/src/index.ts +++ b/src/index.ts @@ -102,6 +102,8 @@ export type { CachePutResponse, CacheDeleteRequest, CacheDeleteResponse, + CacheMultiGetRequest, + CacheMultiGetResponse, CacheScanRequest, CacheScanResponse, CacheHealthResponse,