Skip to content

Commit

Permalink
Merge pull request #1251 from wholien/jc/add-pexpiretime
Browse files Browse the repository at this point in the history
Add PEXPIRETIME
  • Loading branch information
byroot committed Jan 30, 2024
2 parents a51d50c + 1e52c11 commit cd88550
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/redis/commands/keys.rb
Expand Up @@ -105,10 +105,10 @@ def expireat(key, unix_time, nx: nil, xx: nil, gt: nil, lt: nil)
send_command(args, &Boolify)
end

# Get a key's expiration time as an absolute Unix timestamp (since January 1, 1970) in seconds
# Get a key's expiry time specified as number of seconds from UNIX Epoch
#
# @param [String] key
# @return [Integer] expiry time of the key, specified as a UNIX timestamp
# @return [Integer] expiry time specified as number of seconds from UNIX Epoch
def expiretime(key)
send_command([:expiretime, key])
end
Expand Down Expand Up @@ -169,6 +169,14 @@ def pexpireat(key, ms_unix_time, nx: nil, xx: nil, gt: nil, lt: nil)
send_command(args, &Boolify)
end

# Get a key's expiry time specified as number of milliseconds from UNIX Epoch
#
# @param [String] key
# @return [Integer] expiry time specified as number of milliseconds from UNIX Epoch
def pexpiretime(key)
send_command([:pexpiretime, key])
end

# Get the time to live (in milliseconds) for a key.
#
# @param [String] key
Expand Down
5 changes: 5 additions & 0 deletions lib/redis/distributed.rb
Expand Up @@ -150,6 +150,11 @@ def pexpireat(key, ms_unix_time, **kwarg)
node_for(key).pexpireat(key, ms_unix_time, **kwarg)
end

# Get the expiration for a key as number of milliseconds from UNIX Epoch.
def pexpiretime(key)
node_for(key).pexpiretime(key)
end

# Get the time to live (in milliseconds) for a key.
def pttl(key)
node_for(key).pttl(key)
Expand Down
13 changes: 13 additions & 0 deletions test/lint/value_types.rb
Expand Up @@ -153,6 +153,19 @@ def test_pexpireat_keywords
end
end

def test_pexpiretime
target_version "7.0.0" do
r.set("foo", "blar")
assert_equal(-1, r.pexpiretime("foo"))

exp_time = (Time.now + 2).to_i * 1_000
r.pexpireat("foo", exp_time)
assert_equal exp_time, r.pexpiretime("foo")

assert_equal(-2, r.pexpiretime("key-that-exists-not"))
end
end

def test_persist
r.set("foo", "s1")
r.expire("foo", 1)
Expand Down

0 comments on commit cd88550

Please sign in to comment.