Skip to content

Commit

Permalink
Merge pull request #1028 from magni-/pp/set-exat
Browse files Browse the repository at this point in the history
[Redis 6.2] Add EXAT/PXAT options to SET
  • Loading branch information
byroot committed Sep 13, 2021
2 parents 688ac95 + aec0ba7 commit c97360b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/redis.rb
Expand Up @@ -834,14 +834,18 @@ def incrbyfloat(key, increment)
# @param [Hash] options
# - `:ex => Integer`: Set the specified expire time, in seconds.
# - `:px => Integer`: Set the specified expire time, in milliseconds.
# - `:exat => Integer` : Set the specified Unix time at which the key will expire, in seconds.
# - `:pxat => Integer` : Set the specified Unix time at which the key will expire, in milliseconds.
# - `:nx => true`: Only set the key if it does not already exist.
# - `:xx => true`: Only set the key if it already exist.
# - `:keepttl => true`: Retain the time to live associated with the key.
# @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true`
def set(key, value, ex: nil, px: nil, nx: nil, xx: nil, keepttl: nil)
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil)
args = [:set, key, value.to_s]
args << "EX" << ex if ex
args << "PX" << px if px
args << "EXAT" << exat if exat
args << "PXAT" << pxat if pxat
args << "NX" if nx
args << "XX" if xx
args << "KEEPTTL" if keepttl
Expand Down
14 changes: 14 additions & 0 deletions test/lint/strings.rb
Expand Up @@ -51,6 +51,20 @@ def test_set_with_px
end
end

def test_set_with_exat
target_version "6.2" do
r.set("foo", "bar", exat: Time.now.to_i + 2)
assert_in_range 0..2, r.ttl("foo")
end
end

def test_set_with_pxat
target_version "6.2" do
r.set("foo", "bar", pxat: (1000 * Time.now.to_i) + 2000)
assert_in_range 0..2, r.ttl("foo")
end
end

def test_set_with_nx
target_version "2.6.12" do
r.set("foo", "qux", nx: true)
Expand Down

0 comments on commit c97360b

Please sign in to comment.