Skip to content

Commit

Permalink
feat: add mset and mget
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Apr 18, 2024
1 parent 8f34f5c commit 6100812
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/momento-redis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ export interface MomentoIORedis {
...args: [key: RedisKey, ...fields: (string | Buffer)[]]
): Promise<number>;

// 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'>;
Expand Down Expand Up @@ -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<string | null>[] = [];
args.forEach(key => {
promises.push(this.get(key));
});
return await Promise.all(promises);
}

async ttl(key: RedisKey): Promise<number | null> {
const rsp = await this.momentoClient.itemGetTtl(this.cacheName, key);
if (rsp instanceof CacheItemGetTtl.Hit) {
Expand Down
24 changes: 24 additions & 0 deletions test/multiple-set-get.test.ts
Original file line number Diff line number Diff line change
@@ -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]);
});
});

0 comments on commit 6100812

Please sign in to comment.