Skip to content

Commit

Permalink
feat: add incr api
Browse files Browse the repository at this point in the history
  • Loading branch information
rishtigupta committed Apr 16, 2024
1 parent 6bd5b7b commit 6020f4b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/momento-redis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CacheDictionarySetFields,
CacheFlush,
CacheGet,
CacheIncrement,
CacheItemGetTtl,
CacheSet,
CacheSetIfAbsent,
Expand Down Expand Up @@ -91,6 +92,8 @@ export interface MomentoIORedis {
nx: 'NX'
): Promise<'OK' | null>;

incr(key: RedisKey): Promise<number | string>;

ttl(key: RedisKey): Promise<number | null>;

pttl(key: RedisKey): Promise<number | null>;
Expand Down Expand Up @@ -395,6 +398,19 @@ export class MomentoRedisAdapter
return null;
}

async incr(key: RedisKey): Promise<number | string> {
const rsp = await this.momentoClient.increment(this.cacheName, key);
if (rsp instanceof CacheIncrement.Success) {
return rsp.value();
} else if (rsp instanceof CacheIncrement.Error) {
this.emitError('incr', rsp.message(), rsp.errorCode());
return rsp.message();
} else {
this.emitError('incr', `unexpected-response ${rsp.toString()}`);
return rsp.toString();
}
}

async hset(
...args: [
RedisKey,
Expand Down
70 changes: 70 additions & 0 deletions test/increment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {SetupIntegrationTest} from './integration-setup';
import {v4} from 'uuid';

const {client} = SetupIntegrationTest();

describe('increment', () => {
it('should increment the value of the key by 1 if the key exists', async () => {
const key = v4();
const value = 5;

// Set initial key value
await client.set(key, value);

// Increment the value of the key
const incrResp = await client.incr(key);
expect(incrResp).toBe(value + 1);
});

it('should increment the value of the key to 1 if the key does not exists', async () => {
const key = v4();

// Increment the value of the key that is not set
const incrResp = await client.incr(key);
expect(incrResp).toBe(1);
});

it('should error out if the key contains a value of wrong type or contains a string that can be represented as integer', async () => {
const key = v4();
const value = 'monkey';

// Set initial key value
await client.set(key, value);

// Increment the value of the key that is not set
try {
await client.incr(key);
} catch (error) {
if (process.env.MOMENTO_ENABLED === 'true') {
const momentoError = error as {
code: string;
context: {code: string; msg: string; op: string; platform: string};
};
expect(momentoError.code).toBe('ERR_UNHANDLED_ERROR');
expect(momentoError.context.code).toBe('FAILED_PRECONDITION_ERROR');
expect(momentoError.context.msg).toBe(
"System is not in a state required for the operation's execution: 9 FAILED_PRECONDITION: failed to parse value into long"
);
expect(momentoError.context.op).toBe('incr');
expect(momentoError.context.platform).toBe('momento');
} else {
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe(
'ERR value is not an integer or out of range'
);
}
}
});

it('should increment the value of key that contains a string that can be represented as integer', async () => {
const key = v4();
const value = '10';

// Set initial key value
await client.set(key, value);

// Increment the value of the key that is not set
const incrResp = await client.incr(key);
expect(incrResp).toBe(11);
});
});
5 changes: 5 additions & 0 deletions test/integration-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ function setupIntegrationTestWithMomento() {
}
);

// If using Momento with Redis backend, close the Redis connection
afterAll(async () => {
await momentoNodeRedisClient.quit();
});

return {client: momentoNodeRedisClient};
}

Expand Down

0 comments on commit 6020f4b

Please sign in to comment.