Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support KEEPTTL option in SET command #913

Merged
merged 1 commit into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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