Skip to content

Commit

Permalink
Implement hexpire
Browse files Browse the repository at this point in the history
  • Loading branch information
mlb5000 committed Sep 20, 2024
1 parent af83275 commit b8c3cc3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/returnTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ module.exports = {
hdel: "number",
hello: "unknown[]",
hexists: "number",
hexpire: "number[]",
hget: "string | null",
hgetall: "[field: string, value: string][]",
hincrby: "number",
Expand Down
17 changes: 17 additions & 0 deletions lib/utils/RedisCommander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3870,6 +3870,23 @@ interface RedisCommander<Context extends ClientContext = { type: "default" }> {
callback?: Callback<number>
): Result<number, Context>;

/**
* Set an expiration (TTL or time to live) on one or more fields of a given hash key. You must specify at least one field. Field(s) will automatically be deleted from the hash key when their TTLs expire.
* - _group_: hash
* - _complexity_: O(n)
* - _since_: 7.4.0
*/
hexpire(
...args: [
key: RedisKey,
value: string | Buffer | number,
nx: "NX" | "XX" | "GT" | "LT",
fields: "FIELDS",
numFields: number,
...fieldValues: string[]
]
): Result<"OK", Context>;

/**
* Get the value of a hash field
* - _group_: hash
Expand Down
46 changes: 46 additions & 0 deletions test/functional/hexpire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Redis from "../../lib/Redis";
import { expect } from "chai";

const CUSTOM_PROPERTY = "_myCustomProperty";

describe("hexpire", () => {
beforeEach(() => {
Object.defineProperty(Object.prototype, CUSTOM_PROPERTY, {
value: false,
configurable: true,
enumerable: false,
writable: false,
});
});

afterEach(() => {
delete (Object.prototype as any)[CUSTOM_PROPERTY];
});

it("should handle special field names", async () => {
const redis = new Redis();
await redis.hmset(
"test_key",
"__proto__",
"hello",
CUSTOM_PROPERTY,
"world",
"leftbehind",
"stays"
);
var expireResult = await redis.hexpire(
"test_key",
1,
"NX",
"FIELDS",
2,
"__proto__",
CUSTOM_PROPERTY
);
await new Promise((r) => setTimeout(r, 3000));
const ret = await redis.hgetall("test_key");
expect(Object.getPrototypeOf(ret)).to.eql(Object.prototype);
expect(Object.keys(ret).sort()).to.eql(["leftbehind"].sort());
expect(ret.leftbehind).to.eql("stays");
});
});

0 comments on commit b8c3cc3

Please sign in to comment.