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

Add EXPIRETIME #1248

Merged
merged 1 commit into from
Jan 30, 2024
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
8 changes: 8 additions & 0 deletions lib/redis/commands/keys.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading