From f59a49f1e7d3e255fd3a6978a05cb3a94049615c Mon Sep 17 00:00:00 2001 From: Mark Rada Date: Fri, 5 Jun 2020 01:04:07 -0400 Subject: [PATCH] Add support for KEEPTTL option in the SET command This option was introduced in Redis 6.0.0 --- CHANGELOG.md | 1 + lib/redis.rb | 4 ++++ test/lint/strings.rb | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b310a1bf..da8a91d67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Increase buffer size in the ruby connector. See #880. * Fix thread safety of `Redis.queue`. See #878. * Deprecate `Redis::Future#==` as it's likely to be a mistake. See #876. +* Support `KEEPTTL` option for SET command. See #913. # 4.1.3 diff --git a/lib/redis.rb b/lib/redis.rb index a8e275899..7a228661c 100644 --- a/lib/redis.rb +++ b/lib/redis.rb @@ -786,6 +786,7 @@ def incrbyfloat(key, increment) # - `:px => Integer`: Set the specified expire time, 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, options = {}) args = [] @@ -802,6 +803,9 @@ def set(key, value, options = {}) xx = options[:xx] args.concat(["XX"]) if xx + keepttl = options[:keepttl] + args.concat(["KEEPTTL"]) if keepttl + synchronize do |client| if nx || xx client.call([:set, key, value.to_s] + args, &BoolifySet) diff --git a/test/lint/strings.rb b/test/lint/strings.rb index ce998022f..bf18c39fe 100644 --- a/test/lint/strings.rb +++ b/test/lint/strings.rb @@ -74,6 +74,15 @@ def test_set_with_xx end end + def test_set_with_keepttl + target_version "6.0.0" do + r.set("foo", "qux", :ex => 2) + assert_in_range 0..2, r.ttl("foo") + r.set("foo", "bar", :keepttl => true) + assert_in_range 0..2, r.ttl("foo") + end + end + def test_setex assert r.setex("foo", 1, "bar") assert_equal "bar", r.get("foo")