Skip to content

Commit

Permalink
Merge pull request #1024 from heyvito/feat/getex-getdel
Browse files Browse the repository at this point in the history
[Redis 2.6] Add support to getdel and getex
  • Loading branch information
byroot authored Aug 18, 2021
2 parents 97357e4 + 4d70d34 commit 688ac95
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions test/lint/strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 688ac95

Please sign in to comment.