{"id":26130,"date":"2022-06-25T11:39:29","date_gmt":"2022-06-25T01:39:29","guid":{"rendered":"https:\/\/database.guide\/?p=26130"},"modified":"2022-06-27T11:57:39","modified_gmt":"2022-06-27T01:57:39","slug":"redis-expiretime-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-expiretime-command-explained\/","title":{"rendered":"Redis EXPIRETIME Command Explained"},"content":{"rendered":"\n<p>The Redis <code>EXPIRETIME<\/code> command returns the absolute Unix timestamp in seconds at which the given key will expire. This is the number of seconds since 00:00:00 UTC on 1 January 1970 until the expiry time of the key.<\/p>\n\n\n\n<p>The <code>EXPIRETIME<\/code> command was introduced in Redis 7.0.0.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRETIME key<\/code><\/pre>\n\n\n\n<p>Where <code>key<\/code> is the key for which to get the expiry time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we set a key with an expiry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET lunch \"Chicken Salad\" EX 86400<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>We can now use the <code>EXPIRETIME<\/code> command to check that key&#8217;s expiry time in Unix time:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRETIME lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1655886492<\/pre>\n\n\n\n<p>This is the absolute Unix timestamp of the key&#8217;s expiry. If we run the statement again at a later time, we get exactly the same result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRETIME lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1655886492<\/pre>\n\n\n\n<p>The absolute Unix timestamp of the key&#8217;s expiry is not to be confused with the current time to live (TTL) of the key.<\/p>\n\n\n\n<p>Let&#8217;s run the <code>TTL<\/code> command against it to check its TTL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TTL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86214<\/pre>\n\n\n\n<p>We get an integer reply with the current time to live. If we run the same command again, we get a different result, due to the expiry getting nearer (and therefore, the time to live getting shorter):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TTL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 86136<\/pre>\n\n\n\n<p>This is in contrast to the absolute Unix timestamp of the expiry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Keys Without an Expiry<\/h2>\n\n\n\n<p>If the key has no expiry, <code>EXPIRETIME<\/code> returns an integer reply of <code>-1<\/code>.<\/p>\n\n\n\n<p>To test this, let&#8217;s remove the expiry of our key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PERSIST lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>The <code><a href=\"https:\/\/database.guide\/redis-persist-command-explained\/\" data-type=\"post\" data-id=\"26149\">PERSIST<\/a><\/code> command removes any timeout on the given key. We got an integer reply of <code>1<\/code>, which means that it successfully removed the timeout on our key.<\/p>\n\n\n\n<p>Now let&#8217;s run <code>EXPIRETIME<\/code> against that key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRETIME lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) -1<\/pre>\n\n\n\n<p>As expected, we get an integer reply of <code>-1<\/code>, which means that the key doesn&#8217;t have an expiry set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Keys<\/h2>\n\n\n\n<p>If the key doesn&#8217;t exist, <code>EXPIRETIME<\/code> returns an integer reply of <code>-2<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s delete our key: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEL lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) 1<\/pre>\n\n\n\n<p>The key has been deleted.<\/p>\n\n\n\n<p>And now let&#8217;s try to run <code>EXPIRETIME<\/code> against the deleted key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPIRETIME lunch<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(integer) -2<\/pre>\n\n\n\n<p>As expected, an integer reply of <code>-2<\/code>.<\/p>\n\n\n\n<p>There&#8217;s also a <code><a href=\"https:\/\/database.guide\/redis-pexpiretime-command-explained\/\">PEXPIRETIME<\/a><\/code> command that does the same thing as <code>EXPIRETIME<\/code>, except that it returns the result in milliseconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Redis EXPIRETIME command returns the absolute Unix timestamp in seconds at which the given key will expire. This is the number of seconds since 00:00:00 UTC on 1 January 1970 until the expiry time of the key. The EXPIRETIME command was introduced in Redis 7.0.0.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-26130","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=26130"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26130\/revisions"}],"predecessor-version":[{"id":26265,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26130\/revisions\/26265"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}