Skip to content

Commit

Permalink
feat: correct the implementation of mset
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Apr 18, 2024
1 parent 6100812 commit 916853d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 49 deletions.
57 changes: 13 additions & 44 deletions src/momento-redis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,12 @@ 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][]]
...args: [
key: RedisKey,
value: string | Buffer | number,
...keyValues: (RedisKey | string | Buffer | number)[]
]
): Promise<'OK'>;

mget(
Expand Down Expand Up @@ -620,47 +617,19 @@ 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][]]
...args: [
key: RedisKey,
value: string | Buffer | number,
...keyValues: (RedisKey | string | Buffer | number)[]
]
): 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);
for (let i = 0; i < args.length; i += 2) {
await this.set(args[i] as RedisKey, args[i + 1]);
}
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)[]> {
Expand Down
21 changes: 16 additions & 5 deletions test/multiple-set-get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@ import {SetupIntegrationTest} from './integration-setup';
const {client} = SetupIntegrationTest();

describe('multiple get and set', () => {
it('happy path multiple set', async () => {
it('happy path multiple set and get', 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],
]);
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]);
});

it('should return null for keys that do not exist', async () => {
const key1 = v4();
const key2 = v4();
const value = v4();

// Set a key and value
const resp = await client.mset(key1, value);
expect(resp).toEqual('OK');

// Get multiple keys
const getResp = await client.mget(key1, key2);
expect(getResp).toEqual([value, null]);
});
});

0 comments on commit 916853d

Please sign in to comment.