Skip to content

Commit

Permalink
Merge pull request #913 from Marketcircle/v6-set
Browse files Browse the repository at this point in the history
Support KEEPTTL option in SET command
  • Loading branch information
byroot committed Jun 9, 2020
2 parents cc4d1e0 + f59a49f commit ba82682
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 ba82682

Please sign in to comment.