diff --git a/lib/redis.rb b/lib/redis.rb index 8d478e2ac..44f00278c 100644 --- a/lib/redis.rb +++ b/lib/redis.rb @@ -1110,6 +1110,45 @@ def getset(key, value) end end + # Get the value of key and delete the key. This command is similar to GET, + # except for the fact that it also deletes the key on success. + # + # @param [String] key + # @return [String] the old value stored in the key, or `nil` if the key + # did not exist + def getdel(key) + synchronize do |client| + client.call([:getdel, key]) + end + end + + # Get the value of key and optionally set its expiration. GETEX is similar to + # GET, but is a write command with additional options. When no options are + # provided, GETEX behaves like GET. + # + # @param [String] key + # @param [Hash] options + # - `:ex => Integer`: Set the specified expire time, in seconds. + # - `:px => Integer`: Set the specified expire time, in milliseconds. + # - `:exat => true`: Set the specified Unix time at which the key will + # expire, in seconds. + # - `:pxat => true`: Set the specified Unix time at which the key will + # expire, in milliseconds. + # - `:persist => true`: Remove the time to live associated with the key. + # @return [String] The value of key, or nil when key does not exist. + def getex(key, ex: nil, px: nil, exat: nil, pxat: nil, persist: false) + args = [:getex, key] + args << "EX" << ex if ex + args << "PX" << px if px + args << "EXAT" << exat if exat + args << "PXAT" << pxat if pxat + args << "PERSIST" if persist + + synchronize do |client| + client.call(args) + end + end + # Get the length of the value stored in a key. # # @param [String] key diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index 7d823886a..7586139bb 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -316,6 +316,16 @@ def get(key) node_for(key).get(key) end + # Get the value of a key and delete it. + def getdel(key) + node_for(key).getdel(key) + end + + # Get the value of a key and sets its time to live based on options. + def getex(key, **options) + node_for(key).getex(key, **options) + end + # Get the values of all the given keys as an Array. def mget(*keys) mapped_mget(*keys).values_at(*keys) diff --git a/test/lint/strings.rb b/test/lint/strings.rb index e9ecf58c5..291f5fd04 100644 --- a/test/lint/strings.rb +++ b/test/lint/strings.rb @@ -115,6 +115,22 @@ def test_psetex_with_non_string_value end end + def test_getex + target_version "6.2" do + assert r.setex("foo", 1000, "bar") + assert_equal "bar", r.getex("foo", persist: true) + assert_equal(-1, r.ttl("foo")) + end + end + + def test_getdel + target_version "6.2" do + assert r.set("foo", "bar") + assert_equal "bar", r.getdel("foo") + assert_equal nil, r.get("foo") + end + end + def test_getset r.set("foo", "bar")