Skip to content

Commit 94d606c

Browse files
committed
[Redis 2.6] Add support to getdel
1 parent 97357e4 commit 94d606c

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

lib/redis.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,18 @@ def getset(key, value)
11101110
end
11111111
end
11121112

1113+
# Get the value of key and delete the key. This command is similar to GET,
1114+
# except for the fact that it also deletes the key on success.
1115+
#
1116+
# @param [String] key
1117+
# @return [String] the old value stored in the key, or `nil` if the key
1118+
# did not exist
1119+
def getdel(key)
1120+
synchronize do |client|
1121+
client.call([:getdel, key])
1122+
end
1123+
end
1124+
11131125
# Get the length of the value stored in a key.
11141126
#
11151127
# @param [String] key

lib/redis/distributed.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ def get(key)
316316
node_for(key).get(key)
317317
end
318318

319+
# Get the value of a key and delete it.
320+
def getdel(key)
321+
node_for(key).getdel(key)
322+
end
323+
319324
# Get the values of all the given keys as an Array.
320325
def mget(*keys)
321326
mapped_mget(*keys).values_at(*keys)

test/lint/strings.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ def test_psetex_with_non_string_value
115115
end
116116
end
117117

118+
def test_getdel
119+
target_version "6.2" do
120+
assert r.set("foo", "bar")
121+
assert_equal "bar", r.getdel("foo")
122+
assert_equal nil, r.get("foo")
123+
end
124+
end
125+
118126
def test_getset
119127
r.set("foo", "bar")
120128

0 commit comments

Comments
 (0)