From 610081260ac46b1844c2e43e4dd6fd484cdd4fbb Mon Sep 17 00:00:00 2001 From: rishtigupta Date: Thu, 18 Apr 2024 14:53:38 -0700 Subject: [PATCH] feat: add mset and mget --- src/momento-redis-adapter.ts | 66 +++++++++++++++++++++++++++++++++++ test/multiple-set-get.test.ts | 24 +++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 test/multiple-set-get.test.ts diff --git a/src/momento-redis-adapter.ts b/src/momento-redis-adapter.ts index bc3ddd3..a87190f 100644 --- a/src/momento-redis-adapter.ts +++ b/src/momento-redis-adapter.ts @@ -160,6 +160,21 @@ export interface MomentoIORedis { ...args: [key: RedisKey, ...fields: (string | Buffer)[]] ): Promise; + // mset( + // ...args: [...[key: RedisKey, value: string | number | Buffer][]] + // ): Promise<'OK'>; + // mset( + // ...args: [key: RedisKey, value: string | number | Buffer][] + // ): Promise<'OK'>; + + mset( + ...args: [[key: RedisKey, value: string | number | Buffer][]] + ): Promise<'OK'>; + + mget( + ...args: [key: RedisKey, ...keys: RedisKey[]] + ): Promise<(string | null)[]>; + flushdb(): Promise<'OK'>; flushdb(async: 'ASYNC'): Promise<'OK'>; flushdb(sync: 'SYNC'): Promise<'OK'>; @@ -605,6 +620,57 @@ export class MomentoRedisAdapter } } + // async mset( + // ...args: [...[key: RedisKey, value: string | number | Buffer][]] + // ): Promise<'OK'> { + // const keyValue = args.flat(); + // for (let i = 0; i < keyValue.length; i += 2) { + // await this.set(keyValue[i] as RedisKey, keyValue[i + 1]); + // } + // return 'OK'; + // } + + // async mset( + // ...args: [[key: RedisKey, value: string | number | Buffer][]] + // ): Promise<'OK'> { + // const keyValuePairs = args.flat(); + // const promises = keyValuePairs.map(([key, value]) => { + // return this.set(key, value); + // }); + // await Promise.all(promises); + // return 'OK'; + // } + + async mset( + ...args: [[key: RedisKey, value: string | number | Buffer][]] + ): Promise<'OK'> { + const keyValuePairs = args[0]; // Unwrap the array containing key-value pairs + const promises = keyValuePairs.map(([key, value]) => { + return this.set(key, value); // Call the set method for each key-value pair + }); + await Promise.all(promises); + return 'OK'; + } + + // async mset( + // ...args: [key: RedisKey, value: string | number | Buffer][] + // ): Promise<'OK'> { + // for (const [key, value] of args) { + // await this.set(key, value); + // } + // return 'OK'; + // } + + async mget( + ...args: [key: RedisKey, ...keys: RedisKey[]] + ): Promise<(string | null)[]> { + const promises: Promise[] = []; + args.forEach(key => { + promises.push(this.get(key)); + }); + return await Promise.all(promises); + } + async ttl(key: RedisKey): Promise { const rsp = await this.momentoClient.itemGetTtl(this.cacheName, key); if (rsp instanceof CacheItemGetTtl.Hit) { diff --git a/test/multiple-set-get.test.ts b/test/multiple-set-get.test.ts new file mode 100644 index 0000000..7bf3d3b --- /dev/null +++ b/test/multiple-set-get.test.ts @@ -0,0 +1,24 @@ +import {v4} from 'uuid'; +import {SetupIntegrationTest} from './integration-setup'; + +const {client} = SetupIntegrationTest(); + +describe('multiple get and set', () => { + it('happy path multiple set', async () => { + const key1 = v4(); + const key2 = v4(); + const value1 = v4(); + const value2 = v4(); + + // Set multiple keys and values + const resp = await client.mset([ + [key1, value1], + [key2, value2], + ]); + expect(resp).toEqual('OK'); + + // Get multiple keys + const getResp = await client.mget(key1, key2); + expect(getResp).toEqual([value1, value2]); + }); +});