Skip to content
Open
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
16 changes: 14 additions & 2 deletions lib/redis/commands/hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,23 @@ def hscan_each(key, **options, &block)
# @param [String] key
# @param [Integer] ttl
# @param [Array<String>] fields
# @param [Hash] options
# - `:nx => true`: Set expiry only when the key has no expiry.
# - `:xx => true`: Set expiry only when the key has an existing expiry.
# - `:gt => true`: Set expiry only when the new expiry is greater than current one.
# - `:lt => true`: Set expiry only when the new expiry is less than current one.
# @return [Array<Integer>] Feedback on if the fields have been updated.
#
# See https://redis.io/docs/latest/commands/hexpire/#return-information for array reply.
def hexpire(key, ttl, *fields)
send_command([:hexpire, key, ttl, 'FIELDS', fields.length, *fields])
def hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
args = [:hexpire, key, ttl]
args << "NX" if nx
args << "XX" if xx
args << "GT" if gt
args << "LT" if lt
args.concat(['FIELDS', fields.length, *fields])

send_command(args)
end

# Returns the time to live in seconds for one or more fields.
Expand Down
4 changes: 2 additions & 2 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@ def hgetall(key)
node_for(key).hgetall(key)
end

def hexpire(key, ttl, *fields)
node_for(key).hexpire(key, ttl, *fields)
def hexpire(key, ttl, *fields, nx: nil, xx: nil, gt: nil, lt: nil)
node_for(key).hexpire(key, ttl, *fields, nx: nx, xx: xx, gt: gt, lt: lt)
end

def httl(key, *fields)
Expand Down
18 changes: 18 additions & 0 deletions test/lint/hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ def test_hexpire
end
end

def test_hexpire_options
target_version "7.4.0" do
r.hset("foo", "f1", "v2")
assert_equal [0], r.hexpire("foo", 5, "f1", xx: true)
assert_equal [-1], r.httl("foo", "f1")

assert_equal [1], r.hexpire("foo", 5, "f1", nx: true)
assert_in_range(1..5, r.httl("foo", "f1")[0])
assert_equal [0], r.hexpire("foo", 5, "f1", nx: true)

assert_equal [1], r.hexpire("foo", 5, "f1", xx: true)

assert_equal [0], r.hexpire("foo", 10, "f1", lt: true)
assert_equal [1], r.hexpire("foo", 10, "f1", gt: true)
assert_in_range(1..10, r.httl("foo", "f1")[0])
end
end

def test_httl
target_version "7.4.0" do
assert [-2], r.httl("foo", "f1")
Expand Down