Skip to content

Commit

Permalink
Merge pull request #1248 from wholien/jc/add-expiretime
Browse files Browse the repository at this point in the history
Add EXPIRETIME
  • Loading branch information
byroot committed Jan 30, 2024
2 parents e16cab8 + 2e693be commit a51d50c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/redis/commands/keys.rb
Expand Up @@ -105,6 +105,14 @@ 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
#
# @param [String] key
# @return [Integer] expiry time of the key, specified as a UNIX timestamp
def expiretime(key)
send_command([:expiretime, key])
end

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

# Get the expiration for a key as a UNIX timestamp.
def expiretime(key)
node_for(key).expiretime(key)
end

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

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

exp_time = (Time.now + 2).to_i
r.expireat("foo", exp_time)
assert_equal exp_time, r.expiretime("foo")

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

def test_pexpireat
r.set("foo", "s1")
assert r.pexpireat("foo", (Time.now + 2).to_i * 1_000)
Expand Down

0 comments on commit a51d50c

Please sign in to comment.