-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
add support for all hash field expiration commands #2742
base: v5
Are you sure you want to change the base?
Changes from all commits
9bae18c
0a87234
69be96f
5ceb5f5
6a2c58b
986bd84
9252bf2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HEXPIRE from './HEXPIRE'; | ||
|
||
describe('HEXPIRE', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HEXPIRE.transformArguments('key', 'field', 1), | ||
['HEXPIRE', 'key', '1', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HEXPIRE.transformArguments('key', ['field1', 'field2'], 1), | ||
['HEXPIRE', 'key', '1', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
HEXPIRE.transformArguments('key', 'field1', 1, 'NX'), | ||
['HEXPIRE', 'key', '1', 'NX', '1', 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('hExpire', async client => { | ||
assert.equal( | ||
await client.hExpire('key', 'field', 0), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Command, NumberReply, ArrayReply, RedisArgument } from "../RESP/types"; | ||
import { RedisVariadicArgument, pushVariadicArgument } from "./generic-transformers"; | ||
|
||
export const HASH_EXPIRATION = { | ||
/** The field does not exist */ | ||
FIELD_NOT_EXISTS: -2, | ||
/** Specified NX | XX | GT | LT condition not met */ | ||
CONDITION_NOT_MET: 0, | ||
/** Expiration time was set or updated */ | ||
UPDATED: 1, | ||
/** Field deleted because the specified expiration time is in the past */ | ||
DELETED: 2 | ||
} as const; | ||
|
||
export type HashExpiration = typeof HASH_EXPIRATION[keyof typeof HASH_EXPIRATION]; | ||
|
||
export type HashExpirationReply = NumberReply<HashExpiration>; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
transformArguments( | ||
key: RedisArgument, | ||
fields: RedisVariadicArgument, | ||
seconds: number, | ||
mode?: 'NX' | 'XX' | 'GT' | 'LT', | ||
) { | ||
const args = ['HEXPIRE', key, seconds.toString()]; | ||
|
||
if (mode) { | ||
args.push(mode); | ||
} | ||
|
||
return pushVariadicArgument(args, fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HEXPIREAT from './HEXPIREAT'; | ||
|
||
describe('HEXPIREAT', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string + number', () => { | ||
assert.deepEqual( | ||
HEXPIREAT.transformArguments('key', 'field', 1), | ||
['HEXPIREAT', 'key', '1', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array + number', () => { | ||
assert.deepEqual( | ||
HEXPIREAT.transformArguments('key', ['field1', 'field2'], 1), | ||
['HEXPIREAT', 'key', '1', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('date', () => { | ||
const d = new Date(); | ||
assert.deepEqual( | ||
HEXPIREAT.transformArguments('key', ['field1'], d), | ||
['HEXPIREAT', 'key', Math.floor(d.getTime() / 1000).toString(), '1', 'field1'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
HEXPIREAT.transformArguments('key', 'field1', 1, 'GT'), | ||
['HEXPIREAT', 'key', '1', 'GT', 1, 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('expireAt', async client => { | ||
assert.equal( | ||
await client.hExpireAt('key', 'field', 1), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RedisArgument, ArrayReply, Command } from '../RESP/types'; | ||
import { HashExpirationReply } from './HEXPIRE'; | ||
import { RedisVariadicArgument, pushVariadicArgument, transformEXAT } from './generic-transformers'; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
IS_READ_ONLY: true, | ||
transformArguments( | ||
key: RedisArgument, | ||
fields: RedisVariadicArgument, | ||
timestamp: number | Date, | ||
mode?: 'NX' | 'XX' | 'GT' | 'LT' | ||
) { | ||
const args = ['HEXPIREAT', key, transformEXAT(timestamp)]; | ||
|
||
if (mode) { | ||
args.push(mode); | ||
} | ||
|
||
return pushVariadicArgument(args, fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HEXPIRETIME from './HEXPIRETIME'; | ||
|
||
describe('HEXPIRETIME', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HEXPIRETIME.transformArguments('key', 'field'), | ||
['HEXPIRETIME', 'key', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HEXPIRETIME.transformArguments('key', ['field1', 'field2']), | ||
['HEXPIRETIME', 'key', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}) | ||
|
||
testUtils.testAll('hExpireTime', async client => { | ||
assert.equal( | ||
await client.hExpireTime('key', 'field'), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types'; | ||
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers'; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
IS_READ_ONLY: true, | ||
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) { | ||
return pushVariadicArgument(['HEXPIRETIME', key], fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HPERSIST from './HPERSIST'; | ||
|
||
describe('HPERSIST', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HPERSIST.transformArguments('key', 'field'), | ||
['HPERSIST', 'key', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HPERSIST.transformArguments('key', ['field1', 'field2']), | ||
['HPERSIST', 'key', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}) | ||
|
||
testUtils.testAll('hPersist', async client => { | ||
assert.equal( | ||
await client.hPersist('key', 'field'), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types'; | ||
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers'; | ||
|
||
export const HASH_FIELD_PERSIST = { | ||
/** The expiration time was removed */ | ||
EXPIRATION_REMOVED: 1, | ||
/** The field has no expiration time */ | ||
NO_EXPIRATION_TIME: -1, | ||
/** The field does not exist */ | ||
FIELD_NOT_EXISTS: -2, | ||
} as const; | ||
|
||
export type HashFieldPersist = typeof HASH_FIELD_PERSIST[keyof typeof HASH_FIELD_PERSIST]; | ||
|
||
export type HashFieldPersistReply = NumberReply<HashFieldPersist>; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) { | ||
return pushVariadicArgument(['HPERSIST', key], fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<HashFieldPersistReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HPEXPIRE from './HPEXPIRE'; | ||
|
||
describe('HPEXPIRE', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HPEXPIRE.transformArguments('key', 'field', 1), | ||
['HPEXPIRE', 'key', '1', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HPEXPIRE.transformArguments('key', ['field1', 'field2'], 1), | ||
['HPEXPIRE', 'key', '1', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
HPEXPIRE.transformArguments('key', ['field1'], 1, 'NX'), | ||
['HPEXPIRE', 'key', '1', 'NX', '1', 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('hpRxpire', async client => { | ||
assert.equal( | ||
await client.hpExpire('key', 'field', 0), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { RedisArgument, ArrayReply, Command } from '../RESP/types'; | ||
import { HashExpirationReply } from './HEXPIRE'; | ||
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers'; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
transformArguments( | ||
key: RedisArgument, | ||
fields: RedisVariadicArgument, | ||
ms: number, | ||
mode?: 'NX' | 'XX' | 'GT' | 'LT', | ||
) { | ||
const args = ['HPEXPIRE', key, ms.toString()]; | ||
|
||
if (mode) { | ||
args.push(mode); | ||
} | ||
|
||
return pushVariadicArgument(args, fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HPEXPIREAT from './HPEXPIREAT'; | ||
|
||
describe('HPEXPIREAT', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string + number', () => { | ||
assert.deepEqual( | ||
HPEXPIREAT.transformArguments('key', 'field', 1), | ||
['HPEXPIREAT', 'key', '1', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array + number', () => { | ||
assert.deepEqual( | ||
HPEXPIREAT.transformArguments('key', ['field1', 'field2'], 1), | ||
['HPEXPIREAT', 'key', '1', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('date', () => { | ||
const d = new Date(); | ||
assert.deepEqual( | ||
HPEXPIREAT.transformArguments('key', ['field1'], d), | ||
['HPEXPIREAT', 'key', d.getTime().toString(), '1', 'field1'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
HPEXPIREAT.transformArguments('key', ['field1'], 1, 'XX'), | ||
['HPEXPIREAT', 'key', '1', 'XX', '1', 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('hpExpireAt', async client => { | ||
assert.equal( | ||
await client.hpExpireAt('key', 'field', 1), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RedisArgument, ArrayReply, Command } from '../RESP/types'; | ||
import { HashExpirationReply } from './HEXPIRE'; | ||
import { RedisVariadicArgument, pushVariadicArgument, transformEXAT } from './generic-transformers'; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
IS_READ_ONLY: true, | ||
transformArguments( | ||
key: RedisArgument, | ||
fields: RedisVariadicArgument, | ||
timestamp: number | Date, | ||
mode?: 'NX' | 'XX' | 'GT' | 'LT' | ||
) { | ||
const args = ['HPEXPIREAT', key, transformEXAT(timestamp)]; | ||
|
||
if (mode) { | ||
args.push(mode); | ||
} | ||
|
||
return pushVariadicArgument(args, fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HPEXPIRETIME from './HPEXPIRETIME'; | ||
|
||
describe('HPEXPIRETIME', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HPEXPIRETIME.transformArguments('key', 'field'), | ||
['HPEXPIRETIME', 'key', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HPEXPIRETIME.transformArguments('key', ['field1', 'field2']), | ||
['HPEXPIRETIME', 'key', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('hpExpireTime', async client => { | ||
assert.equal( | ||
await client.hpExpireTime('key', 'field'), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types'; | ||
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers'; | ||
|
||
export const HASH_FIELD_EXPIRETIME = { | ||
/** The field does not exist */ | ||
FIELD_NOT_EXISTS: -2, | ||
/** The field has no expiration time */ | ||
NO_EXPIRATION_TIME: -1, | ||
} as const; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
IS_READ_ONLY: true, | ||
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) { | ||
return pushVariadicArgument(['HPEXPIRETIME', key], fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HPTTL from './HPTTL'; | ||
|
||
describe('HPTTL', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HPTTL.transformArguments('key', 'field'), | ||
['HPTTL', 'key', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HPTTL.transformArguments('key', ['field1', 'field2']), | ||
['HPTTL', 'key', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('hpTTL', async client => { | ||
assert.equal( | ||
await client.hpTTL('key', 'field'), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types'; | ||
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers'; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
IS_READ_ONLY: true, | ||
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) { | ||
return pushVariadicArgument(['HPTTL', key], fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | ||
} as const satisfies Command; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import HTTL from './HTTL'; | ||
|
||
describe('HTTL', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
HTTL.transformArguments('key', 'field'), | ||
['HTTL', 'key', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
HTTL.transformArguments('key', ['field1', 'field2']), | ||
['HTTL', 'key', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('hTTL', async client => { | ||
assert.equal( | ||
await client.hTTL('key', 'field'), | ||
[-2] | ||
); | ||
}, { | ||
client: GLOBAL.SERVERS.OPEN, | ||
cluster: GLOBAL.CLUSTERS.OPEN | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types'; | ||
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers'; | ||
|
||
export default { | ||
FIRST_KEY_INDEX: 1, | ||
IS_READ_ONLY: true, | ||
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) { | ||
return pushVariadicArgument(['HTTL', key], fields); | ||
}, | ||
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the reply is array of
should we create an "enum" (not the TS one) for the -2 and -1 values? IDK if I like having them anymore.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an enum can have value to refrer to negative values by name. but isn't that what I did before ala export const HASH_FIELD_EXPIRETIME = {
/** The field does not exist /
FIELD_NOT_EXISTS: -2,
/* The field has no expiration time */
NO_EXPIRATION_TIME: -1,
} as const; |
||
} as const satisfies Command; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do
for "future proofing" (in case there will be more optional arguments)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it doesn't really future proof, it just makes it easier if these options are shared between multiple commands. Unsure it adds that much.