Skip to content

Commit

Permalink
[Redis 6.2] Add support to getex
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvito committed Aug 18, 2021
1 parent 94d606c commit 4d70d34
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/redis.rb
Expand Up @@ -1122,6 +1122,33 @@ def 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
5 changes: 5 additions & 0 deletions lib/redis/distributed.rb
Expand Up @@ -321,6 +321,11 @@ 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
8 changes: 8 additions & 0 deletions test/lint/strings.rb
Expand Up @@ -115,6 +115,14 @@ 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")
Expand Down

0 comments on commit 4d70d34

Please sign in to comment.