Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Redis 2.6] Add support to getdel and getex #1024

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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