Skip to content

Commit 2c4eaac

Browse files
vietqhoangbyroot
authored andcommitted
Add hexpire and httl
Introduced in 7.4.0, https://redis.io/docs/latest/commands/hexpire/
1 parent f9b38d4 commit 2c4eaac

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

lib/redis/commands/hashes.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,38 @@ def hscan_each(key, **options, &block)
253253
break if cursor == "0"
254254
end
255255
end
256+
257+
# Sets the time to live in seconds for one or more fields.
258+
#
259+
# @example
260+
# redis.hset("hash", "f1", "v1")
261+
# redis.hexpire("hash", 10, "f1", "f2") # => [1, -2]
262+
#
263+
# @param [String] key
264+
# @param [Integer] ttl
265+
# @param [Array<String>] fields
266+
# @return [Array<Integer>] Feedback on if the fields have been updated.
267+
#
268+
# See https://redis.io/docs/latest/commands/hexpire/#return-information for array reply.
269+
def hexpire(key, ttl, *fields)
270+
send_command([:hexpire, key, ttl, 'FIELDS', fields.length, *fields])
271+
end
272+
273+
# Returns the time to live in seconds for one or more fields.
274+
#
275+
# @example
276+
# redis.hset("hash", "f1", "v1", "f2", "v2")
277+
# redis.hexpire("hash", 10, "f1") # => [1]
278+
# redis.httl("hash", "f1", "f2", "f3") # => [10, -1, -2]
279+
#
280+
# @param [String] key
281+
# @param [Array<String>] fields
282+
# @return [Array<Integer>] Feedback on the TTL of the fields.
283+
#
284+
# See https://redis.io/docs/latest/commands/httl/#return-information for array reply.
285+
def httl(key, *fields)
286+
send_command([:httl, key, 'FIELDS', fields.length, *fields])
287+
end
256288
end
257289
end
258290
end

lib/redis/distributed.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,14 @@ def hgetall(key)
918918
node_for(key).hgetall(key)
919919
end
920920

921+
def hexpire(key, ttl, *fields)
922+
node_for(key).hexpire(key, ttl, *fields)
923+
end
924+
925+
def httl(key, ttl, *fields)
926+
node_for(key).httl(key, ttl, *fields)
927+
end
928+
921929
# Scan a hash
922930
def hscan(key, cursor, **options)
923931
node_for(key).hscan(key, cursor, **options)

test/lint/hashes.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,28 @@ def test_hscan
227227
expected = ['0', [%w[f1 Jack], %w[f2 33]]]
228228
assert_equal expected, redis.hscan('foo', 0)
229229
end
230+
231+
def test_hexpire
232+
target_version "7.4.0" do
233+
r.hset("foo", "f1", "v2")
234+
235+
assert_equal [1], r.hexpire("foo", 4, "f1")
236+
assert_in_range(1..4, r.httl("foo", "f1")[0])
237+
end
238+
end
239+
240+
def test_httl
241+
target_version "7.4.0" do
242+
assert [-2], r.httl("foo", "f1")
243+
244+
r.hset("foo", "f1", "v2")
245+
246+
assert [-1], r.httl("foo", "f1")
247+
248+
r.hexpire("foo", 4, "f1")
249+
250+
assert_in_range(1..4, r.httl("foo", "f1")[0])
251+
end
252+
end
230253
end
231254
end

0 commit comments

Comments
 (0)