Skip to content

Commit a3d8071

Browse files
authored
Merge pull request #1036 from fatkodima/set-get
[Redis 6.2] Add GET option to SET command
2 parents 6e7b38c + cde42bd commit a3d8071

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/redis.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,9 @@ def incrbyfloat(key, increment)
841841
# - `:nx => true`: Only set the key if it does not already exist.
842842
# - `:xx => true`: Only set the key if it already exist.
843843
# - `:keepttl => true`: Retain the time to live associated with the key.
844+
# - `:get => true`: Return the old string stored at key, or nil if key did not exist.
844845
# @return [String, Boolean] `"OK"` or true, false if `:nx => true` or `:xx => true`
845-
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil)
846+
def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, keepttl: nil, get: nil)
846847
args = [:set, key, value.to_s]
847848
args << "EX" << ex if ex
848849
args << "PX" << px if px
@@ -851,6 +852,7 @@ def set(key, value, ex: nil, px: nil, exat: nil, pxat: nil, nx: nil, xx: nil, ke
851852
args << "NX" if nx
852853
args << "XX" if xx
853854
args << "KEEPTTL" if keepttl
855+
args << "GET" if get
854856

855857
synchronize do |client|
856858
if nx || xx

test/lint/strings.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ def test_set_with_keepttl
9797
end
9898
end
9999

100+
def test_set_with_get
101+
target_version "6.2" do
102+
r.set("foo", "qux")
103+
104+
assert_equal "qux", r.set("foo", "bar", get: true)
105+
assert_equal "bar", r.get("foo")
106+
107+
assert_nil r.set("baz", "bar", get: true)
108+
assert_equal "bar", r.get("baz")
109+
end
110+
end
111+
100112
def test_setex
101113
assert r.setex("foo", 1, "bar")
102114
assert_equal "bar", r.get("foo")

0 commit comments

Comments
 (0)