mirror of
https://github.com/DeBrosOfficial/network-ts-sdk.git
synced 2025-12-11 01:58:49 +00:00
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.
This commit is contained in:
parent
cee0cd62a9
commit
2cdb78ee1d
73
src/cache/client.ts
vendored
73
src/cache/client.ts
vendored
@ -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<Map<string, any | null>> {
|
||||
try {
|
||||
if (keys.length === 0) {
|
||||
return new Map();
|
||||
}
|
||||
|
||||
const response = await this.httpClient.post<CacheMultiGetResponse>(
|
||||
"/v1/cache/mget",
|
||||
{
|
||||
dmap,
|
||||
keys,
|
||||
}
|
||||
);
|
||||
|
||||
// Convert array to Map
|
||||
const resultMap = new Map<string, any | null>();
|
||||
|
||||
// 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<string, any | null>();
|
||||
keys.forEach((key) => {
|
||||
resultMap.set(key, null);
|
||||
});
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
// Log and return empty results for other errors
|
||||
const resultMap = new Map<string, any | null>();
|
||||
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
|
||||
*/
|
||||
|
||||
@ -102,6 +102,8 @@ export type {
|
||||
CachePutResponse,
|
||||
CacheDeleteRequest,
|
||||
CacheDeleteResponse,
|
||||
CacheMultiGetRequest,
|
||||
CacheMultiGetResponse,
|
||||
CacheScanRequest,
|
||||
CacheScanResponse,
|
||||
CacheHealthResponse,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user