-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for all hash field expiration commands
- Loading branch information
Showing
20 changed files
with
555 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { transformArguments } from './HEXPIRE'; | ||
import { HASH_EXPIRATION_TIME } from './HEXPIRETIME'; | ||
|
||
describe('HEXPIRE', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 'field', 1), | ||
['HEXPIRE', 'key', '1', 'FIELDS', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1', 'field2'], 1), | ||
['HEXPIRE', 'key', '1', 'FIELDS', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1'], 1, 'NX'), | ||
['HEXPIRE', 'key', '1', 'NX', 'FIELDS', '1', 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testWithClient('hexpire', async client => { | ||
assert.deepEqual( | ||
await client.hExpire('key', ['field1'], 0), | ||
[ HASH_EXPIRATION_TIME.FieldNotExists ] | ||
); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { RedisCommandArgument } from '.'; | ||
import { pushVerdictArgument } from './generic-transformers'; | ||
|
||
/** | ||
* @readonly | ||
* @enum {number} | ||
*/ | ||
export const HASH_EXPIRATION = { | ||
/** @property {number} */ | ||
/** The field does not exist */ | ||
FieldNotExists: -2, | ||
/** @property {number} */ | ||
/** Specified NX | XX | GT | LT condition not met */ | ||
ConditionNotMet: 0, | ||
/** @property {number} */ | ||
/** Expiration time was set or updated */ | ||
Updated: 1, | ||
/** @property {number} */ | ||
/** 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 const FIRST_KEY_INDEX = 1; | ||
|
||
export function transformArguments( | ||
key: RedisCommandArgument, | ||
fields: RedisCommandArgument| Array<RedisCommandArgument>, | ||
seconds: number, | ||
mode?: 'NX' | 'XX' | 'GT' | 'LT', | ||
) { | ||
const args = ['HEXPIRE', key, seconds.toString()]; | ||
|
||
if (mode) { | ||
args.push(mode); | ||
} | ||
|
||
args.push('FIELDS'); | ||
|
||
return pushVerdictArgument(args, fields); | ||
} | ||
|
||
export declare function transformReply(): Array<HashExpiration>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import { transformArguments } from './HEXPIREAT'; | ||
import { HASH_EXPIRATION_TIME } from './HEXPIRETIME'; | ||
|
||
describe('HEXPIREAT', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string + number', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 'field', 1), | ||
['HEXPIREAT', 'key', '1', 'FIELDS', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array + number', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1', 'field2'], 1), | ||
['HEXPIREAT', 'key', '1', 'FIELDS', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('date', () => { | ||
const d = new Date(); | ||
|
||
assert.deepEqual( | ||
transformArguments('key', ['field1'], d), | ||
['HEXPIREAT', 'key', Math.floor(d.getTime() / 1000).toString(), 'FIELDS', '1', 'field1'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 'field1', 1, 'GT'), | ||
['HEXPIREAT', 'key', '1', 'GT', 'FIELDS', '1', 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testWithClient('expireAt', async client => { | ||
assert.deepEqual( | ||
await client.hExpireAt('key', 'field1', 1), | ||
[ HASH_EXPIRATION_TIME.FieldNotExists ] | ||
); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { RedisCommandArgument } from '.'; | ||
import { pushVerdictArgument, transformEXAT } from './generic-transformers'; | ||
import { HashExpiration } from './HEXPIRE'; | ||
|
||
export const FIRST_KEY_INDEX = 1; | ||
|
||
export function transformArguments( | ||
key: RedisCommandArgument, | ||
fields: RedisCommandArgument | Array<RedisCommandArgument>, | ||
timestamp: number | Date, | ||
mode?: 'NX' | 'XX' | 'GT' | 'LT' | ||
) { | ||
const args = [ | ||
'HEXPIREAT', | ||
key, | ||
transformEXAT(timestamp) | ||
]; | ||
|
||
if (mode) { | ||
args.push(mode); | ||
} | ||
|
||
args.push('FIELDS') | ||
|
||
return pushVerdictArgument(args, fields); | ||
} | ||
|
||
export declare function transformReply(): Array<HashExpiration>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import { HASH_EXPIRATION_TIME, transformArguments } from './HEXPIRETIME'; | ||
|
||
describe('HEXPIRETIME', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 'field'), | ||
['HEXPIRETIME', 'key', 'FIELDS', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1', 'field2']), | ||
['HEXPIRETIME', 'key', 'FIELDS', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}) | ||
|
||
testUtils.testWithClient('hExpireTime', async client => { | ||
assert.deepEqual( | ||
await client.hExpireTime('key', 'field1'), | ||
[ HASH_EXPIRATION_TIME.FieldNotExists ] | ||
); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { RedisCommandArgument } from '.'; | ||
import { pushVerdictArgument } from './generic-transformers'; | ||
|
||
export const HASH_EXPIRATION_TIME = { | ||
/** @property {number} */ | ||
/** The field does not exist */ | ||
FieldNotExists: -2, | ||
/** @property {number} */ | ||
/** The field exists but has no associated expire */ | ||
NoExpiration: -1, | ||
} as const; | ||
|
||
export const FIRST_KEY_INDEX = 1 | ||
|
||
export const IS_READ_ONLY = true; | ||
|
||
export function transformArguments(key: RedisCommandArgument, fields: RedisCommandArgument | Array<RedisCommandArgument>) { | ||
return pushVerdictArgument(['HEXPIRETIME', key, 'FIELDS'], fields); | ||
} | ||
|
||
export declare function transformReply(): Array<number>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { transformArguments } from './HPERSIST'; | ||
import { HASH_EXPIRATION_TIME } from './HEXPIRETIME'; | ||
|
||
describe('HPERSIST', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 'field'), | ||
['HPERSIST', 'key', 'FIELDS', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1', 'field2']), | ||
['HPERSIST', 'key', 'FIELDS', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
}) | ||
|
||
testUtils.testWithClient('hPersist', async client => { | ||
assert.deepEqual( | ||
await client.hPersist('key', 'field1'), | ||
[ HASH_EXPIRATION_TIME.FieldNotExists ] | ||
); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { RedisCommandArgument } from '.'; | ||
import { pushVerdictArgument } from './generic-transformers'; | ||
|
||
export const FIRST_KEY_INDEX = 1; | ||
|
||
export function transformArguments(key: RedisCommandArgument, fields: RedisCommandArgument | Array<RedisCommandArgument>) { | ||
return pushVerdictArgument(['HPERSIST', key, 'FIELDS'], fields); | ||
} | ||
|
||
export declare function transformReply(): Array<number> | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { transformArguments } from './HPEXPIRE'; | ||
import { HASH_EXPIRATION_TIME } from './HEXPIRETIME'; | ||
|
||
describe('HEXPIRE', () => { | ||
testUtils.isVersionGreaterThanHook([7, 4]); | ||
|
||
describe('transformArguments', () => { | ||
it('string', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 'field', 1), | ||
['HPEXPIRE', 'key', '1', 'FIELDS', '1', 'field'] | ||
); | ||
}); | ||
|
||
it('array', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1', 'field2'], 1), | ||
['HPEXPIRE', 'key', '1', 'FIELDS', '2', 'field1', 'field2'] | ||
); | ||
}); | ||
|
||
it('with set option', () => { | ||
assert.deepEqual( | ||
transformArguments('key', ['field1'], 1, 'NX'), | ||
['HPEXPIRE', 'key', '1', 'NX', 'FIELDS', '1', 'field1'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testWithClient('hexpire', async client => { | ||
assert.deepEqual( | ||
await client.hpExpire('key', ['field1'], 0), | ||
[ HASH_EXPIRATION_TIME.FieldNotExists ] | ||
); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN | ||
}); | ||
}); |
Oops, something went wrong.