diff --git a/lib/redis/commands/keys.rb b/lib/redis/commands/keys.rb index 6b1a6e860..0b731c885 100644 --- a/lib/redis/commands/keys.rb +++ b/lib/redis/commands/keys.rb @@ -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 diff --git a/lib/redis/distributed.rb b/lib/redis/distributed.rb index acd1a2c9a..21062c12b 100644 --- a/lib/redis/distributed.rb +++ b/lib/redis/distributed.rb @@ -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) diff --git a/test/lint/value_types.rb b/test/lint/value_types.rb index 5ee2f2ca8..6edff9725 100644 --- a/test/lint/value_types.rb +++ b/test/lint/value_types.rb @@ -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)