Skip to content

Commit

Permalink
Add support for KEEPTTL option in the SET command
Browse files Browse the repository at this point in the history
This option was introduced in Redis 6.0.0
  • Loading branch information
ferrous26 committed Jun 6, 2020
1 parent cc4d1e0 commit f59a49f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/redis.rb
Expand Up @@ -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 = []
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/lint/strings.rb
Expand Up @@ -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")
Expand Down

0 comments on commit f59a49f

Please sign in to comment.