Skip to content

Commit 3cbdd82

Browse files
committed
Add hexpire
Introduced in 7.4.0, https://redis.io/docs/latest/commands/hexpire/
1 parent b72d36f commit 3cbdd82

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/redis/commands/hashes.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@ 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.hexpire("hash", 10, "f1", "f2") # => [1, 1]
261+
#
262+
# @param [String] key
263+
# @param [Integer] ttl
264+
# @param [Array<String>] fields
265+
# @return [Integer] The number of fields that were added to the hash
266+
def hexpire(key, ttl, *fields)
267+
send_command([:hexpire, key, ttl, 'FIELDS', fields.length, *fields])
268+
end
256269
end
257270
end
258271
end

test/lint/hashes.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,18 @@ 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", 1, "f1")
236+
assert r.hexists("foo", "f1")
237+
238+
sleep(1)
239+
240+
refute r.hexists("foo", "f1")
241+
end
242+
end
230243
end
231244
end

0 commit comments

Comments
 (0)