Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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 expiry for hash field using relative time to expire (seconds)
* - _group_: hash
* - _complexity_: O(N) where N is the number of specified fields
* - _since_: 7.4.0
*/
hexpire(...args: [key: RedisKey, seconds: number | string, fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[], callback: Callback<number[]>]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[]]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, nx: 'NX', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[], callback: Callback<number[]>]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, nx: 'NX', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[]]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, xx: 'XX', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[], callback: Callback<number[]>]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, xx: 'XX', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[]]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, gt: 'GT', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[], callback: Callback<number[]>]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, gt: 'GT', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[]]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, lt: 'LT', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[], callback: Callback<number[]>]): Result<number[], Context>;
hexpire(...args: [key: RedisKey, seconds: number | string, lt: 'LT', fieldsToken: 'FIELDS', numfields: number | string, ...fields: (string | Buffer)[]]): Result<number[], Context>;

/**
* Get the value of a hash field
* - _group_: hash
Expand Down
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"url": "https://opencollective.com/ioredis"
},
"dependencies": {
"@ioredis/commands": "^1.1.1",
"@ioredis/commands": "^1.3.0",
"cluster-key-slot": "^1.1.0",
"debug": "^4.3.4",
"denque": "^2.1.0",
Expand Down
72 changes: 72 additions & 0 deletions test/functional/hexpire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Redis from "../../lib/Redis";
import { expect } from "chai";

describe("hexpire", () => {
const hashKey = "test_hash_key";
const field = "test_field";

it("should handle non-existing field", async () => {
const redis = new Redis();

const result = await redis.hexpire(
"non_existing_hash_key",
60,
"NX",
"FIELDS",
1,
"non_existing_field"
);

expect(result).to.deep.equal([-2]);
});

it("should handle existing field", async () => {
const redis = new Redis();

await redis.hset(hashKey, field, "value");

const result = await redis.hexpire(hashKey, 60, "FIELDS", 1, field);

expect(result).to.deep.equal([1]);
});

it("should return 0 when condition is not met", async () => {
const redis = new Redis();

await redis.hset(hashKey, field, "value");
await redis.hexpire(hashKey, 60, "FIELDS", 1, field); // Set initial expiry

// Try to set expiry with NX when field already has expiry
const result = await redis.hexpire(hashKey, 120, "NX", "FIELDS", 1, field);

expect(result).to.deep.equal([0]);
});

it("should return 2 when expiring field with 0 seconds", async () => {
const redis = new Redis();

await redis.hset(hashKey, field, "value");

const result = await redis.hexpire(hashKey, 0, "FIELDS", 1, field);

expect(result).to.deep.equal([2]);
});

it("should expire multiple fields", async () => {
const redis = new Redis();

await redis.hset(hashKey, field, "value", "field2", "value2");

const result = await redis.hexpire(
hashKey,
60,
"FIELDS",
3,
field,
"field2",
"non_existing_field"
);

expect(result).to.deep.equal([1, 1, -2]);
});
});
Loading