Skip to content

Commit 4d70d34

Browse files
committed
[Redis 6.2] Add support to getex
1 parent 94d606c commit 4d70d34

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/redis.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,33 @@ def getdel(key)
11221122
end
11231123
end
11241124

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+
11251152
# Get the length of the value stored in a key.
11261153
#
11271154
# @param [String] key

lib/redis/distributed.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ def getdel(key)
321321
node_for(key).getdel(key)
322322
end
323323

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+
324329
# Get the values of all the given keys as an Array.
325330
def mget(*keys)
326331
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_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+
118126
def test_getdel
119127
target_version "6.2" do
120128
assert r.set("foo", "bar")

0 commit comments

Comments
 (0)