-
Notifications
You must be signed in to change notification settings - Fork 77
DX-1780: Add HEXPIRE #1367
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
Merged
Merged
DX-1780: Add HEXPIRE #1367
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c15f590
feat: add hexpire command
CahidArda 447a5e3
fix: review
CahidArda de94ddb
fix: command types
CahidArda d86a5ab
fix: revert changes in script.ts
CahidArda 6435137
fix: update createScript with a generic
CahidArda 908f626
fix: add generic to createScript
CahidArda a455cd7
Merge branch 'main' into DX-1780-hexpire
CahidArda 402e9d3
fix: tests
CahidArda 8c48867
fix: test
CahidArda 6a63941
fix: tests
CahidArda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,174 @@ | ||
import { keygen, newHttpClient, randomID } from "../test-utils"; | ||
|
||
import { afterAll, describe, expect, test } from "bun:test"; | ||
import { HSetCommand } from "./hset"; | ||
import { HExpireCommand } from "./hexpire"; | ||
import { HGetCommand } from "./hget"; | ||
const client = newHttpClient(); | ||
|
||
const { newKey, cleanup } = keygen(); | ||
afterAll(cleanup); | ||
|
||
test("expires a hash key correctly", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 1]).exec(client); | ||
expect(res).toEqual([1]); | ||
await new Promise((res) => setTimeout(res, 2000)); | ||
const res2 = await new HGetCommand([key, hashKey]).exec(client); | ||
|
||
expect(res2).toEqual(null); | ||
}); | ||
|
||
describe("NX", () => { | ||
test("should set expiry only when the field has no expiry", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
const res = await new HExpireCommand([key, [hashKey], 1, "NX"]).exec(client); | ||
expect(res).toEqual([1]); | ||
await new Promise((res) => setTimeout(res, 2000)); | ||
const res2 = await new HGetCommand([key, hashKey]).exec(client); | ||
|
||
expect(res2).toEqual(null); | ||
}); | ||
|
||
test("should not set expiry when the field has expiry", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
await new HExpireCommand([key, hashKey, 1000]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 1, "NX"]).exec(client); | ||
expect(res).toEqual([0]); | ||
}); | ||
}); | ||
|
||
describe("XX", () => { | ||
test( | ||
"should set expiry only when the field has an existing expiry", | ||
async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
await new HExpireCommand([key, hashKey, 1]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 5, "XX"]).exec(client); | ||
expect(res).toEqual([1]); | ||
await new Promise((res) => setTimeout(res, 6000)); | ||
const res2 = await new HGetCommand([key, hashKey]).exec(client); | ||
expect(res2).toEqual(null); | ||
}, | ||
{ | ||
timeout: 7000, | ||
} | ||
); | ||
|
||
test("should not set expiry when the field does not have an existing expiry", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 5, "XX"]).exec(client); | ||
expect(res).toEqual([0]); | ||
}); | ||
}); | ||
|
||
describe("GT", () => { | ||
test( | ||
"should set expiry only when the new expiry is greater than current one", | ||
async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
await new HExpireCommand([key, hashKey, 1]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 5, "GT"]).exec(client); | ||
expect(res).toEqual([1]); | ||
await new Promise((res) => setTimeout(res, 6000)); | ||
const res2 = await new HGetCommand([key, hashKey]).exec(client); | ||
expect(res2).toEqual(null); | ||
}, | ||
{ | ||
timeout: 7000, | ||
} | ||
); | ||
|
||
test("should not set expiry when the new expiry is not greater than current one", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
await new HExpireCommand([key, hashKey, 10]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 5, "GT"]).exec(client); | ||
expect(res).toEqual([0]); | ||
}); | ||
}); | ||
|
||
describe("LT", () => { | ||
test("should set expiry only when the new expiry is less than current one", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
await new HExpireCommand([key, hashKey, 5]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 3, "LT"]).exec(client); | ||
expect(res).toEqual([1]); | ||
await new Promise((res) => setTimeout(res, 4000)); | ||
const res2 = await new HGetCommand([key, hashKey]).exec(client); | ||
expect(res2).toEqual(null); | ||
}); | ||
|
||
test("should not set expiry when the new expiry is not less than current one", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const value = randomID(); | ||
await new HSetCommand([key, { [hashKey]: value }]).exec(client); | ||
await new HExpireCommand([key, hashKey, 10]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey, 20, "LT"]).exec(client); | ||
expect(res).toEqual([0]); | ||
}); | ||
}); | ||
|
||
test("should return -2 if no such field exists in the provided hash key", async () => { | ||
const key = newKey(); | ||
const hashKey = newKey(); | ||
const hashKey2 = newKey(); | ||
await new HSetCommand([key, { [hashKey]: 1 }]).exec(client); | ||
const res = await new HExpireCommand([key, hashKey2, 1]).exec(client); | ||
expect(res).toEqual([-2]); | ||
}); | ||
|
||
test("should return results for multiple fields in order", async () => { | ||
const key = newKey(); | ||
const hashKey1 = newKey(); | ||
const hashKey2 = newKey(); | ||
const hashKey3 = newKey(); | ||
const value1 = randomID(); | ||
const value2 = randomID(); | ||
|
||
await new HSetCommand([key, { [hashKey1]: value1, [hashKey2]: value2 }]).exec(client); | ||
|
||
// Set expiry for the first field | ||
await new HExpireCommand([key, hashKey1, 1]).exec(client); | ||
|
||
// Pass both fields to HExpireCommand | ||
const res = await new HExpireCommand([key, [hashKey1, hashKey2, hashKey3], 1, "NX"]).exec(client); | ||
|
||
// Expect the results in order: hashKey1 already has expiry, hashKey2 does not | ||
expect(res).toEqual([0, 1, -2]); | ||
|
||
// Wait for the expiry to take effect | ||
await new Promise((res) => setTimeout(res, 2000)); | ||
|
||
// Verify that hashKey1 is expired | ||
const res1 = await new HGetCommand([key, hashKey1]).exec(client); | ||
expect(res1).toEqual(null); | ||
|
||
// Verify that hashKey2 is expired | ||
const res2 = await new HGetCommand([key, hashKey2]).exec(client); | ||
expect(res2).toEqual(null); | ||
}); |
This file contains hidden or 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,30 @@ | ||
import type { CommandOptions } from "./command"; | ||
import { Command } from "./command"; | ||
|
||
type HExpireOptions = "NX" | "nx" | "XX" | "xx" | "GT" | "gt" | "LT" | "lt"; | ||
export class HExpireCommand extends Command<(-2 | 0 | 1 | 2)[], (-2 | 0 | 1 | 2)[]> { | ||
constructor( | ||
cmd: [ | ||
key: string, | ||
fields: (string | number) | (string | number)[], | ||
seconds: number, | ||
option?: HExpireOptions, | ||
], | ||
opts?: CommandOptions<(-2 | 0 | 1 | 2)[], (-2 | 0 | 1 | 2)[]> | ||
) { | ||
const [key, fields, seconds, option] = cmd; | ||
yunusemreozdemir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const fieldArray = Array.isArray(fields) ? fields : [fields]; | ||
super( | ||
[ | ||
"hexpire", | ||
key, | ||
seconds, | ||
...(option ? [option] : []), | ||
"FIELDS", | ||
fieldArray.length, | ||
...fieldArray, | ||
], | ||
opts | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.