Skip to content

Commit 688ac95

Browse files
authored
Merge pull request #1024 from heyvito/feat/getex-getdel
[Redis 2.6] Add support to getdel and getex
2 parents 97357e4 + 4d70d34 commit 688ac95

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

lib/redis.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,45 @@ 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+
1125+
# Get the value of key and optionally set its expiration. GETEX is similar to
1126+
# GET, but is a write command with additional options. When no options are
1127+
# provided, GETEX behaves like GET.
1128+
#
1129+
# @param [String] key
1130+
# @param [Hash] options
1131+
# - `:ex => Integer`: Set the specified expire time, in seconds.
1132+
# - `:px => Integer`: Set the specified expire time, in milliseconds.
1133+
# - `:exat => true`: Set the specified Unix time at which the key will
1134+
# expire, in seconds.
1135+
# - `:pxat => true`: Set the specified Unix time at which the key will
1136+
# expire, in milliseconds.
1137+
# - `:persist => true`: Remove the time to live associated with the key.
1138+
# @return [String] The value of key, or nil when key does not exist.
1139+
def getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false)
1140+
args = [:getex, key]
1141+
args << "EX" << ex if ex
1142+
args << "PX" << px if px
1143+
args << "EXAT" << exat if exat
1144+
args << "PXAT" << pxat if pxat
1145+
args << "PERSIST" if persist
1146+
1147+
synchronize do |client|
1148+
client.call(args)
1149+
end
1150+
end
1151+
11131152
# Get the length of the value stored in a key.
11141153
#
11151154
# @param [String] key

lib/redis/distributed.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,16 @@ 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+
324+
# Get the value of a key and sets its time to live based on options.
325+
def getex(key, **options)
326+
node_for(key).getex(key, **options)
327+
end
328+
319329
# Get the values of all the given keys as an Array.
320330
def mget(*keys)
321331
mapped_mget(*keys).values_at(*keys)

test/lint/strings.rb

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

118+
def test_getex
119+
target_version "6.2" do
120+
assert r.setex("foo", 1000, "bar")
121+
assert_equal "bar", r.getex("foo", persist: true)
122+
assert_equal(-1, r.ttl("foo"))
123+
end
124+
end
125+
126+
def test_getdel
127+
target_version "6.2" do
128+
assert r.set("foo", "bar")
129+
assert_equal "bar", r.getdel("foo")
130+
assert_equal nil, r.get("foo")
131+
end
132+
end
133+
118134
def test_getset
119135
r.set("foo", "bar")
120136

0 commit comments

Comments
 (0)