Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support for expire command #35

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/momento-redis-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,23 @@

pexpire(key: RedisKey, milliseconds: number): Promise<number | null>;

pexpire(
key: RedisKey,
milliseconds: number,
nx: 'NX'
): Promise<number | null>;
pexpire(key: RedisKey, milliseconds: number, nx: 'NX'): Promise<number>;

pexpire(
key: RedisKey,
milliseconds: number,
xx: 'XX'
): Promise<number | null>;
pexpire(key: RedisKey, milliseconds: number, xx: 'XX'): Promise<number>;

pexpire(
key: RedisKey,
milliseconds: number,
gt: 'GT'
): Promise<number | null>;
pexpire(key: RedisKey, milliseconds: number, gt: 'GT'): Promise<number>;
eaddingtonwhite marked this conversation as resolved.
Show resolved Hide resolved

pexpire(
key: RedisKey,
milliseconds: number,
lt: 'LT'
): Promise<number | null>;
pexpire(key: RedisKey, milliseconds: number, lt: 'LT'): Promise<number>;

expire(key: RedisKey, seconds: number): Promise<number>;

expire(key: RedisKey, seconds: number, nx: 'NX'): Promise<number>;

expire(key: RedisKey, seconds: number, xx: 'XX'): Promise<number>;

expire(key: RedisKey, seconds: number, gt: 'GT'): Promise<number>;

expire(key: RedisKey, seconds: number, lt: 'LT'): Promise<number>;

del(...args: [...keys: RedisKey[]]): Promise<number>;

Expand Down Expand Up @@ -195,7 +189,7 @@
// TODO compile time checks on what methods are supported by the MomentoIORedis
// TODO interface. We could try defining our own ChainableMomento interface
// TODO instead here potentially
pipeline(commands?: Array<Array<any>>): ChainableCommander;

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 16

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 16

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 16

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 16

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 18

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 18

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 16

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 16

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 20

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 20

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 18

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 18

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 18

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 18

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 20

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 20

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 20

Unexpected any. Specify a different type

Check warning on line 192 in src/momento-redis-adapter.ts

View workflow job for this annotation

GitHub Actions / Test on Node 20

Unexpected any. Specify a different type

quit(): Promise<'OK'>;
}
Expand Down Expand Up @@ -785,7 +779,7 @@
key: RedisKey,
milliseconds: number,
ttlFlagIdentifier?: 'NX' | 'XX' | 'GT' | 'LT'
): Promise<number | null> {
): Promise<number> {
let shouldUpdateTtl = true;

if (ttlFlagIdentifier === 'NX') {
Expand Down Expand Up @@ -831,6 +825,14 @@
return 0;
}

async expire(
key: RedisKey,
seconds: number,
ttlFlagIdentifier?: 'NX' | 'XX' | 'GT' | 'LT'
): Promise<number> {
return await this.pexpire(key, seconds * 1000, ttlFlagIdentifier);
}

async unlink(...args: [...keys: RedisKey[]]): Promise<number> {
await this.del(...args);
return args.length;
Expand Down
186 changes: 186 additions & 0 deletions test/pexpire.test.ts → test/expire.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,189 @@ describe('pexpire', () => {
expect(pexpireRsp).toBe(0);
});
});

describe('expire', () => {
it('should update ttl using expire when no flags are specified', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key
const expireRsp = await client.expire(key, 5);
expect(expireRsp).toBe(1);

// Get ttl of key
const ttlResp = await client.ttl(key);
expect(ttlResp).toBeGreaterThan(2);
});

it('should overwrite ttl using expire when no flags are specified', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key
const expireRsp = await client.expire(key, 10);
expect(expireRsp).toBe(1);

// Get ttl of key
const ttlResp = await client.ttl(key);
expect(ttlResp).toBeGreaterThan(5);
});

it('should not update ttl using expire with nx flag when key exists when momento enabled', async () => {
if (process.env.MOMENTO_ENABLED === 'true') {
const key = v4();
const value = v4();

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

// Set ttl of key using nx flag
const expireRsp = await client.expire(key, 5, 'NX');
expect(expireRsp).toBe(0);
}
});

it('should update ttl using expire with nx flag when key exists with no ttl when redis enabled', async () => {
if (process.env.REDIS_ENABLED === 'true') {
const key = v4();
const value = v4();

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

// Set ttl of key using nx flag
const expireRsp = await client.expire(key, 5, 'NX');
expect(expireRsp).toBe(1);
}
});

it('should not update ttl using expire with nx flag when key exists and ttl on key exists', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key using nx flag
const expireRsp = await client.expire(key, 10, 'NX');
expect(expireRsp).toBe(0);
});

it('should not update ttl when expire with nx flag when key does not exist', async () => {
const key = v4();
const key1 = v4();
const value = v4();

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

// Set ttl of key using nx flag
const expireRsp = await client.expire(key1, 10, 'NX');
expect(expireRsp).toBe(0);
});

it('should update ttl using expire with xx flag is specified when key exists and ttl on key exits', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key using xx flag
const expireRsp = await client.expire(key, 10, 'XX');
expect(expireRsp).toBe(1);
});

it('should not update ttl using expire with xx flag when key does not exist', async () => {
const key = v4();
const key1 = v4();
const value = v4();

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

// Set ttl of key using xx flag
const expireRsp = await client.expire(key1, 10, 'XX');
expect(expireRsp).toBe(0);
});

it('should update ttl using expire with gt flag when key exists and new expiry is greater than current one', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key using gt flag
const expireRsp = await client.expire(key, 10, 'GT');
expect(expireRsp).toBe(1);
});

it('should not update ttl using expire with gt flag when key exists and new expiry is less than the current one', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key using gt flag
const expireRsp = await client.expire(key, 5, 'GT');
expect(expireRsp).toBe(0);
});

it('should not update ttl using expire with gt flag when key does not exist', async () => {
const key = v4();
const key1 = v4();
const value = v4();

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

// Set ttl of key using gt flag
const expireRsp = await client.expire(key1, 5, 'GT');
expect(expireRsp).toBe(0);
});

it('should update ttl using expire with lt flag when key exists and new expiry is less than current one', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key using lt flag
const expireRsp = await client.expire(key, 5, 'LT');
expect(expireRsp).toBe(1);
});

it('should not update ttl using expire with lt flag when key exists and new expiry is greater than current one', async () => {
const key = v4();
const value = v4();

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

// Set ttl of key using lt flag
const expireRsp = await client.expire(key, 10, 'LT');
expect(expireRsp).toBe(0);
});

it('should not update ttl using expire with lt flag when key does not exist', async () => {
const key = v4();
const key1 = v4();
const value = v4();

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

// Set ttl of key using lt flag
const expireRsp = await client.expire(key1, 10, 'LT');
expect(expireRsp).toBe(0);
});
});
Loading